我想創建類似於JOptionPane的東西,但會從輸入中獲取多個(3)變量。所以我想我會使用一個單獨的有三個textField的JFrame。我使用Get和Set等訪問方法將變量從一個類獲取到另一個類,但我得到一個空指針excpetion。我想我會去弄錯變量的方法,並且很難找到一個可行的解決方案。從另一個JFrame中獲取變量信息的麻煩
public class Instructor()
{
public void Insert(JPanel panel)
{
panel.removeAll();
panel.updateUI();
//ResultSet resultSet = null;
String bNum = "";
String fName = "";
String lName = "";
InsertFrame insert = new InsertFrame();
insert.setVisible(true);
bNum = insert.getBNumber();
fName = insert.getFirstName();
lName = insert.getLastName();
/*
String bNum = JOptionPane.showInputDialog("Enter BNumber");
String fName = JOptionPane.showInputDialog("Enter First Name");
String lName = JOptionPane.showInputDialog("Enter Last Name");*/
try
{
connection = DriverManager.getConnection(URL);
insertNewInstructor = connection.prepareStatement(
"INSERT INTO Instructor" + "(BNumber, FirstName, LastName)" + "VALUES (?,?,?)");
}catch(SQLException sqlException){
sqlException.printStackTrace();
System.exit(1);
}//end catch
try
{
insertNewInstructor.setString(1, bNum);
insertNewInstructor.setString(2, fName);
insertNewInstructor.setString(3, lName);
insertNewInstructor.executeUpdate();
}catch(SQLException sqlException){
sqlException.printStackTrace();
}//end of catch
finally
{
close();
}//end
Display(panel);
}//end of insert method
}//end of class Instructor
class InsertFrame extends JFrame implements ActionListener
{
private JTextField bNumber;
private JLabel bNum;
private JTextField firstName;
private JLabel fName;
private JTextField lastName;
private JLabel lName;
private JButton ok;
private JPanel fieldPanel;
private JPanel buttonPanel;
private String bNumr = "";
private String frName = "";
private String lsName = "";
public InsertFrame()
{
bNumber = new JTextField(10);
bNum = new JLabel();
firstName = new JTextField(10);
fName = new JLabel();
lastName = new JTextField(10);
lName = new JLabel();
fieldPanel = new JPanel();
fieldPanel.setLayout(new GridLayout(3,2,4,4));
bNum.setText("B-Number:");
fieldPanel.add(bNum);
fieldPanel.add(bNumber);
fName.setText("First Name:");
fieldPanel.add(fName);
fieldPanel.add(firstName);
lName.setText("Last Name:");
fieldPanel.add(lName);
fieldPanel.add(lastName);
ok = new JButton("Ok");
ok.addActionListener(this);
this.add(fieldPanel, BorderLayout.CENTER);
this.add(buttonPanel,BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(310,300);
this.setResizable(false);
this.setVisible(false);
}//end of constructor
public void actionPerformed(ActionEvent e)
{
bNumr = bNumber.getText();
frName = firstName.getText();
lsName = lastName.getText();
}//end of method actionPerformed
public void setBNumber(String number)
{
bNumr = number;
}//end of setBNumber
public String getBNumber()
{
return bNumr;
}//end of getBNumber method
public void setFirstName(String firstN)
{
frName = firstN;
}//end of setFirstName
public String getFirstName()
{
return frName;
}//end of getFirstName method
public void setLastName(String lastN)
{
lsName = lastN;
}//end of setLastName method
public String getLastName()
{
return lsName;
}//end of getLastName method
}//end of InsertFrame
爲什麼不仍然使用的JOptionPane,但只要給它3個JTextField的情況?例如,請查看[this](http://stackoverflow.com/a/9952457/522444)。 – 2012-04-14 01:28:18