日期:2014-05-20  浏览次数:20779 次

求助 关于窗体的问题
在Eclipse下写了个程序 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class window extends JFrame
{
private JTextField CellPNum;
private JButton okButton;
private JButton exitButton;
public window()
{
JTextField CellPNum=new JTextField(11);
JLabel CPNum=new JLabel("CellPhone Number:");
okButton=new JButton("OK");
exitButton=new JButton("EXIT");
okButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==okButton)
{CHECK();}
}
});
exitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==exitButton)
{EXIT();}
}
});
JPanel CPN=new JPanel();
CPN.add(CPNum);
CPN.add(CellPNum);
JPanel ButtonPanel=new JPanel();
ButtonPanel.add(okButton);
ButtonPanel.add(exitButton);
Container container=this.getContentPane();
container.setLayout(new GridLayout(2,1));
container.add(CPN);
container.add(ButtonPanel);
setSize(325,225);
setVisible(true);
}
protected void CHECK() {
if(CellPNum.getText().equals(""))
{JOptionPane.showMessageDialog(this,"Please fill All field");}
else if(CellPNum.getText().length()>11)
{JOptionPane.showMessageDialog(this, "The CellPhone Number's length limit 11 letters");}
else if(CellPNum.getText().length()<11)
{JOptionPane.showMessageDialog(this, "Please fill the CellPhone Number");}
else if(CellPNum.getText().matches("[135]+[0-9]*"))
{JOptionPane.showMessageDialog(this, "This CellPhone Number isn't start with 135");}
else
JOptionPane.showMessageDialog(this,"This Number is start with 135");
}
protected void EXIT(){
this.setVisible(false);
System.exit(0);

}
public static void main(String[]args)
{
new window();
}

}

测试的时候就是不知道错在哪里了
求各位大神打救

------解决方案--------------------
空指针异常吧?还有说下类名大写!

原因写在注释里了:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class window extends JFrame {
private JTextField CellPNum;
private JButton okButton;
private JButton exitButton;

public window() {
CellPNum = new JTextField(11);//前面那个定义去了。因为你后面button里要用这个变量。
JLabel CPNum = new JLabel("CellPhone Number:");
okButton = new JButton("OK");
exitButton = new JButton("EXIT");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okButton) {
CHECK();
}
}
});
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == exitButton) {
EXIT();
}
}
});
JPanel CPN = new JPanel();
CPN.add(CPNum);
CPN.add(CellPNum);
JPanel ButtonPanel = new JPanel();
ButtonPanel.add(okButton);
ButtonPanel.add(exitButton);
Container container = this.getContentPane();
container.setLayout(new GridLayout(2, 1));
container.add(CPN);
container.add(ButtonPanel);
setSize(325, 225);
setVisible(true);
}

protected void CHECK() {
if (CellPNum.getText().equals("")) {
JOptionPane.showMessageDialog(this, "Please fill All field");
} else if (CellPNum.getText().length() > 11) {
JOptionPane.showMessageDialog(this,
"The CellPhone Number's length limit 11 letters");
} else if (CellPNum.getText().length() < 11) {
JOptionPane.showMessageDialog(this,
"Please fill the CellPhone Number");
} else if (CellPNum.getText().matches("[135]+[0-9]*")) {
JOptionPane.showMessageDialog(this,
"This CellPhone Number isn't start with 135");
} else
JOptionPane
.showMessageDialog(this, "This Number is start with 135");
}

protected void EXIT() {
this.setVisible(false);
System.exit(0);

}

public static void main(String[] args) {
new window();
}

}