JPanel不能显示在JFrame中..请问是怎么加事 import java.awt.*;
import java.awt.event.*;import javax.swing.*;
import javax.swing.border.*;
public class lcd extends JFrame{
JPanel p = new JPanel(); //存放工具设置的
private JButton b1 = new JButton("Setup");
private JButton b2 = new JButton("Hide");
public JLabel l1 = new JLabel("");
private Image icon;
//Thread t1 = new Thread(new t1(this));
//Thread t2 = new Thread(new t2(this));
public int x=0;
public int y=40;
Graphics gg;
Timer tme1;
Timer tme2;
public int speed = 10;
//设置程序屏幕的大小
public int screenw = 400;
public int screenh = 300;
public lcd(){
icon=Toolkit.getDefaultToolkit().getImage("1.gif");
Container con = this.getContentPane(); con.setLayout(new BorderLayout());
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
b1_button(e);
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
b2_button(e);
}
}); tme1 = new Timer(speed,new ActionListener(){
public void actionPerformed(ActionEvent e){
tme1_button(e);
}
});
tme2 = new Timer(speed,new ActionListener(){
public void actionPerformed(ActionEvent e){
tme2_button(e);
}
});
tme1.start();
p.setBackground(Color.BLACK);
b1.setBounds(340, 0, 60, 40);
b1.setBackground(Color.BLACK);
b1.setForeground(Color.WHITE);
b1.setBorder(null);
b2.setBounds(280, 0, 60, 40);
b2.setBackground(Color.BLACK);
b2.setForeground(Color.WHITE);
b2.setBorder(null);
l1.setBounds(200, 0, 90, 60);
l1.setBackground(Color.BLACK);
l1.setForeground(Color.WHITE);
l1.setBorder(null);
p.setLayout(null);
p.setSize(1500,1500);
p.add(b1);
p.add(b2);
// con.add(p,BorderLayout.SOUTH);
con.add(p,BorderLayout.SOUTH);
// con.setBackground(Color.WHITE);
this.setSize(screenw,screenh);
int windowWidth = this.getWidth();
int windowHeight = this.getHeight();
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
this.setLocation(screenWidth/2-windowWidth/2, screenHeight/2-windowHeight/2);
this.setTitle("LCD");
// this.setResizable(false);
this.setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.RED);
g.drawString("Weloce to lcd....请先进行设置", x,y);
}
public void tme1_button(ActionEvent e){
this.x=this.x+1;
this.l1.setText("Postion:"+x);
this.repaint();
if(x>230){
tme1.stop();
tme2.start();
}
}
public void tme2_button(ActionEvent e){
x=x-1;
l1.setText("Posttion"+x);
this.repaint();
if(x==0){
tme2.stop();
tme1.start();
}
}
public void b1_button(ActionEvent e){
//lcdcomset lc = new lcdcomset();
//lc.setVisible(true);
}
public void b2_button(ActionEvent e){
tme1.start();
}
public static void main(String args[]){
lcd lc= new lcd();
}
}
import java.awt.event.*;import javax.swing.*;
import javax.swing.border.*;
public class lcd extends JFrame{
JPanel p = new JPanel(); //存放工具设置的
private JButton b1 = new JButton("Setup");
private JButton b2 = new JButton("Hide");
public JLabel l1 = new JLabel("");
private Image icon;
//Thread t1 = new Thread(new t1(this));
//Thread t2 = new Thread(new t2(this));
public int x=0;
public int y=40;
Graphics gg;
Timer tme1;
Timer tme2;
public int speed = 10;
//设置程序屏幕的大小
public int screenw = 400;
public int screenh = 300;
public lcd(){
icon=Toolkit.getDefaultToolkit().getImage("1.gif");
Container con = this.getContentPane(); con.setLayout(new BorderLayout());
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
b1_button(e);
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
b2_button(e);
}
}); tme1 = new Timer(speed,new ActionListener(){
public void actionPerformed(ActionEvent e){
tme1_button(e);
}
});
tme2 = new Timer(speed,new ActionListener(){
public void actionPerformed(ActionEvent e){
tme2_button(e);
}
});
tme1.start();
p.setBackground(Color.BLACK);
b1.setBounds(340, 0, 60, 40);
b1.setBackground(Color.BLACK);
b1.setForeground(Color.WHITE);
b1.setBorder(null);
b2.setBounds(280, 0, 60, 40);
b2.setBackground(Color.BLACK);
b2.setForeground(Color.WHITE);
b2.setBorder(null);
l1.setBounds(200, 0, 90, 60);
l1.setBackground(Color.BLACK);
l1.setForeground(Color.WHITE);
l1.setBorder(null);
p.setLayout(null);
p.setSize(1500,1500);
p.add(b1);
p.add(b2);
// con.add(p,BorderLayout.SOUTH);
con.add(p,BorderLayout.SOUTH);
// con.setBackground(Color.WHITE);
this.setSize(screenw,screenh);
int windowWidth = this.getWidth();
int windowHeight = this.getHeight();
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
this.setLocation(screenWidth/2-windowWidth/2, screenHeight/2-windowHeight/2);
this.setTitle("LCD");
// this.setResizable(false);
this.setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.RED);
g.drawString("Weloce to lcd....请先进行设置", x,y);
}
public void tme1_button(ActionEvent e){
this.x=this.x+1;
this.l1.setText("Postion:"+x);
this.repaint();
if(x>230){
tme1.stop();
tme2.start();
}
}
public void tme2_button(ActionEvent e){
x=x-1;
l1.setText("Posttion"+x);
this.repaint();
if(x==0){
tme2.stop();
tme1.start();
}
}
public void b1_button(ActionEvent e){
//lcdcomset lc = new lcdcomset();
//lc.setVisible(true);
}
public void b2_button(ActionEvent e){
tme1.start();
}
public static void main(String args[]){
lcd lc= new lcd();
}
}
另外我记得我在写JFrame的时候,第一句要super("...")这句 你看看我的这个程序:http://blog.csdn.net/justinavril/archive/2008/09/13/2922604.aspx