[英]JPanel not showing when added to JFrame
我正在用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中)。
我认为您使用null来获得“将其放置在适当的位置”吗? 然后使用FlowLayout
frame.setLayout(new FlowLayout());
那应该解决它:)
有人可以建议这是为什么吗?
使用null
布局,而不调用pack()
。
在我注释掉对setUndecorated(true)
的调用并将其拖动到更大一点之后,获得了我在问题中编辑过的图像作为GUI的屏幕截图。 这样做会使JRE验证组件结构( pack()
会做什么),从而使组件出现。
正如我在评论中提到的:
..一个更好的问题是“如何布局此GUI?” (只要您提供尝试)
这就是我的第一个评论。 (现在较长)
Java GUI可能必须在多种平台上,不同的屏幕分辨率和使用不同的PLAF上运行。 因此,它们不利于组件的精确放置。 要为健壮的GUI组织组件,请改用布局管理器或它们的组合 1 ,以及空白 2的布局填充和边框。
所以回到:
(只要您提供尝试)
查看这两个示例以了解它们的工作原理,然后尝试结合一些布局和填充以创建一个框架,然后将其打包以减小为自然尺寸。
并提示JTextArea
。 建议在列x行中结合Font
大小的大小。
只需从上面的代码第46行删除/注释该行。
// frame.getContentPane().setLayout(null);
它应该工作正常。
更换
frame.getContentPane().setLayout(null);
与
frame.getContentPane().setLayout(new BorderLayout());
祝好运。
编辑:为了将来参考,要确定在您的情况下应该使用哪个LayoutManager
,您应该参考此LayoutManagers可视指南 。
1:永远不要调用setLayout(null)。 2:尝试使用frame.validate()使用布局来布局组件。
也许您应该替换:
frame.getContentPane().add(panel);
通过
frame.setContentPane(panel);
希望能有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.