将组件添加到JFrame内的JPanel中

由于我是初学者而且我不想参与布局管理器,我只是将JPanel添加到我的主JFrame中,并为面板中的每个组件提供特定位置。 但不知何故输出似乎太错了..

frame = new JFrame(email + " (Offline)"); frame.setSize(400, 400); frame.setLocation(0, 0); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.setLayout(new FlowLayout()); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { // out.println("BYE"); // out.flush(); frame.dispose(); thread.stop(); } }); panel = new JPanel(); frame.add(panel); chat = new JTextArea(); chat.setSize(400, 200); chat.setLocation(0, 0); chat.setEditable(false); panel.add(chat); panel.validate(); JLabel you = new JLabel("You:"); you.setSize(you.getPreferredSize()); you.setLocation(0, 210); panel.add(you); panel.validate(); input = new JTextArea(); input.setSize(200, 200); input.setLocation(0, 220 + chat.getSize().height); panel.add(input); panel.validate(); send = new JButton("Send"); send.setSize(send.getPreferredSize()); send.setLocation(210, 220 + chat.getSize().height); panel.add(send); panel.validate(); frame.setVisible(true); 

这个框架的结果是文本区域是不可见的,You:标签位于中间,右侧是按钮..我在这里缺少什么?

同样,不要使用null布局,因为它使得更新和维护GUI比应该更加困难,并且如果您打算让它们在多个平台上运行,可能会导致丑陋的GUI。 代替

  • 使用几个JPanel,每个JPanel包含一组核心组件,每个组件都使用其最佳布局管理器
  • 将这些JPanel嵌套在使用最佳布局管理器的其他JPanel中以显示它们
  • 这将允许您的GUI可以resize,而无需额外的代码。
  • 将您的JTextAreas放在JScrollPanes中,这样即使文本超出文本区域,您也可以看到所有文本。
  • 永远不要设置JTextArea的大小,因为它不允许滚动。 而是设置其列和行。

作为一个非常简单的例子,运行它来看看我的意思:

 import java.awt.*; import javax.swing.*; public class FooSwing2 { public static void main(String[] args) { JTextArea chatArea = new JTextArea(8, 40); chatArea.setEditable(false); chatArea.setFocusable(false); JScrollPane chatScroll = new JScrollPane(chatArea); JPanel chatPanel = new JPanel(new BorderLayout()); chatPanel.add(new JLabel("Chat:", SwingConstants.LEFT), BorderLayout.PAGE_START); chatPanel.add(chatScroll); JTextField inputField = new JTextField(40); JButton sendBtn = new JButton("Send"); JPanel inputPanel = new JPanel(); inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.LINE_AXIS)); inputPanel.add(inputField); inputPanel.add(sendBtn); JPanel youLabelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0)); youLabelPanel.add(new JLabel("You:")); JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS)); mainPanel.add(chatPanel); mainPanel.add(Box.createVerticalStrut(10)); mainPanel.add(youLabelPanel); mainPanel.add(inputPanel); JFrame frame = new JFrame("Foo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(mainPanel); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } } 

这将导致一个简单(无function)的GUI,如下所示:
在此处输入图像描述

现在说你要改变它并添加另一个按钮,一个“退出”JButton在发送JButton的右边。 如果您使用了null布局,则必须调整GUI的大小,您必须将发送按钮移到左侧并确保数学没有错误等。如果您使用布局管理器,则需要只需两行代码(更改显示,而不是function):

  JTextField inputField = new JTextField(40); JButton sendBtn = new JButton("Send"); JButton exitBtn = new JButton("Exit"); // ***** added JPanel inputPanel = new JPanel(); inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.LINE_AXIS)); inputPanel.add(inputField); inputPanel.add(sendBtn); inputPanel.add(exitBtn); // ***** added 

就是这样,这会显示:
在此处输入图像描述