将组件添加到JFrame内的JPanel中 [英] Adding components into JPanel inside a JFrame

查看:113
本文介绍了将组件添加到JFrame内的JPanel中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Since im a beginner and i don't want to get involved with the layout managers, i was simply adding a JPanel into my main JFrame and giving spesific location to each component in the panel. But somehow the output appears way too wrong..

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:标签位于中间和下一个在它右边的按钮..我在这里缺少什么?

The outcome of this frame is that text areas are invisible, a You: label in the middle and next to the right of it the button.. What am i missing here?

推荐答案

再次,不要使用null布局,因为它使更新和维护GUI比应该更加困难,如果你计划在多个平台上运行它们,可能会导致丑陋的GUI。相反

Again, don't use null layout since it makes updating and maintaining your GUI much more difficult than it should be, and can lead to ugly GUI's if you plan on having them run on multiple platforms. Instead


  • 使用多个JPanel,每个JPanel包含一组核心组件,每个组件都使用其最佳布局管理器

  • 将这些JPanel嵌套在使用最佳布局管理器的其他JPanel中显示它们

  • ,这将允许您的GUI可以调整大小而无需额外的代码。

  • 将您的JTextAreas放在JScrollPanes中,这样即使文本区域超出文本区域,您也可以看到所有文本。

  • 永远不要设置JTextArea的大小,因为它不允许它滚动。而是设置它的列和行。

  • Use several JPanels, each one holding a core group of components and each using its best layout manager
  • Nest these JPanels in other JPanels that use the best layout manager to display them
  • and that will allow your GUI to be resizeable without need of extra code.
  • Put your JTextAreas in JScrollPanes so that you can see all text even if it goes beyond the text area.
  • Never set the size of the JTextArea as that will not allow it to scroll. Instead set its columns and rows.

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

As a very simple example, run this to see what I mean:

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看起来像这样:

This would result in a simple (non-functioning) GUI that looked like this:

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

Now say you want to change this and add another button, an "exit" JButton to the right of the send JButton. If you used null layout, you'd have to resize your GUI, you'd have to move the send button over to the left and make sure that your math was without error, etc. If you used layout managers, you'd need just two new lines of code (to change the display, not the functionality of course):

  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

就是这样,这会显示:

That's it, and this would display:

这篇关于将组件添加到JFrame内的JPanel中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆