2014-03-26 64 views
1

我正在嘗試構建一個小型程序,它有3個菜單項,學生,大學和出口。我已經完成了它,它編譯,但學生和大學菜單項無法打開內部Java面板。當menuItems點擊時Java內部面板不打開

它爲我的Java 2審查額外的信用問題(我真的需要點) 分配問題讀作:

編寫建立一個菜單一個桌面應用程序,具有大學及面板程序學生組成的面板。大學小組有名稱和州代碼。學生面板具有姓名和ID。用戶可以分別輸入名稱和大學或學生的代碼或ID。每個面板都有一個提交按鈕,用於接受數據,清除字段並重置焦點。每個面板都有一個檢查輸入法。 Id是一個整數。大學或學生的數據被放置在各自的數組列表中。

我有一個工作的例子,我基於這一個,並不能找出我出錯的地方。單擊學生和大學時面板將不會打開。

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

public class Question4 extends JFrame 
{ 
    private JDesktopPane theDesktop; 
    ArrayList<Student> studentList = new ArrayList<Student>(); 
    ArrayList<University> universityList = new ArrayList<University>(); 

    public static void main(String args[]) 
    { 
     Question4 q4 = new Question4(); 
     q4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     q4.setSize(400,400); 
     q4.setLocationRelativeTo(null); 
     q4.setVisible(true); 
    } 

    public Question4() 
    { 
     super("Question 4"); 
     theDesktop = new JDesktopPane(); 
     JMenuBar bar = new JMenuBar(); 
     JMenu menu = new JMenu("Menu"); 
     JMenuItem studentItem = new JMenuItem("Student"); 
     JMenuItem universityItem = new JMenuItem("University"); 
     JMenuItem exitItem = new JMenuItem("Exit"); 

     exitItem.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent ae) 
      { 
       System.exit(0); 
      } 
     }); 

     studentItem.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent ae) 
      { 
       JInternalFrame sFrame = new JInternalFrame("Student",true,true,true); 
       AddStudentPanel sPanel = new AddStudentPanel(); 
       sFrame.add(sPanel); 
       sFrame.pack(); 
       theDesktop.add(sFrame); 
       sFrame.setVisible(true);   
      } 
     }); 

     universityItem.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent ae) 
      { 
       JInternalFrame uFrame = new JInternalFrame("University",true,true,true);  
       AddUniversityPanel uPanel = new AddUniversityPanel(); 
       uFrame.add(uPanel); 
       uFrame.pack(); 
       theDesktop.add(uFrame); 
       uFrame.setVisible(true);   
      } 
     }); 

     menu.add(studentItem); 
     menu.add(universityItem); 
     menu.add(exitItem); 

     bar.add(menu); 

     setJMenuBar(bar); 
     add(theDesktop); 
    } 

    private class AddStudentPanel extends JPanel 
    { 
     private JLabel nameLabel; 
     private JLabel idLabel; 
     private JLabel submitLabel; 
     private JTextField nameField; 
     private JTextField idField; 
     private JButton submitButton; 

     public AddStudentPanel() 
     { 
      setLayout(new GridLayout (3,2)); 

      nameLabel = new JLabel("Enter Name"); 
      idLabel = new JLabel("Enter ID"); 
      submitLabel = new JLabel("Submit"); 

      nameField = new JTextField(15); 
      idField = new JTextField(15); 

      StudentHandler sHandler = new StudentHandler(); 
      submitButton.addActionListener(sHandler); 

      add(nameLabel); 
      add(nameField); 
      add(idLabel); 
      add(idField); 
      add(submitLabel); 
      add(submitButton); 
     } 

     class StudentHandler implements ActionListener 
     { 
      public void actionPerformed(ActionEvent ae) 
      { 

       if(ae.getSource() == submitButton) 
       { 
        if(checkStudentInput()) 
        { 

         studentList.add(new Student(nameField.getText(),Integer.parseInt(idField.getText()))); 
         nameField.setText(""); 
         idField.setText(""); 
         nameField.requestFocus(); 
        } 
       } 
      } 

      public boolean checkStudentInput() 
      { 
       boolean input = true; 

       if(nameField.getText().isEmpty()) 
       { 
        JOptionPane.showMessageDialog(null,"Please enter a name"); 
        nameField.requestFocus(); 
        input = false; 
       } 
       else if(idField.getText().isEmpty()) 
       { 
        JOptionPane.showMessageDialog(null,"Please enter an id"); 
        idField.requestFocus(); 
        input = false; 
       } 
       else 
       { 
        try 
        { 
         Integer.parseInt(idField.getText()); 
        }catch(NumberFormatException nfe) 
        { 
         JOptionPane.showMessageDialog(null,"Please enter an integer for id"); 
         idField.setText(""); 
         idField.requestFocus(); 
         input = false; 
        } 
       } 

       return input; 
      } 
     } 
    } 

    private class AddUniversityPanel extends JPanel 
    { 
     private JLabel nameLabel; 
     private JLabel codeLabel; 
     private JLabel submitLabel; 
     private JTextField nameField; 
     private JTextField codeField; 
     private JButton submitButton; 

     public AddUniversityPanel() 
     { 
      setLayout(new GridLayout (3,2)); 

      nameLabel = new JLabel("Enter Name"); 
      codeLabel = new JLabel("Enter State Code"); 
      submitLabel = new JLabel("Submit"); 

      nameField = new JTextField(15); 
      codeField = new JTextField(15); 

      UniversityHandler uHandler = new UniversityHandler(); 
      submitButton.addActionListener(uHandler); 

      add(nameLabel); 
      add(nameField); 
      add(codeLabel); 
      add(codeField); 
      add(submitLabel); 
      add(submitButton); 
     } 

     class UniversityHandler implements ActionListener 
     { 
      public void actionPerformed(ActionEvent ae) 
      { 

       if(ae.getSource() == submitButton) 
       { 
        if(checkUniversityInput()) 
        { 

         universityList.add(new University(nameField.getText(),Integer.parseInt(codeField.getText()))); 
         nameField.setText(""); 
         codeField.setText(""); 
         nameField.requestFocus(); 
        } 
       } 
      } 


      public boolean checkUniversityInput() 
      { 
       boolean input = true; 

       if(nameField.getText().isEmpty()) 
       { 
        JOptionPane.showMessageDialog(null,"Please enter a name"); 
        nameField.requestFocus(); 
        input = false; 
       } 
       else if(codeField.getText().isEmpty()) 
       { 
        JOptionPane.showMessageDialog(null,"Please enter an State Code"); 
        codeField.requestFocus(); 
        input = false; 
       } 
       else 
       { 
        try 
        { 
         Integer.parseInt(codeField.getText()); 
        }catch(NumberFormatException nfe) 
        { 
         JOptionPane.showMessageDialog(null,"Please enter an integer for State Code"); 
         codeField.setText(""); 
         codeField.requestFocus(); 
         input = false; 
        } 
       } 

       return input; 
      } 
     } 
    } 
} 

回答

2

更改線路

add(theDesktop); 

setContentPane(theDesktop); 

,你應該能夠繼續。

您還需要在AddStudentPanelAddUniversityPanel中初始化submitButton以避免NullPointerException

submitButton = new JButton() 

希望這可以幫助您一路完成您的任務。

+0

提交按鈕是問題,我不能相信我忽略了初始化它。只需要第二組眼睛。 謝謝! –

+0

不客氣。我以爲你必須使用'setContentPane',但顯然'add'也可以。 :) – Steinar

相關問題