Java 里JButton问题?

//The"MainGameScreen"class.importjava.awt.*;importjava.applet.*;importjava.awt.FlowLa... // 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..
哪位高手帮我看看,什么问题?怎么解决?
展开
 我来答
百度网友b5880be
2010-06-02 · TA获得超过689个赞
知道小有建树答主
回答量:164
采纳率:0%
帮助的人:193万
展开全部
//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

哥,你知道吗?我没次改你的代码,我日蛮蛋疼!
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
和谐射惠
2010-06-02 · TA获得超过211个赞
知道小有建树答主
回答量:234
采纳率:0%
帮助的人:0
展开全部
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
我改好的 你运行看看
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式