gpt4 book ai didi

java - 将组件添加到 JFrame 内的 JPanel

转载 作者:行者123 更新时间:2023-12-03 21:45:14 25 4
gpt4 key购买 nike

由于我是初学者并且我不想参与布局管理器,所以我只是将 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: 标签在中间,旁边是按钮。我在这里错过了什么?

最佳答案

同样,不要使用空布局,因为它会使更新和维护您的 GUI 比应有的困难得多,并且如果您计划让它们在多个平台上运行,可能会导致丑陋的 GUI。相反

  • 使用多个 JPanel,每个 JPanel 包含一组核心组件,每个都使用其最佳布局管理器
  • 将这些 JPanel 嵌套在使用最佳布局管理器显示它们的其他 JPanel 中
  • 这将使您的 GUI 无需额外代码即可调整大小。
  • 将您的 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);
}
}

这将导致一个简单(无功能)的 GUI,如下所示:
enter image description here

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

  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

就是这样,这将显示:
enter image description here

关于java - 将组件添加到 JFrame 内的 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13907202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com