package testawt;import java.awt.*;
import java.awt.event.*;public class YaoHao {
Frame f1 = new Frame("摇号");
Panel p1 = new Panel();
Panel p2 = new Panel();
Button b1 = new Button("开始");
Button b2 = new Button("停止");
String[] name = { "璐璐", "七七", "小敏", "婷婷", "芳芳" };
Label[] label = new Label[name.length];
boolean truefalse = true;
CardLayout cardLayout1; public void run() {
cardLayout1 = new CardLayout();
f1.setLayout(new FlowLayout());
p1.setLayout(cardLayout1);
for (int i = 0; i < name.length; i++) {
label[i] = new Label(name[i]);
p1.add(name[i], label[i]);
} b1.addActionListener(new Action());
b2.addActionListener(new Action());
p2.add(b1);
p2.add(b2);
f1.add(p2);
f1.add(p1);
f1.addWindowListener(new WindowClose());
f1.setResizable(false);
f1.setSize(150, 100);
f1.setVisible(true);
while (truefalse) {
cardLayout1.previous(p1);
}
} class WindowClose extends WindowAdapter {
public void windowClosing(WindowEvent e) { System.exit(0);
}
} public class Action implements ActionListener { public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == b1) { truefalse = true; } else if (e.getSource() == b2) {
truefalse = false; }
} catch (Exception e2) {
e2.printStackTrace();
} }
} public static void main(String[] args) {
YaoHao yh = new YaoHao(); yh.run(); }
}
源代码如上,问题是按了“停止”后,再按“开始”就没反应了,也就是说只运行了一次,我不知道怎么改,请各位大虾帮帮忙了,谢谢!

解决方案 »

  1.   


    package test; import java.awt.*; 
    import java.awt.event.*; public class YaoHao { 
    Frame f1 = new Frame("摇号"); 
    Panel p1 = new Panel(); 
    Panel p2 = new Panel(); 
    Button b1 = new Button("开始"); 
    Button b2 = new Button("停止"); 
    String[] name = { "璐璐", "七七", "小敏", "婷婷", "芳芳" }; 
    Label[] label = new Label[name.length]; 
    boolean truefalse = true; 
    CardLayout cardLayout1; public void run() { 
    cardLayout1 = new CardLayout(); 
    f1.setLayout(new FlowLayout()); 
    p1.setLayout(cardLayout1); 
    for (int i = 0; i < name.length; i++) { 
    label[i] = new Label(name[i]); 
    p1.add(name[i], label[i]); 
    }  b1.addActionListener(new Action()); 
    b2.addActionListener(new Action()); 
    p2.add(b1); 
    p2.add(b2); 
    f1.add(p2); 
    f1.add(p1); 
    f1.addWindowListener(new WindowClose()); 
    f1.setResizable(false); 
    f1.setSize(150, 100); 
    f1.setVisible(true); 
    while(true){
    while (truefalse) { 
    cardLayout1.previous(p1); 

    }
    } class WindowClose extends WindowAdapter { 
    public void windowClosing(WindowEvent e) {  System.exit(0); 

    } public class Action implements ActionListener {  public void actionPerformed(ActionEvent e) { 
    try { 
    if (e.getSource() == b1) {  truefalse = true;  } else if (e.getSource() == b2) { 
    truefalse = false;  } 
    } catch (Exception e2) { 
    e2.printStackTrace(); 
    }  } 
    }  public static void main(String[] args) { 
    YaoHao yh = new YaoHao();  yh.run();  }