我正在学习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
容器以获得预期结果?任何方向将不胜感激。
答案 0 :(得分:2)
如果GUI可以有Buying an investment
& Messages
作为TitledBorder
,我按照以下方式列出:
BorderLayout
Buying an investment
使用两列GridBagLayout
标签和字段。将它放在边框布局的CENTER
。Reset
中的Buy
/ GridLayout
按钮,带有充足的EmptyBorder
和垂直布局填充。将该面板放在边框布局的LINE_END
。GridLayout
中使用其他PAGE_END
。(
JFrame
是1000w x 800h)
不要试图猜测GUI需要的大小。布局所有组件,然后pack()
框架。 GUI将成为显示其包含的所有内容所需的最小尺寸。
答案 1 :(得分:2)
这主要是@ AndrewThompson答案的详细阐述。 与原始方法相比的变化和简化是:
setSize
来电。而是让布局经理选择
在pack()
上调用JFrame
时的尺寸
当用户调整大小时。 CENTER
,EAST
和SOUTH
部分
在BorderLayout
)。TitledBorder
,而不是JLabel
。GridBagLayout
代替GridLayout
,因为那样你
通过使用适当的GridBagConstraints
可以更好地控制
(尤其是fill
,anchor
,weightx
,weighty
,insets
)。上面的布局是通过以下代码生成的:
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);
}
}