当前位置: 技术问答>java相关
java的JPanel问题
来源: 互联网 发布时间:2015-09-27
本文导语: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class app extends JFrame { jpanel j; jpanel1 j1; Button b,b1; public app() { super("Swing application"); Container contentPane=getContentPane(); j=new jpanel(); b=new Button(); ...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class app extends JFrame
{
jpanel j;
jpanel1 j1;
Button b,b1;
public app()
{
super("Swing application");
Container contentPane=getContentPane();
j=new jpanel();
b=new Button();
j.add(b);
contentPane.add(j);
j1=new jpanel1();
b1=new Button();
j1.add(b1);
contentPane.add(j1);
}
public static void main(String [] args)
{
JFrame f=new app();
f.setBounds(100,100,300,300);
f.setVisible(true);
}
}
class jpanel extends JPanel
{
jpanel()
{
setBackground(Color.red);
}
}
class jpanel1 extends JPanel
{
jpanel1()
{
setBackground(Color.blue);
}
}
为什么程序运行是blue背景
j是被j1覆盖了吗?
j,j1是怎么排列的
还有b,b1好象是都在j1 上
为什么?
import javax.swing.*;
import java.awt.event.*;
public class app extends JFrame
{
jpanel j;
jpanel1 j1;
Button b,b1;
public app()
{
super("Swing application");
Container contentPane=getContentPane();
j=new jpanel();
b=new Button();
j.add(b);
contentPane.add(j);
j1=new jpanel1();
b1=new Button();
j1.add(b1);
contentPane.add(j1);
}
public static void main(String [] args)
{
JFrame f=new app();
f.setBounds(100,100,300,300);
f.setVisible(true);
}
}
class jpanel extends JPanel
{
jpanel()
{
setBackground(Color.red);
}
}
class jpanel1 extends JPanel
{
jpanel1()
{
setBackground(Color.blue);
}
}
为什么程序运行是blue背景
j是被j1覆盖了吗?
j,j1是怎么排列的
还有b,b1好象是都在j1 上
为什么?
|
把contentPane.add(j1);换成contentPane.add(j1, BorderLayout.NORTH);看一下