views:

240

answers:

3

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);
    }
}
+2  A: 

I suggest you start by reading the section from the Swing tutorial on Using Layout ManagersYour code won't work because you are trying to add 3 components to the "center" of the BorderLayout which won't work. Choose a LayoutManager that better meets your needs.

There is no need to use the setBounds(...) method when using a layout manager. The job of the layout manager is position the components based on the rules of the layout manager. The preferred size of each button should be the same since you have not added any text to the buttons.

Also, there is not need to invoke setVisible() on the buttons, all Swing components are visible by default, except the top level containers (JFrame, JDialog).

camickr
A: 

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.

TofuBeer
A: 

Since you're setting bounds on everything, you don't need a LayoutManager at all. Set your LayoutManager to null, then you can position everything yourself, as you are in your example.

this.setLayoutManager(null)
Sam Barnum
This solves the problem that the original poster is asking, but - except for a few cases - using `null` layout is just introducing new problems.
Nate