打开后窗口是这样
点击最大公约数后,新打开一个窗口如图
**输入第一个和第二个整数,希望点击计算按钮后答案出现在文本框中,但是没有出现,急需找出错误!
**
我有三个类,分别是Police(实现接口implements ActionListener)、Test(包含main方法)和MyWin(创建窗口)
代码如下,希望大家帮我看看哪里出了问题**(Police类中actionPerformed方法出错概率较大)**:
1.Test类(.java文件)
import java.awt.*;
import javax.swing.*;
public class Test{
public static void main(String[] args){
MyWin mywin=new MyWin("计算页面");
}
}
2.Police类(.java文件)
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Police implements ActionListener{ //构造创建监视器对象的类,java要求该种类实现相关接口ActionListener
private JTextField txt,txt2;
private JButton btn,btn1,btn2,btn3,btn4,btn5;
private JTextArea txta;
int m,n;
String s,s1="chushi",s2;
public Police(JTextField a,JTextField b,JTextArea n,JButton c,JButton d,JButton e,JButton f,JButton g,JButton h){
txt=a;
txt2=b;
txta=n;
btn=c;
btn1=d;
btn2=e;
btn3=f;
btn4=g;
btn5=h;
}
public Police(JTextArea a,JButton c,JButton d,JButton e,JButton f,JButton g,JButton h){
txta=a;
btn=c;
btn1=d;
btn2=e;
btn3=f;
btn4=g;
btn5=h;
}
public Police(JButton d,JButton e,JButton f,JButton g,JButton h){
btn1=d;
btn2=e;
btn3=f;
btn4=g;
btn5=h;
}
public void actionPerformed(ActionEvent e){ //发生事件后的事件处理函数,接口内的抽象方法,需要被重写。
s=e.getActionCommand(); /*通过System.out.println(e.getSource().toString());
可以以字符串形式输出发生Actionevent事件的事件源对象的引用(此处.toString()不能少)*/
if(e.getSource()!=btn){
s1=s;
new MyWin(s1);
}
else{
if(e.getSource()==btn1){
System.out.println(s1);
m=Integer.parseInt(txt.getText());
n=Integer.parseInt(txt2.getText());
s2=m+n+"";
txta.setText(s2);
}
else if(e.getSource()==btn2){
m=Integer.parseInt(txt.getText());
n=Integer.parseInt(txt2.getText());
s2=m+n+"";
txta.setText(s2);
}
else if(e.getSource()==btn3){
txta.setText("153,370,371,407");
}
else if(e.getSource()==btn4){
txta.setText("6,28,496");
}
else if(e.getSource()==btn5){
txta.setText("{101,103},{107,109},{137,139},{149,151},{179,181},{191,193},{197,199}{227,229},{239,241},{269,271},{281,283},{311,313},{347,349},{419,421},{431,433}{461,463},{521,523},{569,571},{599,601},{617,619},{641,643},{659,661},{809,811},{821,823},{827,829},{857,859},{881,883}");
}
}
}
}
3.MyWin类(.java文件)
import java.awt.*;
import javax.swing.*;
public class MyWin extends JFrame{
private JTextField txt,txt2;
private JLabel label,label2,label3;
private JButton btn,btn1,btn2,btn3,btn4,btn5;
private JTextArea txta;
private Police police;
JPanel jp;
String s;
public MyWin(String s){
this.s=s;
label=new JLabel("第一个整数");
label2=new JLabel("第二个整数");
label3=new JLabel("计算结果");
txt=new JTextField();
txt2=new JTextField();
txta=new JTextArea();
btn=new JButton("计算");
btn1=new JButton("求最大公约数");
btn2=new JButton("求最小公倍数");
btn3=new JButton("求水仙花数");
btn4=new JButton("求1000以内完数");
btn5=new JButton("求所有三位孪生素数");
jp=new JPanel();
txt.setPreferredSize( new Dimension(300,30));
txt2.setPreferredSize( new Dimension(300,30));
btn.setPreferredSize( new Dimension(300,30));
txta.setPreferredSize( new Dimension(290,60));
label3.setPreferredSize( new Dimension(70,30));
jp.setPreferredSize( new Dimension(400,90));
setLayout(new FlowLayout());
if((s=="最大公约数")||(s=="最小公倍数")){
init1();
}
else if(s=="计算页面"){
init2();
}else{
init();
}
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public void init(){
this.setBounds(500,300,400,320);
this.setTitle(s);
add(label3);
add(txta);
add(btn);
jp.add(btn1);
jp.add(btn2);
jp.add(btn3);
jp.add(btn4);
jp.add(btn5);
add(jp);
btn.setActionCommand("计算");
btn1.setActionCommand("最大公约数");
btn2.setActionCommand("最小公倍数");
btn3.setActionCommand("水仙花数");
btn4.setActionCommand("完数");
btn5.setActionCommand("三位孪生素数");
police=new Police(txta,btn,btn1,btn2,btn3,btn4,btn5);
btn.addActionListener(police);
btn1.addActionListener(police);
btn2.addActionListener(police);
btn3.addActionListener(police);
btn4.addActionListener(police);
btn5.addActionListener(police);
}
public void init1(){
this.setBounds(500,300,400,320);
this.setTitle(s);
add(label);
add(txt);
add(label2);
add(txt2);
add(label3);
add(txta);
add(btn);
jp.add(btn1);
jp.add(btn2);
jp.add(btn3);
jp.add(btn4);
jp.add(btn5);
add(jp);
txt.setActionCommand("数1");
txt2.setActionCommand("数2");
btn.setActionCommand("计算");
btn1.setActionCommand("最大公约数");
btn2.setActionCommand("最小公倍数");
btn3.setActionCommand("水仙花数");
btn4.setActionCommand("完数");
btn5.setActionCommand("三位孪生素数");
police=new Police(txt,txt2,txta,btn,btn1,btn2,btn3,btn4,btn5);
txt.addActionListener(police);
txt2.addActionListener(police);
btn.addActionListener(police);
btn1.addActionListener(police);
btn2.addActionListener(police);
btn3.addActionListener(police);
btn4.addActionListener(police);
btn5.addActionListener(police);
}
public void init2(){
this.setBounds(500,300,400,320);
this.setTitle(s);
jp.add(btn1);
jp.add(btn2);
jp.add(btn3);
jp.add(btn4);
jp.add(btn5);
add(jp);
btn1.setActionCommand("最大公约数");
btn2.setActionCommand("最小公倍数");
btn3.setActionCommand("水仙花数");
btn4.setActionCommand("完数");
btn5.setActionCommand("三位孪生素数");
police=new Police(btn1,btn2,btn3,btn4,btn5);
btn1.addActionListener(police);
btn2.addActionListener(police);
btn3.addActionListener(police);
btn4.addActionListener(police);
btn5.addActionListener(police);
}
}