What's Here?
- Members: 340,029
- Replies: 920,147
- Topics: 154,875
- Snippets: 4,853
- Tutorials: 1,256
- Total Online: 4,094
- Members: 110
- Guests: 3,984
|
Welcome to Dream.In.Code |
|
|
Become an Expert!
Join 340,029 Programmers for FREE! Get instant access to thousands  of experts, tutorials, code snippets, and more! There are 4,094 people online right now. Registration is fast and FREE... Join Now!
Chat LIVE With a Expert
|
JButton Manipulation?
JButton Manipulation?
Rate Topic:
   
Posted 09 January 2008 - 11:11 AM
how would you manipulate JButton? such as changing the buttons to specific locations or resizing the buttons?
I've looked around but sites have just said how to use it. plz and ty
import java.awt.*;
import javax.swing.*;
public class calcprgrm1 extends JFrame
{
JTextField field = new JTextField(25);
JButton button1 = new JButton("C");//buttons wanted to manipulate
JButton button2 = new JButton("CE");
JButton button3 = new JButton("");
public calcprgrm1()
{
super("JFrame with Panels");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
JPanel pane2 = new JPanel();
Container con = getContentPane();
con.setLayout(new FlowLayout());
con.add(pane);
con.add(pane2);
pane.add(field);
pane2.add(button1);
pane2.add(button2);
pane2.add(button3)
setSize(300, 250);
setVisible(true);
}
public static void main(String[] args)
{
calcprgrm1 panel = new calcprgrm1();
}
}
Posted 09 January 2008 - 12:15 PM
Take a look at the methods "setLocation" and "setBounds" inherited from Component... which a JButton is part of. These work when you have set no layout manager (aka setLayout(null)). Otherwise some components are determined by the type of layout you have. You will have to check out the documentation on the type of layout manager you are using to see what you can do about size. One great method you could also look at is "setPreferredSize" which works with some layouts like box layout etc.
Hope this helps.
Posted 09 January 2008 - 02:12 PM
yes, when dealing with more complicated layouts, using setSize and setPreferredsize are ideal. It is often a good idea to use setpreferredsize anyway, in case there is a resizing issue or alignment problem, java will do it's best to accommodate those settings.
Sun.com Documentation on JButton:
http://java.sun.com/...ng/JButton.html
Posted 09 January 2008 - 03:30 PM
hey i was just playing with setLocation() just to see how that works. i usually used layout manager to position but im not getting the result im looking for. the button doesnt even appear. the question is if i have no layout where does the button go when you add it? heres what i got
class Test2 extends JFrame
{
JButton but = new JButton("Button");
JPanel pan = new JPanel();
public Test2()
{
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(200, 200);
pan.add(but);
but.setLocation(10, 10);
}
public static void main(String args[])
{
Test2 t = new Test2();
t.setVisible(true);
}
}
Posted 09 January 2008 - 05:00 PM
Well for one you are not adding the panel to the form. I will go into this in more depth when I get home later tonight.
Posted 09 January 2008 - 05:08 PM
i dont see why you use a jpanel. just set the layout to null as the other guy said and youre good to go.
import javax.swing.*;
public class myClass extends JFrame{
JButton but = new JButton("Button");
public myClass(){
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(200, 200);
this.add(but);
but.setLocation(100, 10);
but.setSize(50, 50);
}
public static void main(String args[]){
myClass t = new myClass();
t.setVisible(true);
}
}
ps: martyr2 is right. in your code you dont add the jpanel to your form.
ps #2: thanks for answering martyr2 but you keep surfing the internet while youre at work and youre gonna get fired. take care.
This post has been edited by jowharshamshiri: 09 January 2008 - 05:12 PM
Posted 09 January 2008 - 05:35 PM
ah, ok. i was told in class that components go on a panel and then the panel gets added to the frame. guess it works just as well without the panel. thanks
Posted 09 January 2008 - 05:47 PM
keep in mind if you want your code to be backward compatible you shouldnt add stuff directly to your form. the guys at the class youre attending are right. this feature is available since j2se 5. so my code is not going to work with compilers before that.
ps: follow the routine of adding to jpanel first. thats going to help you alot when you work with many components.
This post has been edited by jowharshamshiri: 09 January 2008 - 05:49 PM
1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users
|
Be Social
Programming
Web Development
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|