我尝试使用这段代码来显示一个有标签的窗口,但它只显示一个空白窗口。
JFrame window = new JFrame("My Window");
window.setVisible(true);
window.setResizable(true);
window.setSize(680,420);
window.setContentPane(new Container());
window.getContentPane().setLayout(new FlowLayout());
JPanel panel = new JPanel();
JLabel label = new JLabel("LABEL");
label.setBackground(Color.BLACK);
panel.add(label);
window.add(panel);
尝试呼叫setVisible
last
JFrame window = new JFrame("My Window");
//window.setVisible(true);
//window.setResizable(true);
//window.setSize(680,420);
//window.setContentPane(new Container());
window.setLayout(new FlowLayout());
JPanel panel = new JPanel();
JLabel label = new JLabel("LABEL");
label.setBackground(Color.BLACK);
panel.add(label);
window.add(panel);
window.setResizable(true);
// Pack will size the window to fit the content,
// tacking into account the preferred size of the
// content...
window.pack();
window.setVisible(true);
还要注意,JLabel
默认是透明的,所以设置它的背景颜色将没有效果,除非你改变它的opaque
属性为true
将所有component
, set
加到frame
的visibility
。
JFrame window = new JFrame("My Window");
window.setResizable(true);
window.setSize(680,420);
window.setContentPane(new Container());
window.getContentPane().setLayout(new FlowLayout());
JPanel panel = new JPanel();
JLabel label = new JLabel("LABEL");
label.setBackground(Color.BLACK);
panel.add(label);
window.add(panel);
window.setVisible(true);
(当你在开始时调用setvisible()方法时)..您还可以使用update()方法,以便更新或重新绘制jframe上的组件。