【问题标题】:JPanel not showing when added to JFrameJPanel 添加到 JFrame 时不显示
【发布时间】:2014-04-04 10:02:12
【问题描述】:

我正在用 java 创建一个 GUI。目前我有一个空的 JFrame 并正在尝试向它添加一个 JPanel。 JPanel 包含按钮、文本等。但是这些都没有显示。我的代码如下:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class memoDisplayUI { 


JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextArea jTextBox = new JTextArea();
JScrollPane scroll = new JScrollPane();

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                memoDisplayUI frame = new memoDisplayUI();
                frame.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public memoDisplayUI() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    
    frame.getContentPane().setBackground(new Color(255, 255, 255));
    frame.getContentPane().setLayout(null);
    frame.setBounds(100, 100, 270, 400);
    frame.setUndecorated(true); //REMOVES MENU BAR
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JLabel lblMemos = new JLabel("MEMOS");
    lblMemos.setForeground(new Color(100, 149, 237));
    lblMemos.setFont(new Font("Moire", Font.BOLD, 30));
    lblMemos.setBounds(16, 16, 234, 37);
    panel.add(lblMemos);
    
    JButton button = new JButton("");
    button.setBackground(new Color(100, 149, 237));
    button.setBounds(7, 350, 40, 40);
    panel.add(button);
    button.setIcon(new ImageIcon("back.png"));
    
    JButton button_1 = new JButton("");
    button_1.setBackground(new Color(100, 149, 237));
    button_1.setBounds(113, 350, 40, 40);
    panel.add(button_1);
    button_1.setIcon(new ImageIcon("Edit.png"));
    
    JButton button_2 = new JButton("");
    button_2.setBackground(new Color(100, 149, 237));
    button_2.setBounds(220, 350, 40, 40);
    panel.add(button_2);
    button_2.setIcon(new ImageIcon("memo.png"));
    
    JButton btnExit = new JButton("");
    btnExit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            System.exit(0);
        }
    });
    btnExit.setBorder(null);
    btnExit.setIcon(new ImageIcon("Exit.jpg"));
    btnExit.setBounds(216, 19, 40, 40);
    panel.add(btnExit);
    
    jTextBox = new JTextArea();
    scroll.setViewportView(jTextBox); // add scroll panel
    jTextBox.setTabSize(4);
    jTextBox.setLineWrap(true);
    jTextBox.setBackground(new Color(192, 192, 192));
    jTextBox.setBounds(8, 60, 255, 286);
    panel.add(jTextBox);
    
    frame.getContentPane().add(panel);
}
}

有人能告诉我这是为什么吗?

非常感谢:)

编辑

通过对代码的一些调整,这似乎是所需的布局(在不可调整大小的 GUI 中)。

【问题讨论】:

  • 提示:您的组件是添加到框架还是面板?你的框架是怎么回事,面板应该是怎么回事?
  • 话虽如此,您应该真正考虑使用LayoutManager 并避免空布局。让布局为您调整大小和放置;)
  • 我认为您使用 null 来获得“将其放置在合适的位置”?然后使用 FlowLayout :)
  • Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为强大的 GUI 组织组件,请改用布局管理器或 combinations of them,以及 white space 的布局填充和边框。
  • 顺便说一句 - 我在问题中编辑了一张图片。这是您要实现的基本布局吗?如果是这样,一个更好的问题是“如何布局这个 GUI?” (只要您提供尝试)。

标签: java swing jpanel layout-manager null-layout-manager


【解决方案1】:

我认为您使用 null 来获得“将其放置在合适的位置”?然后使用 FlowLayout

frame.setLayout(new FlowLayout());

这应该可以解决它:)

【讨论】:

    【解决方案2】:

    有人能告诉我这是为什么吗?

    使用null 布局而不调用pack()

    我在问题中编辑的图像是在我注释掉对setUndecorated(true) 的调用并将其拖得更大后获得的 GUI 屏幕截图。这样做会导致 JRE 验证组件结构(pack() 会做什么),从而使组件出现。


    正如我在评论中提到的:

    ..更好的问题是 “如何布局这个 GUI?”(只要您提供尝试)

    这就引出了我的第一条评论。 (现在是更长的形式)

    Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为强大的 GUI 组织组件,请改用布局管理器或 combinations of them1,以及 white space2 的布局填充和边框。




    所以回到:

    (只要您提供尝试)

    查看这两个示例以了解它们是如何工作的,然后尝试结合一些布局和填充来创建一个框架,然后可以将其打包以减小到自然大小。

    还有一个小费JTextArea。建议以 x 行为单位的大小与 Font 大小相结合。

    【讨论】:

    • 非常感谢您提供全面的答案,但我仍在努力解决如何实现这一点。谢谢:)
    • 如果这是您的问题..问一个问题。这个问题,具体来说,“有人可以告知这是为什么吗?”已回答 IMO。
    【解决方案3】:

    1: 你不应该调用 setLayout(null)。 2: 尝试 frame.validate() 用你的布局来布局组件。

    【讨论】:

      【解决方案4】:

      替换

      frame.getContentPane().setLayout(null);
      

      frame.getContentPane().setLayout(new BorderLayout());
      

      祝你好运。

      编辑:为了将来参考,要决定在你的情况下应该使用哪个LayoutManager,你应该参考这个Visual Guide to LayoutManagers

      【讨论】:

      • 如果他决定使用它,我添加了一些他可能需要的代码。
      • 谢谢,但是面板中的所有项目都没有对齐
      • 在这种情况下,您也应该使用panel.setLayout(new FlowLayout());
      【解决方案5】:

      只需从上面第 46 行的代码中删除/注释这一行。

      // frame.getContentPane().setLayout(null);
      

      它应该可以正常工作..

      【讨论】:

        【解决方案6】:

        也许你应该替换:

        frame.getContentPane().add(panel);
        

        frame.setContentPane(panel);
        

        希望对你有帮助

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-01-29
          • 1970-01-01
          • 2013-08-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多