2

我想制作一个 JFrame 来显示这样的 GUI:http: //i.stack.imgur.com/2POFs.png (我不允许嵌入图片)

空白空间是相同的 X 和 Y 尺寸,按钮是相同的 X 和 Y 尺寸,JTextArea 是框架的一半。

public static JFrame frame = new JFrame ("Launcher " + version);
public static GridBagConstraints c = new GridBagConstraints();
public static JTextArea news = new JTextArea();
public static JButton launchGame = new JButton("Launch Game");
public static JButton settings = new JButton("Settings");
public static JButton exitGame = new JButton("Exit");

public static void main(String[] args) 
{
    news.setEditable(false);
    news.setBounds(0, 0, (screenSize.width - 500) / 2,  screenSize.height - 400);
    news.append("                        NEWS:\n");
    news.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.gray, Color.black));

    frame.setLayout(new GridBagLayout());
    frame.setBounds(300, 200, screenSize.width - 500,  screenSize.height - 400);
    c.fill = GridBagConstraints.VERTICAL;
    c.weighty = 0.5;
    c.gridx = 0;
    c.gridy = 0;
    c.gridheight = 3;
    frame.add(news, c);
    frame.setVisible(true);
    c.fill = GridBagConstraints.EAST;
    c.weightx = 0.5;
    c.gridx = 1;
    c.gridheight = 1;
    frame.add(launchGame, c);
    c.gridy = 1;
    frame.add(settings, c);
            c.gridy = 2;
    frame.add(exitGame, c);
}
4

2 回答 2

2

首先...

c.fill = GridBagConstraints.EAST;

这不是我想你想做的。我认为填充应该至少HORIZONTAL

第二...

您可以Insets用来调整组件之间的空间,例如

在此处输入图像描述

public class TestLayout {

    public static void main(String[] args) {    
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new FormPane());
                frame.setSize(500, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    protected static class FormPane extends JPanel {

        private JTextArea news = new JTextArea();
        private JButton launchGame = new JButton("Launch Game");
        private JButton settings = new JButton("Settings");
        private JButton exitGame = new JButton("Exit");

        public FormPane() {
            setLayout(new GridBagLayout());

            news.setEditable(false);
            news.append("                        NEWS:\n");
            news.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.gray, Color.black));

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weighty = 1;
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridheight = GridBagConstraints.REMAINDER;
            gbc.weightx = 0.25;
            add(news, gbc);

            gbc.insets = new Insets(0, 50, 0, 50);
            gbc.gridheight = 1;
            gbc.gridx++;            
            add(launchGame, gbc);
            gbc.gridy = 1;
            add(settings, gbc);
            gbc.gridy = 2;
            add(exitGame, gbc);
        }
    }
}

我也可能倾向于将按钮放置在自己的容器中(如 a JPanel),并可能使用 aGridLayout来布置它们,然后使用上述建议来放置文本区域和按钮容器。

这隔离了单独的布局需求,而不会使布局进一步复杂化......

于 2012-10-08T19:35:25.100 回答
2

这是你想要的吗?我在您的按钮约束中添加/修改了以下内容:

    c.fill = GridBagConstraints.HORIZONTAL;
    c.insets = new Insets(0, 10, 0, 10); // 10 is the marginal

这是完整的代码:

public static void main(String[] args)
{
    news.setEditable(false);
    news.append("                        NEWS:\n");
    news.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.gray, Color.black));

    frame.setLayout(new GridBagLayout());
    c.fill = GridBagConstraints.VERTICAL;
    c.weighty = 0.5;
    c.gridx = 0;
    c.gridy = 0;
    c.gridheight = 3;
    frame.add(news, c);
    c.fill = GridBagConstraints.HORIZONTAL;
    c.insets = new Insets(10, 10, 10, 10); // 10 is the marginal
    c.weightx = 0.5;
    c.gridx = 1;
    c.gridheight = 1;
    frame.add(launchGame, c);
    c.gridy = 1;
    frame.add(settings, c);
    c.gridy = 2;
    frame.add(exitGame, c);

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

这是一个类似的布局使用BoxLayout代替(我认为更容易使用):

private static JComponent buildButtons() {
    for (JButton button : Arrays.asList(launchGame, settings, exitGame)) {
        button.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
    }

    Box b = Box.createVerticalBox();
    b.add(Box.createVerticalStrut(10));
    b.add(launchGame);
    b.add(Box.createVerticalStrut(10));
    b.add(settings);
    b.add(Box.createVerticalStrut(10));
    b.add(exitGame);
    b.add(Box.createVerticalStrut(10));

    return b;
}

static JComponent buildUI() {
    Box b = Box.createHorizontalBox();
    b.add(news);
    b.add(Box.createHorizontalStrut(10));
    b.add(buildButtons());
    b.add(Box.createHorizontalStrut(10));

    return b;
}

public static void main(String[] args)
{
    news.setEditable(false);
    news.append("                        NEWS:\n");
    news.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.gray, Color.black));

    frame.add(buildUI());
    frame.pack();
    frame.setVisible(true);
}
于 2012-10-08T18:56:42.967 回答