当前位置: 代码迷 >> Eclipse >> 请问一个题目
  详细解决方案

请问一个题目

热度:91   发布时间:2016-04-23 13:43:08.0
请教一个题目,
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);    }}
  相关解决方案