首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

请问一个题目

2012-03-18 
请教一个题目,import javax.swing.*import java.awt.event.*import java.awt.*import java.applet.*im

请教一个题目,
import javax.swing.*;import java.awt.event.*;import java.awt.*;
import java.applet.*;import java.awt.CardLayout;
public class Sb_5_7 extends Applet implements ActionListener{
//卡布局
  JLabel l1=new JLabel("one");
  JLabel l2=new JLabel("two");
  JButton b1=new JButton("向前");
  JButton b2=new JButton("向后");
  CardLayout card=new CardLayout();
  BorderLayout bord=new BorderLayout();
  JFrame f;
  JPanel p1;JPanel p2;
  public void init(){
f=new JFrame();
Container con=f.getContentPane();
con.setLayout(bord);
setSize(400,150);
setVisible(true);
 
JPanel p1=new JPanel();
p1.setLayout(card);
p1.add(l1);p1.add(l2);
 
JPanel p2=new JPanel();
b1.addActionListener(this);b2.addActionListener(this);
p2.add(b1);p2.add(b2);
 
con.add(p1,"Center");con.add(p2,"South");
 
  }
  public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("向前")) card.last(p1);
else card.next(p1);
 
  }

}

请问下大家,我这个程序可以运行,但为什么出现一个白板,我定义的东西,怎么都没显示,哪里错了,麻烦帮我改下,谢谢了


[解决办法]

Java code
import javax.swing.*;import java.awt.event.*;import java.awt.*;import java.applet.*;import java.awt.CardLayout;public class Sb_5_7 extends Applet implements ActionListener {    // 卡布局    JLabel l1 = new JLabel("one");    JLabel l2 = new JLabel("two");    JButton b1 = new JButton("向前");    JButton b2 = new JButton("向后");    CardLayout card = new CardLayout(5,5);    BorderLayout bord = new BorderLayout();    JFrame f;    JPanel p1;    JPanel p2;    public void init() {        f = new JFrame();        Container con = f.getContentPane();        con.setLayout(bord);        setSize(400, 150);        setVisible(true);        p1 = new JPanel();        p1.setLayout(card);        p1.add(l1,"a");        p1.add(l2,"b");        p2 = new JPanel();        b1.addActionListener(this);        b2.addActionListener(this);        p2.add(b1);        p2.add(b2);        con.add(p1, "Center");        con.add(p2, "South");        f.pack();        f.setVisible(true);    }    public void actionPerformed(ActionEvent e) {        if (e.getActionCommand().equals("向前"))            card.last(p1);        else            card.next(p1);    }} 

热点排行