Java Swing / AWT gui格式化

时间:2017-11-23 23:10:05

标签: java swing layout-manager

我正在学习Java GUI,关于布局管理器,我正在寻找使用以下布局创建GUI。什么是最好的方法来解决这个问题? (JFrame是1000w x 800h)

以下是我一直想到的成功案例,以及与所需布局不匹配的凌乱解决方案完全

JFrame myFrame (GridLayout(2,1))
    - JPanel topPanel (BorderLayout)
        - JPanel topLeftPanel (GridLayout(9,2) & setSize 666,400)
            - JLabel buyingAnInvestment      - Jlabel empty1
            - JLabel type                    - JComboBox typeSelect
            - JLabel symbol                  - JTextField symbolField
            - JLabel empty2                  - JLabel empty3
            - JLabel name                    - JTextField nameField
            - JLabel empty4                  - JLabel empty5
            - JLabel quantity                - JTextField quantityField
            - JLabel empty6                  - JLabel epty7
            - JLabel price                   - JTextField price
        - JPanel topRightPanel (GridLayout(2,1) & setSize 333,400)
            - JButton reset
            - JButton buy
    - JPanel bottomPanel (What should I do for this?)
        - JLabel messages
        - JTextArea & JScrollArea

如何布局组件和JPanel容器以获得预期结果?任何方向将不胜感激。

2 个答案:

答案 0 :(得分:2)

如果GUI可以有Buying an investment& Messages作为TitledBorder,我按照以下方式列出:

  • 外部布局 - BorderLayout
    1. Buying an investment使用两列GridBagLayout标签和字段。将它放在边框布局的CENTER
    2. 单个列Reset中的
    3. Buy / GridLayout按钮,带有充足的EmptyBorder和垂直布局填充。将该面板放在边框布局的LINE_END
    4. 文本区域隐藏了一个面板/约束,将其拉伸到全宽度&身高E.G.在边框布局的GridLayout中使用其他PAGE_END
  

JFrame是1000w x 800h)

不要试图猜测GUI需要的大小。布局所有组件,然后pack()框架。 GUI将成为显示其包含的所有内容所需的最小尺寸。

答案 1 :(得分:2)

这主要是@ AndrewThompson答案的详细阐述。 与原始方法相比的变化和简化是:

  • 没有明确的setSize来电。而是让布局经理选择 在pack()上调用JFrame时的尺寸 当用户调整大小时。
  • 只有3个面板(CENTEREASTSOUTH部分 在BorderLayout)。
  • 标题“购买投资”和“消息”实施为 TitledBorder,而不是JLabel
  • 使用GridBagLayout代替GridLayout,因为那样你 通过使用适当的GridBagConstraints可以更好地控制 (尤其是fillanchorweightxweightyinsets)。

screenshot

上面的布局是通过以下代码生成的:

public class Main {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(Main::initGUI);
    }

    private static void initGUI() {
        JFrame myFrame = new JFrame("Investment Portfolio");
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setLayout(new BorderLayout());

        JPanel topLeftPanel = new JPanel(new GridBagLayout());
        myFrame.add(topLeftPanel, BorderLayout.CENTER);
        topLeftPanel.setBorder(BorderFactory.createTitledBorder("Buying an investment"));
        GridBagConstraints labelConstraints = new GridBagConstraints();
        labelConstraints.anchor = GridBagConstraints.WEST;
        labelConstraints.gridx = 0;
        labelConstraints.gridy = 0;
        labelConstraints.weightx = 0.5;
        labelConstraints.weighty = 1;
        labelConstraints.insets = new Insets(5, 10, 5, 10);
        GridBagConstraints fieldConstraints = new GridBagConstraints();
        fieldConstraints.anchor = GridBagConstraints.WEST;
        fieldConstraints.gridx = 1;
        fieldConstraints.gridy = 0;
        fieldConstraints.weightx = 0.5;
        fieldConstraints.weighty = 1;
        fieldConstraints.insets = new Insets(5, 10, 5, 10);
        topLeftPanel.add(new JLabel("Type"), labelConstraints);
        JComboBox<String> typeSelect = new JComboBox<>(new String[] { "stock", "aaaaaaaa" });
        topLeftPanel.add(typeSelect, fieldConstraints);
        labelConstraints.gridy++;
        topLeftPanel.add(new JLabel("Symbol"), labelConstraints);
        JTextField symbolField = new JTextField(10);
        fieldConstraints.gridy++;
        topLeftPanel.add(symbolField, fieldConstraints);
        labelConstraints.gridy++;
        topLeftPanel.add(new JLabel("Name"), labelConstraints);
        JTextField nameField = new JTextField(20);
        fieldConstraints.gridy++;
        topLeftPanel.add(nameField, fieldConstraints);
        labelConstraints.gridy++;
        topLeftPanel.add(new JLabel("Quantity"), labelConstraints);
        JTextField quantityField = new JTextField(6);
        fieldConstraints.gridy++;
        topLeftPanel.add(quantityField, fieldConstraints);
        labelConstraints.gridy++;
        topLeftPanel.add(new JLabel("Price"), labelConstraints);
        JTextField priceField = new JTextField(6);
        fieldConstraints.gridy++;
        topLeftPanel.add(priceField, fieldConstraints);

        JPanel topRightPanel = new JPanel(new GridBagLayout());
        topRightPanel.setBorder(BorderFactory.createEmptyBorder());
        myFrame.add(topRightPanel, BorderLayout.EAST);
        GridBagConstraints buttonConstraints = new GridBagConstraints();
        buttonConstraints.fill = GridBagConstraints.HORIZONTAL;
        buttonConstraints.insets = new Insets(10, 10, 10, 10);
        buttonConstraints.weighty = 1;
        buttonConstraints.gridy = 0;
        JButton reset = new JButton("Reset");
        topRightPanel.add(reset, buttonConstraints);
        JButton buy = new JButton("Buy");
        buttonConstraints.gridy++;
        topRightPanel.add(buy, buttonConstraints);

        JPanel bottomPanel = new JPanel(new BorderLayout());
        myFrame.add(bottomPanel, BorderLayout.SOUTH);
        bottomPanel.setBorder(BorderFactory.createTitledBorder("Messages"));
        JTextArea messagesArea = new JTextArea(6, 30);
        bottomPanel.add(new JScrollPane(messagesArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS), BorderLayout.CENTER);

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