import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
public class test {
public static void main(String[]ar)
{
show1 ah=new show1();
ah.setSize(300,300);
ah.setVisible(true);
ah.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class show1 extends JFrame implements ActionListener
{
Container A;
JPanel p1;
JTextField JF1,JF2;
JButton JB,start;
show1()
{
A=getContentPane();
JF1=new JTextField(5);
JF2=new JTextField(5);
JB=new JButton("OK");
JB.addActionListener(this);
start=new JButton("start");
start.addActionListener(this);
p1=new JPanel();
p1.add(JF1);p1.add(JF2);
p1.add(JB);p1.add(start);
A.add(p1);
}
public void actionPerformed(ActionEvent e)
{
JButton source=(JButton)e.getSource();
if(source!=start)
{
if(JF1.getText().equals("1")&&JF2.getText().equals("2"))
{
if(source==JB)
{
JOptionPane.showMessageDialog(show1.this,"JB");
JB.removeActionListener(this);
}
}
else
{
if(!JF1.getText().equals("1")&&JF2.getText().equals("2"))
JOptionPane.showMessageDialog(show1.this,"JB2");
else
JOptionPane.showMessageDialog(show1.this,"JB1");
} System.out.println("test");
}
else
{
if(source==start)
{
JB.addActionListener(this);
}
}
}
}
我想问的是:第一次当点OK按钮时,会弹出一个提示面板,这个你看那个if语句也知道,但当第一个if通过时,就会去掉它的事件,然后点start按钮为OK添加事件,这时再按OK,就会弹出至少二个以上的提示面板,这就是我不知道为什么的,明明看那个if判断一次只能弹出一个提示面板呀,怎么会连续弹出呢,高手们看看吧。这只是做个测式,代码的有作性不用管哈。