java - 添加到 JFrame 时 JPanel 不显示

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

我正在用 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 中)。

Memos GUI

最佳答案

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

frame.setLayout(new FlowLayout());

这应该可以解决它:)

关于java - 添加到 JFrame 时 JPanel 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22859617/

相关文章:

java - JTable 未显示在 JPanel 中

java - 变量不能传递到for循环中

java - 找不到 http ://www. objectdb.com/eclipse

java - 给出意外匹配的正则表达式

java - 需要有关 Spring Boot API 微服务错误处理的建议或更好的设计

java - JOptionPane 出现问题并使其正常工作

java - 方法 PaintComponent(Graphics g) 不起作用

java - JPanel 中的 JLabels,在运行时新标签包含以前的标签

java - 可视化数组列表

java - 重绘方法后无法在 JPanel 上绘图