我试图在一个框架中显示大约 7 或 8 个多个文本字段和 JLabel。我很难让他们全部出现。我知道显示 GUI 各个部分的唯一方法是创建一个 JFrame,然后在该框架内创建一个容器,然后创建多个面板并将它们添加到容器中。有一个更好的方法吗?一个不那么限制并允许我显示超过 4 帧的?(我仅限于选择 NORTH、EAST、SOUTH、WEST,并且这些位置只会显示一项)
这是我的代码:
Container content = n.getContentPane();
Container contentTwo = n.getContentPane();
JTextField JField = new JTextField(agentID);
JTextField stateField = new JTextField("Running");
JTextField transField = new JTextField(5);
JTextField opsField = new JTextField("0");
stateField.setEnabled(false);
transField.setEnabled(false);
JField.setEnabled(false);
opsField.setEnabled(false);
JLabel stateLabel = new JLabel("State:");
JLabel transLabel = new JLabel("Amount transferred:");
JLabel opsLabel = new JLabel("Operations Completed:");
JPanel fundsPanel = new JPanel(new BorderLayout());
JLabel agentLabel = new JLabel("Agent ID: ");
agentLabel.setDisplayedMnemonic(KeyEvent.VK_ENTER);
stateLabel.setLabelFor(stateField);
transLabel.setLabelFor(transField);
agentLabel.setLabelFor(JField);
opsLabel.setLabelFor(opsField);
fundsPanel.add(agentLabel, BorderLayout.WEST);
fundsPanel.add(JField, BorderLayout.CENTER);
content.add(fundsPanel, BorderLayout.NORTH);
JLabel accLabel = new JLabel("Amount in $: ");
JPanel accPanel = new JPanel(new BorderLayout());
JPanel accPanelTwo = new JPanel(new BorderLayout());
JPanel accPanelThree = new JPanel(new BorderLayout());
accLabel.setDisplayedMnemonic(KeyEvent.VK_ENTER);
JFormattedTextField accTextField = new JFormattedTextField(amount);
accTextField.setEnabled(false);
JLabel opLabel = new JLabel("Operations per second:");
opLabel.setDisplayedMnemonic(KeyEvent.VK_ENTER);
JTextField accTextFieldTwo = new JTextField(Double.toString(opsPerSec), 5);
accTextFieldTwo.setEnabled(false);
accLabel.setLabelFor(accTextField);
opLabel.setLabelFor(accTextFieldTwo);
accPanel.add(accLabel, BorderLayout.WEST);
accPanel.add(accTextField, BorderLayout.CENTER);
accPanelTwo.add(opLabel, BorderLayout.WEST);
accPanelTwo.add(accTextFieldTwo, BorderLayout.CENTER);
accPanelTwo.add(opsLabel, BorderLayout.EAST);
accPanelTwo.add(opsField, BorderLayout.NORTH);
accPanelThree.add(stateLabel, BorderLayout.WEST);
accPanelThree.add(stateField, BorderLayout.CENTER);
content.add(accPanel, BorderLayout.CENTER);
content.add(accPanelTwo, BorderLayout.EAST);
contentTwo.add(accPanelThree, BorderLayout.EAST);
JButton jButton1 = new JButton("Stop agent");
JButton jButton2 = new JButton("Dismiss");
jButton1.addActionListener(handler);
jButton2.addActionListener(handler);
buttonPanel.setLayout(new GridLayout(2, 3, 3, 3));
n.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
buttonPanel.add(jButton2, null);
buttonPanel.add(jButton1, null);
n.pack();