2012-04-14 61 views
3

我想創建類似於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 
+0

爲什麼不仍然使用的JOptionPane,但只要給它3個JTextField的情況?例如,請查看[this](http://stackoverflow.com/a/9952457/522444)。 – 2012-04-14 01:28:18

回答

8

同樣,爲什麼不使用JOptionPane?許多人誤解了這些有用的結構,認爲只有在沒有任何事情能夠離開事實時才能用於最簡單的gui。使用這些功能獲得最大權力的關鍵在於瞭解大多數JOptionPane方法中的第二個參數是Object,並且此對象可能是一個非常複雜甚至是大型的JPanel,其中包含其他組件,包括其他JPanel,JTables,JComboBox等等。 ..我用這個來向用戶展示覆雜的模態輸入對話框,就像你想要做的一樣。然後,當JOptionPane已經處理並且程序流返回到您的程序時,您將查詢顯示在JOptionPane中的複雜GUI的屬性並提取其信息。再次請查看我的鏈接here,看看我的意思。

例如,對於您的情況,如果你想要一個持有3個JTextFields將讓你的B號,名字和姓氏的信息一個JPanel,只需創建包含JTextField中和其相應的Jlabel之下一個JPanel:

import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import java.util.HashMap; 
import java.util.Map; 

import javax.swing.BorderFactory; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

class PlayerEditorPanel extends JPanel { 
    enum FieldTitle { 
     B_NUMBER("B Number"), FIRST_NAME("First Name"), LAST_NAME("Last Name"); 
     private String title; 

     private FieldTitle(String title) { 
     this.title = title; 
     } 

     public String getTitle() { 
     return title; 
     } 
    }; 

    private static final Insets WEST_INSETS = new Insets(5, 0, 5, 5); 
    private static final Insets EAST_INSETS = new Insets(5, 5, 5, 0); 
    private Map<FieldTitle, JTextField> fieldMap = new HashMap<FieldTitle, JTextField>(); 

    public PlayerEditorPanel() { 
     setLayout(new GridBagLayout()); 
     setBorder(BorderFactory.createCompoundBorder(
      BorderFactory.createTitledBorder("Player Editor"), 
      BorderFactory.createEmptyBorder(5, 5, 5, 5))); 
     GridBagConstraints gbc; 
     for (int i = 0; i < FieldTitle.values().length; i++) { 
     FieldTitle fieldTitle = FieldTitle.values()[i]; 
     gbc = createGbc(0, i); 
     add(new JLabel(fieldTitle.getTitle() + ":", JLabel.LEFT), gbc); 
     gbc = createGbc(1, i); 
     JTextField textField = new JTextField(10); 
     add(textField, gbc); 

     fieldMap.put(fieldTitle, textField); 
     } 
    } 

    private GridBagConstraints createGbc(int x, int y) { 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = x; 
     gbc.gridy = y; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 

     gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST; 
     gbc.fill = (x == 0) ? GridBagConstraints.BOTH 
      : GridBagConstraints.HORIZONTAL; 

     gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS; 
     gbc.weightx = (x == 0) ? 0.1 : 1.0; 
     gbc.weighty = 1.0; 
     return gbc; 
    } 

    public String getFieldText(FieldTitle fieldTitle) { 
     return fieldMap.get(fieldTitle).getText(); 
    } 

} 

然後顯示它在一個的JOptionPane像這樣:

PlayerEditorPanel playerEditorPanel = new PlayerEditorPanel(); 


int result = JOptionPane.showConfirmDialog(null, playerEditorPanel, 
    "Edit Player JOptionPane", JOptionPane.OK_CANCEL_OPTION, 
    JOptionPane.PLAIN_MESSAGE); 

,然後提取從JPanel中必要的信息:

 if (result == JOptionPane.OK_OPTION) { 
      for (PlayerEditorPanel.FieldTitle fieldTitle : PlayerEditorPanel.FieldTitle 
       .values()) { 
       textArea.append(String.format("%10s: %s%n", 
        fieldTitle.getTitle(), 
        playerEditorPanel.getFieldText(fieldTitle))); 
      } 
     } 

主類可能看起來像:

import java.awt.*; 
import java.awt.event.*; 
import java.util.HashMap; 
import java.util.Map; 

import javax.swing.*; 

@SuppressWarnings("serial") 
public class ComplexOptionPane extends JPanel { 
    private PlayerEditorPanel playerEditorPanel = new PlayerEditorPanel(); 
    private JTextArea textArea = new JTextArea(12, 30); 

    public ComplexOptionPane() { 
     textArea.setEditable(false); 
     textArea.setFocusable(false); 
     textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16)); 
     JPanel bottomPanel = new JPanel(); 
     bottomPanel.add(new JButton(new AbstractAction("Get Player Information") { 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      int result = JOptionPane.showConfirmDialog(null, playerEditorPanel, 
        "Edit Player JOptionPane", JOptionPane.OK_CANCEL_OPTION, 
        JOptionPane.PLAIN_MESSAGE); 
      if (result == JOptionPane.OK_OPTION) { 
       for (PlayerEditorPanel.FieldTitle fieldTitle : PlayerEditorPanel.FieldTitle 
        .values()) { 
        textArea.append(String.format("%10s: %s%n", 
         fieldTitle.getTitle(), 
         playerEditorPanel.getFieldText(fieldTitle))); 
       } 
      } 
     } 
     })); 
     setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     setLayout(new BorderLayout(5, 5)); 
     add(new JScrollPane(textArea), BorderLayout.CENTER); 
     add(bottomPanel, BorderLayout.PAGE_END); 
    } 

    private static void createAndShowGui() { 
     ComplexOptionPane mainPanel = new ComplexOptionPane(); 

     JFrame frame = new JFrame("ComplexOptionPane"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 
+0

哇,謝謝。 – 2012-04-14 14:07:04

+0

@Shane:不客氣 – 2012-04-14 15:22:47