开发者

Problem with FlowLayout

开发者 https://www.devze.com 2022-12-25 13:21 出处:网络
public class MyFrame extends JFrame { public MyFrame(String title) { setSize(200, 200); setTitle(Integer.toString(super.getSize().width));
public class MyFrame extends JFrame
{
public MyFrame(String title)
{

    setSize(200, 200);
    setTitle(Integer.toString(super.getSize().width));
    setLayout(new FlowLayout());
    for (int i = 0; i < 5; ++i)
    {
        JButton b = new JButton();
        b.setSize(90,50);
        b.setText(Integer.toString(b.getSize().width));
        this.add(b);![alt text][1]
    }
    this.setVisible(true);
}
}

why if having button widht 90 I'm getting window where three buttons are in one row instead of tw开发者_Go百科o?


FlowLayout will lay out Components left-to-right (or right-to-left) wrapping them if required. If you wish to explicitly set the size of each JButton you should use setPreferredSize rather than setSize as layout managers typically make use of the minimum, preferred and maximum sizes when performing a layout.

Size properties are quite confusing - There is an interesting article here. In particular, note:

Are the size properties always honored?

Some layout managers, such as GridLayout, completely ignore the size properties.

FlowLayout, attempts to honor both dimensions of preferredSize, and possibly has no need to honor either minimumSize or maximumSize.


The FlowLayout just places component one beside the other in a left-to-right order. When the width reaches the one of the container that has that layout it simply wraps on the other line.

If you want to arrange them in a grid-style layout (like it seems you want) you can use the GridLayout that allows you to specify the number of columns and rows:

component.setLayout(new GridLayout(2,2))

The only downside of GridLayout is that every cell of the grid will be of the same size (which is usually good if you just have JButtons or JLabels but when you mix things it will be visually bad).

If you really need more power go with the GridBagLayout, very customizable but with a steeper learning curve at the beginning.

Probably your size problem is related to the fact that you are using setSize but in Swing these things have strange behaviours, you should try by setting setPreferredSize(200,200) instead of setSize. But don't ask me why!

NOTE: you should ALWAYS refer to the frame's content pane and not to the frame it self. When you set layout you should do getContentPane().setLayout(..), when you add items you should do getContentPane().add(..) and so on.

Errata: now every JFrame add, remove, setLayout automatically forward to the content pane.


For one thing, you're not using JFrame correctly: you don't add components directly to the frame, you add them to a JPanel that you then pass to the frame with setContentPane().

Also: it's not very elegant to directly subclass JFrame just to add components. Instead, create your frame as a separate object.

0

精彩评论

暂无评论...
验证码 换一张
取 消