Java 使用空布局将JPanel添加到JApplet
我想在我的JApplet中添加4个JPanel,并给每个JPanel赋予不同的颜色。但是没有显示任何颜色-我的意思是我看不到输出。没有颜色。以下代码位于Java 使用空布局将JPanel添加到JApplet,java,swing,Java,Swing,我想在我的JApplet中添加4个JPanel,并给每个JPanel赋予不同的颜色。但是没有显示任何颜色-我的意思是我看不到输出。没有颜色。以下代码位于init()方法中 this.setSize(1400, 780); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.se
init()方法中
this.setSize(1400, 780);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setLayout(null) ;
setLayout(null) ;
Panel1 = new JPanel() ;
Panel2 = new JPanel () ;
Panel3 = new JPanel() ;
Panel4 = new JPanel() ;
Label1 = new JLabel ("Label1") ;
Label2 = new JLabel ("Label2") ;
Label3 = new JLabel ("Label3") ;
Label4 = new JLabel ("Label4") ;
Panel1.add(Label1) ;
Panel2.add(Label2) ;
Panel3.add(Label3) ;
Panel4.add(Label4) ;
// Panel 1 "About Me"
Panel1.setSize(140,390) ;
Panel1.setLocation(0,0) ;
Panel1.setBackground(Color.red) ;
Panel1.setVisible(true) ;
this.add(Panel1) ;
// Panel 2 "MyHoppies"
Panel2.setSize(140,390) ;
Panel2.setLocation(0,700) ;
Panel2.setBackground(Color.yellow) ;
this.add(Panel2) ;
// Panel 3 "Photo Gallery"
Panel3.setSize(140,390) ;
Panel3.setLocation(390,0) ;
Panel3.setBackground(Color.black) ;
this.add(Panel3) ;
// Panel 4 "Happey face"
Panel4.setSize(140,390) ;
Panel4.setLocation(390,700) ;
Panel4.setBackground(Color.pink) ;
this.add(Panel4) ;
您应该这样做:
panelx.setOpaque(true)
而且它应该首先起作用:您需要JPanel.setOpaque(true)
面板才能看到背景色
第二:背景色属性在不同的平台上有不同的效果。例如:如果设置JButton的背景,您将在Win7中看到该按钮的颜色,但在WinXP中看不到(不确定它在其他操作系统下的颜色)。这至少是我的经验…
this.setVisible(true)
必须是GUI构造函数中的最后一行代码
- 正确使用,则
Panel1
应为Panel1
e.i
- 不要扩展
JFrame
或JApplet
,使用与Panel1
- 不要使用
NullLayout
,而是使用适当的LayoutManager
,在这种情况下,可能是GridLayout
,否则JFrame
内容无法使用JFrame
代码
也许是因为你一直把<代码>设置背景< /代码>(和<代码>设置位置<代码>)提交到PANEL1?@ ASSILIAS,考虑把它发布为answer@GuillaumePolet显然是一个打字错误。1)this.setSize(1400780)代码>不设置帧的大小,不扩展帧-只使用一个实例。2) this.setVisible(true)代码>在添加组件并调用pack()
之前不要这样做。3) this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)如果这是从小程序调用的,则code>将爆炸-即使是受信任的小程序也不应显式关闭VM 4)this.setLocationRelativeTo(null)代码>使用setLocationByPlaftform(true)
5)this.setLayout(null)代码>参见mKorbel的答案。6) setLayout(空)代码>是的,JRE在你第一次写这篇文章时就听到了。@brimboriumJFrame.DISPOSE\u ON\u CLOSE
是这个问题的简短答案。自定义操作完成后,调用frame.dispose()
,VM应该自动退出。如果无法退出,则说明小程序仍在加载,或者有另一个非守护进程线程正在运行。要了解更多细节,那么。。这是一个问答网站谢谢,我看到一些带有颜色的长方形,但它们并没有填充它应该填充的空间。我想把窗户分成四个面板,每个面板都有不同的颜色。我指定的窗口大小是(1400700),那么每个面板的大小应该是(400390)宽和高。请更新您的代码,这样我可以有更好的主意吗?@user1413188:如果您不需要其他面板,您可以创建一个JPanel/JLabel,覆盖公共组件(Graphics g)
方法在其上绘制您想要的任何内容。通常使用布局进行绘制不是一个好主意。除非您想用组件和东西填充JPanel。@brimborium,因为如果您不扩展类的功能,就不应该扩展它。“不要扩展JFrame
或JApplet
”同意frame,但试着在不扩展applet
或JApplet
;)的情况下制作一个小程序。@GuillaumePolet:你完全正确,谢谢。我还以为你是这个意思呢
import java.awt.*;
import javax.swing.*;
public class ColorongPanels {
private JFrame frame = new JFrame("ColorongPanels");
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JPanel panel3 = new JPanel();
private JPanel panel4 = new JPanel();
private JLabel label1 = new JLabel("Label1");
private JLabel label2 = new JLabel("Label2");
private JLabel label3 = new JLabel("Label3");
private JLabel label4 = new JLabel("Label4");
public ColorongPanels() {
frame.setLayout(new GridLayout(2, 2, 5, 5));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.setBackground(Color.red);
panel1.setLayout(new BorderLayout());
panel1.add(label1, BorderLayout.CENTER);
panel2.setBackground(Color.yellow);
panel2.setLayout(new BorderLayout());
panel2.add(label2, BorderLayout.CENTER);
panel3.setBackground(Color.black);
panel3.setLayout(new BorderLayout());
panel3.add(label3, BorderLayout.CENTER);
panel4.setBackground(Color.pink);
panel4.setLayout(new BorderLayout());
panel4.add(label4, BorderLayout.CENTER);
frame.add(panel1);
frame.add(panel2);
frame.add(panel3);
frame.add(panel4);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ColorongPanels();
}
});
}
}