I’m Stuck with how to resize the third button to became the same size like the other two and place it on the bottom.
class ControlFrame extends JFrame
implements Runnable
{
JButton jb_inc = new JButton();
JButton jb_zero = new JButton();
JButton jb_dec = new JButton();
ControlFrame() {
super("Control Frame");
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
ControlFrame(int x,int y, int w, int h) {
this();
this.setBounds(x, y, w, h);
this.setVisible(true);
jb_inc.setBounds(10,10,90,20);
jb_zero.setBounds(10,40,90,20);
jb_dec.setBounds(10,60,90,20);
jb_inc.setVisible(true);
jb_zero.setVisible(true);
jb_dec.setVisible(true);
this.getContentPane().add(jb_inc);
this.getContentPane().add(jb_zero);
this.getContentPane().add(jb_dec);
}
public void run() {
}
}
public class Counting_Machine
{
public static void main(String[] args) {
ControlFrame cf = new ControlFrame(0,200,80,150);
}
}
Two suggestions:
1) put common things into the instance initialization block. Personally I shudder whenever I see a call to “this()”.
{
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
ControlFrame()
{
super(“Control Frame”);
}
ControlFrame(int x,int y, int w, int h)
{
super(“Control Frame”);
…
}
2) I would get rid of the x, y, w, h constructor… personally I have a class called WindowUtils that has a “position” method that figures out the screen size and then uses the values passed in to create a window that is the relative size of the screen. Then the code that creates the window calls that. I prefer to have as few constructors as possible (very often that is zero or one, I hardly ever have two or more).
3) this.getContentPane().add(jb_inc); can now be written as add(jb_inc); – since JDK 1.5 I think.
4) never call overrideable methods (all the things you have this. before) inside the constructor. If a subclass were to override “add” you could see things break. So you can call super.add(), or do the adds in another method, or make your class final.
Now to answer your question… 🙂
You need to make use of LayoutManagers to get what you want.
Depending on what you want you probably want to use a BorderLayout so you can get the button on the bottom.