Java 里JButton问题?

// The "MainGameScreen" class.
import java.awt.*;
import java.applet.*;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.io.*;

public class Test extends JFrame implements ActionListener
{
public Test ()
{
Frame1 ();
}

JButton a = new JButton ("Button");
JFrame F1;
public void Frame1 ()
{
F1 = new JFrame ("F1");
Container c = F1.getContentPane ();
c.add (a);
a.addActionListener (this);
F1.setVisible (true);
F1.pack ();
}

JButton b = new JButton ("Button");
JFrame F2;
JButton Q = new JButton ("Quit");
public void Frame2 ()
{
F2 = new JFrame ("F2");
Container c = F2.getContentPane ();
c.setLayout (new FlowLayout ());
c.add (b);
c.add (Q);
Q.addActionListener (this);
b.addActionListener (this);
F2.setVisible (true);
F2.pack ();
}

public void actionPerformed (ActionEvent e)
{
if (e.getSource () == a)
{
Frame2 ();
}
if (e.getSource () == Q)
{
F2.dispose ();
}
if (e.getSource () == b)
{
System.out.println ("Catch");
}
}

public static void main (String[] args)
{
Test a = new Test ();
} // main method
} // Test class

按出第二个窗口,
按button,每次按输出一次catch
关掉第二个窗口,再开
按button,每次按输出两次catch
..第三次按出再按button就输出三次catch..
哪位高手帮我看看,什么问题?怎么解决?

第1个回答  2010-06-02
//The "MainGameScreen" class.
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Test implements ActionListener {
private JButton a;
private JFrame f1;
private JButton b;
private JFrame f2;
private JButton q;

public Test() {
a = new JButton("Button");
b = new JButton("Button");
q = new JButton("Quit");
f1 = new JFrame("F1");
Container c = f1.getContentPane();
c.add(a);
a.addActionListener(this);
f1.setVisible(true);
f1.pack();
f2 = new JFrame("F2");
Container c1 = f2.getContentPane();
c1.setLayout(new FlowLayout());
c1.add(b);
c1.add(q);
q.addActionListener(this);
b.addActionListener(this);
f2.pack();
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == a) {
f2.setVisible(true);
}
if (e.getSource() == q) {
f2.setVisible(false);
}
if (e.getSource() == b) {
System.out.println("Catch");
}
}

public static void main(String[] args) {
Test a = new Test();
} // main method
} // Test class

哥,你知道吗?我没次改你的代码,我日蛮蛋疼!本回答被提问者采纳
第2个回答  2010-06-02
import java.awt.*;
import java.applet.*;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.io.*;

public class Test extends JFrame implements ActionListener
{
public static int i = 0 ;
public Test ()
{
Frame1 ();
}

JButton a = new JButton ("Button");
JFrame F1;
public void Frame1 ()
{
F1 = new JFrame ("F1");
Container c = F1.getContentPane ();
c.add (a);
a.addActionListener (this);
F1.setVisible (true);
F1.pack ();
}

JButton b = new JButton ("Button");
JFrame F2;
JButton Q = new JButton ("Quit");
public void Frame2 ()
{
F2 = new JFrame ("F2");
Container c = F2.getContentPane ();
c.setLayout (new FlowLayout ());
c.add (b);
c.add (Q);
Q.addActionListener (this);
b.addActionListener (this);
F2.setVisible (true);
F2.pack ();
}

public void actionPerformed (ActionEvent e)
{
if (e.getSource () == a)
{
Frame2 ();
}
if (e.getSource () == Q)
{
F2.dispose();

}
if (e.getSource () == b)
i++;
{
for(int j=0;j<i;j++){
System.out.println ("Catch");
}
}
}

public static void main (String[] args)
{
Test a = new Test ();
} // main method
} // Test class
我改好的 你运行看看

相关了解……

你可能感兴趣的内容

大家正在搜

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网