在myFrame2中的actionPerformed方法下面加上Form1 = Application.Form1;就没问题,但是在windowOpened方法下面已经有了为什么还出现这种问题?
import java.awt.*;
public class j02110501_Project1 {
private static j02110501_Project1 Application;
myFrame1 Form1;
myFrame2 Form2;
myFrame3 Form3;
public static void main(String arg[]) {
Application = new j02110501_Project1();
Application.Form1 = new myFrame1( Application );
Application.Form2 = new myFrame2( Application );
Application.Form1.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
public class myFrame1 extends Frame {
private j02110501_Project1 Application;
private myFrame1 Form1;
private myFrame2 Form2;
private myFrame3 Form3;
Button B1, B2;
Label L1, L2;
public myFrame1(j02110501_Project1 app) {
Application = app;
Form1 = this;
this.setLayout(null);
this.setBounds(100,300,265,200);
this.setTitle("Form1:主窗体");
this.addWindowListener(new myWindowAdapter() );
B1 = new Button("Show Form2");
B1.setBounds(60,30,140,30);
B1.addActionListener(new myActionListener() );
this.add(B1);
L1 = new Label("0",Label.RIGHT);
L1.setBounds(60,70,140,30);
L1.setBackground(Color.PINK);
this.add(L1);
B2 = new Button("Show Form3");
B2.setBounds(60,120,140,30);
B2.addActionListener(new myActionListener());
this.add(B2);
L2 = new Label("0",Label.RIGHT);
L2.setBounds(60,160,140,30);
L2.setBackground(Color.PINK);
this.add(L2);
}
class myWindowAdapter extends WindowAdapter {
public void windowOpened(WindowEvent e) {
//此时Application所有静态建立的窗体都已建立完成
//取得Form1所属项目的其他窗体
Form2 = Application.Form2;
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
class myActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == B1) {
Form2.setVisible(true);
}
else if(e.getSource()==B2) {
if(Form3 == null) {
Application.Form3 = new myFrame3(Application);
Form3 = Application.Form3;
System.out.println("Create Form");
}
else
System.out.println("Show Form");
Form3.setVisible(true);
}
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class myFrame2 extends Frame {
private j02110501_Project1 Application;
private myFrame1 Form1;
private myFrame2 Form2;
private myFrame3 Form3;
TextField text1,text2;
Label lb1;
Button B1;
public myFrame2( j02110501_Project1 app) {
Application = app;
Form2 = this;
this.setLayout(null);
this.setBounds(420,300,300,90);
this.setTitle("Foem2: 加法运算");
this.addWindowListener(new myWindowAdapter());
text1 = new TextField("0",6);
text1.setBounds(30,40,80,30);
this.add(text1);
lb1 = new Label("+");
lb1.setBounds(115,40,20,30);
this.add(lb1);
text2 = new TextField("0",6);
text2.setBounds(150,40,80,30);
this.add(text2);
B1 = new Button("计算");
B1.setBounds(240,40,40,30);
B1.addActionListener(new myActionListener());
this.add(B1);
}
class myActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//Form1 = Application.Form1;
double sum;
try {
sum = Double.parseDouble( Form2.text1.getText().trim())
+ Double.parseDouble(Form2.text2.getText().trim());
Form1.L1.setText(String.valueOf(sum));
if(Form3 != null) {
System.out.println("called by Form2:\n Form3.text1 = "
+ Form3.text1.getText()
+ " Form3.text2 = "
+ Form3.text2.getText() );
}
}
catch(NumberFormatException ecp) {
JOptionPane.showMessageDialog(null,"必须输入两个“数值”!");
}
}
}
class myWindowAdapter extends WindowAdapter {
public void windowOpend(WindowEvent e) {
Form1 = Application.Form1;
Form3 = Application.Form3;
}
public void windowActivated(WindowEvent e) {
if(Form3 == null)
Form3 = Application.Form3;
}
public void windowClosing(WindowEvent e) {
Form2.setVisible(false);
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class myFrame3 extends Frame {
private j02110501_Project1 Application;
private myFrame1 Form1;
private myFrame2 Form2;
private myFrame3 Form3;
TextField text1,text2;
Label lb1;
Button B1;
public myFrame3(j02110501_Project1 app) {
Application = app;
Form3 = this;
this.setLayout(null);
this.setBounds(420,410,300,90);
this.setTitle("Foem3: 乘法运算");
this.addWindowListener(new myWindowAdapter());
text1 = new TextField("1",6);
text1.setBounds(30,40,80,30);
this.add(text1);
lb1 = new Label("*");
lb1.setBounds(115,40,20,30);
this.add(lb1);
text2 = new TextField("1",6);
text2.setBounds(150,40,80,30);
this.add(text2);
B1 = new Button("计算");
B1.setBounds(240,40,40,30);
B1.addActionListener(new myActionListener());
this.add(B1);
}
class myActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
double multiplication;
try {
multiplication =
Double.parseDouble(Form3.text1.getText().trim())
* Double.parseDouble(Form3.text2.getText().trim());
Form1.L2.setText(String.valueOf(multiplication));
System.out.println("called by Form3:\n Form2.text1 = "
+ Form2.text1.getText()
+ " Form2.text2 = "
+ Form2.text2.getText() );
}
catch(NumberFormatException ecp) {
JOptionPane.showMessageDialog(null,"必须输入两个“数值”!");
}
}
}
class myWindowAdapter extends WindowAdapter {
public void windowOpened(WindowEvent e) {
Form1 = Application.Form1;
Form2 = Application.Form2;
}
public void windowClosing(WindowEvent e) {
Form3.setVisible(false);
}
}
}
错误信息:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at myFrame2$myActionListener.actionPerformed(myFrame2.java:46)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)