单击menuItem时,Java内部面板无法打开

时间:2014-03-26 19:35:00

标签: java swing jinternalframe

我正在尝试构建一个包含3个菜单项的小程序,学生,大学和退出。我完成了它,它编译但是学生和大学菜单项无法打开内部java面板。

这是我的Java 2 Review的额外信用问题(我真的需要积分) 作业问题如下:

编写一个程序,构建一个桌面应用程序,其菜单包含一个面向大学的面板和一个面向学生的面板。大学小组有姓名和州代码。学生面板有姓名和ID。用户可以分别输入名称以及大学或学生的代码或ID。每个面板都有一个提交按钮,可以接受数据,清除字段并重置焦点。每个面板都有一个检查输入法。 Id是整数。大学或学生的数据被放置在他们各自的arraylist中。

我有一个工作示例,我将此基于此,并且无法弄清楚我哪里出错了。单击学生和大学时,面板将不会打开。

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;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

更改行

add(theDesktop);

setContentPane(theDesktop);

你应该能够继续。

您还需要初始化submitButtonAddStudentPanel中的AddUniversityPanel以避免NullPointerException

submitButton = new JButton()

希望这可以帮助您完成任务。