質問

I'm trying to build a small little program that has 3 menu items on it, Student, University, and Exit. I have it pretty much finished, it compiles but the student and university menu items fail to open the Internal java panel.

Its an extra credit question for my Java 2 Review (I really need the points) The Assignment question reads as:

Write a program that builds a desktop application with a menu that has a panel for University and a panel for Student. The University panel has name and state code. The Student panel has name and id. The user can type in the name and either the code or id for University or Student, respectively. Each panel has a submit button that accepts the data, clears the fields, and resets focus. Each panel has a check input method. Id is an integer. The university or student data is placed in their respective arraylist.

I have a working example that I based this one off of, and can't figure out where I've gone wrong. The panels will not open when Student and University are clicked.

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;
            }
        }
    }
}
役に立ちましたか?

解決

Change the line

add(theDesktop);

to

setContentPane(theDesktop);

and you should be able to continue.

You will also need to initialize the submitButton in both AddStudentPanel and AddUniversityPanel to avoid a NullPointerException.

submitButton = new JButton()

Hope this helps you along the way to finish your assignment.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top