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..
哪位高手帮我看看,什么问题?怎么解决? 展开
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..
哪位高手帮我看看,什么问题?怎么解决? 展开
2个回答
展开全部
//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
哥,你知道吗?我没次改你的代码,我日蛮蛋疼!
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
哥,你知道吗?我没次改你的代码,我日蛮蛋疼!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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
我改好的 你运行看看
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
我改好的 你运行看看
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询