开发者

Swing layout problem

Hello I'd like to get this swing layout inside a JPanel:

JLabel - JTextField - JComboBox

as the panel resizes I'd like the textField to expand but not the other two. Everything must remain in line. I tried grid bag layout but 开发者_开发百科doesn't work... or I can't. Ideas?


If you have three components and want the one in the middle to expand, you can for example use a BorderLayout, put your JLabel at BorderLayout.WEST, your JComboBox at BorderLayout.EAST and the one you want to expand (JTextField) at BorderLayout.CENTER.

The following is ugly, but the intent is to be minimal yet show the behavior you want:

public class Gotch {
    public static void main( String[] args ) {
        JFrame main = new JFrame();
        JPanel p = new JPanel();
        p.setLayout( new BorderLayout() );
        p.add( new JLabel( "test" ), BorderLayout.WEST );
        p.add( new JTextField( "growable" ), BorderLayout.CENTER );
        p.add( new JComboBox(), BorderLayout.EAST );
        main.add( p );
        main.pack();
        main.setVisible( true );
    }
}


Thought I'd post some code how to do it with GridBagLayout as well. Could be useful for when you have something that does not fit so perfectly with BorderLayout, which will be often when making GUIs.

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        JFrame frame = new JFrame("GridBagLayout Demo");
        frame.setLayout(new GridBagLayout());

        JLabel label = new JLabel("Demo Label");
        JTextField textField = new JTextField("Demo Text");
        JComboBox comboBox = new JComboBox(new String[] {"hello", "goodbye", "foo"});

        GridBagConstraints cons = new GridBagConstraints();
        cons.insets = new Insets(10, 10, 10, 10);
        frame.add(label, cons);

        cons.gridx = 1;
        cons.weightx = 1;
        cons.weighty = 1;
        cons.insets = new Insets(10, 0, 10, 10);
        cons.fill = GridBagConstraints.HORIZONTAL;

        frame.add(textField, cons);

        cons.gridx = 2;
        cons.weightx = 0;
        cons.weighty = 0;
        cons.insets = new Insets(10, 0, 10, 10);
        cons.fill = GridBagConstraints.NONE;

        frame.add(comboBox, cons);

        frame.pack();
        frame.setVisible(true);
    }
}


Extending Box is a nice alternative:

import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class MyPanel extends Box {

    public MyPanel(int axis) {
        super(axis);
        this.setPreferredSize(new Dimension(320, 240));
        JLabel lb = new JLabel("label");
        lb.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        this.add(lb);
        JTextField tf = new JTextField("field");
        this.add(tf);
        String [] items = { "One", "Two", "Three" };
        JComboBox c = new JComboBox(items);
        c.setMaximumSize(new Dimension(100, Short.MAX_VALUE));
        this.add(c);
    }

    private static void create() {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            MyPanel p = new MyPanel(BoxLayout.Y_AXIS);
            f.add(p);
            f.pack();
            f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                create();
            }
        });
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜