当我单击按钮bAddStudent时,什么也没发生。也许是因为我有这些标签?我不知道,这看起来还不错。有什么想法吗?对于任何建议或解决方案,我将非常感谢。这是我的代码:

        import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.ParseException;
    import java.util.Vector;

    import javax.swing.JButton;
    import javax.swing.JFormattedTextField;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTabbedPane;
    import javax.swing.JTable;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.text.MaskFormatter;






    public class MainWindow extends JFrame implements ActionListener {

    private JPanel students;
    private JPanel teachers;
    private JPanel lessons;
    private JPanel courses;
    private JPanel addPanel;

    private JTabbedPane tp;
    private JTabbedPane tp2;

    private Container pane;

    private JButton bAddStudent;
    private JButton addTeacher;


    private JLabel lStudent;
    private JLabel lTeacher;
    private JLabel lStudentName;

    private JTextField tStudentName;
    private JTextField tStudentSurname;
    private JTextField tStudentPhone;
    private JTextField tStudentCourse;

    private MaskFormatter FORMAT;


        public MainWindow(){

            setTitle("School");
            setSize(800, 600);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setVisible(true);

            JPanel students = new JPanel();
            JPanel teachers = new JPanel();
            JPanel lessons = new JPanel();
            JPanel courses = new JPanel();
            JPanel addPanel = new JPanel(new BorderLayout());
            JPanel translations = new JPanel();
            JPanel addStudent = new JPanel(null);
            JPanel addTeacher = new JPanel(null);
            JPanel addTranslation = new JPanel(null);



            tp=new JTabbedPane();
            tp2 = new JTabbedPane();
            Container pane = this.getContentPane();
            pane.add(tp);
            tp.addTab("Uczniowie",students);
            tp.addTab("Nauczyciele",teachers);
            tp.addTab("Harmonogram zajec",lessons);
            tp.addTab("Kursy",courses);
            tp.addTab("Tlumaczenia", translations);
            tp.addTab("Panel administratora", addPanel);

            tp2.add("Nowy uczen",addStudent);
            tp2.add("Nowy nauczyciel",addTeacher);
            tp2.add("Nowe zlecenia",addTranslation);

            addPanel.add(tp2,BorderLayout.CENTER);

    ////******************DODAWANIE UCZNIA***************************/////////

            JLabel lNotes = new JLabel("DODATKOWE UWAGI");
            lNotes.setBounds(490, 75, 200, 40);
            addStudent.add(lNotes);


            JLabel lInfo = new JLabel("PODSTAWOWE INFORMACJE");
            lInfo.setBounds(75, 75, 200, 40);
            addStudent.add(lInfo);

            JTextArea DATABASE_FIELD = new JTextArea(3, 3);
            JScrollPane scrollPane = new JScrollPane(DATABASE_FIELD);
            scrollPane.setBounds(400, 125, 300, 175);
            addStudent.add(scrollPane);
            DATABASE_FIELD.setEditable(true);

            JLabel lStudentName = new JLabel("Imie : ");
            lStudentName.setBounds(50, 125, 100, 30);
            addStudent.add(lStudentName);
            JTextField tStudentName = new JTextField();
            tStudentName.setBounds(170, 125, 100, 30);
            addStudent.add(tStudentName);

            JLabel lStudentSurname = new JLabel("Nazwisko : ");
            lStudentSurname.setBounds(50, 175, 100, 30);
            addStudent.add(lStudentSurname);
            JTextField tStudentSurname = new JTextField();
            tStudentSurname.setBounds(170, 175, 100, 30);
            addStudent.add(tStudentSurname);

            JLabel lStudentPhone = new JLabel("Telefon : ");
            lStudentPhone.setBounds(50, 225, 100, 30);
            addStudent.add(lStudentPhone);
            JTextField tStudentPhone = new JTextField();
            tStudentPhone.setBounds(170, 225, 100, 30);
            addStudent.add(tStudentPhone);

            JLabel lStudentCourse = new JLabel("Kurs : ");
            lStudentCourse.setBounds(50, 275, 100, 30);
            addStudent.add(lStudentCourse);
            JTextField tStudentCourse = new JTextField();
            tStudentCourse.setBounds(170, 275, 100, 30);
            addStudent.add(tStudentCourse);

            JButton bAddStudent = new JButton("Dodaj ucznia");
            bAddStudent.setBounds(60, 325, 200, 40);
            bAddStudent.addActionListener(this);
            addStudent.add(bAddStudent);

    /////************************DODAWANIE NAUCZYCIELA****************//////////

            JLabel lNotest = new JLabel("DODATKOWE UWAGI");
            lNotest.setBounds(490, 75, 200, 40);
            addTeacher.add(lNotest);


            JLabel lInfot = new JLabel("PODSTAWOWE INFORMACJE");
            lInfot.setBounds(75, 25, 200, 40);
            addTeacher.add(lInfot);

            JTextArea DATABASE_FIELDt = new JTextArea(3, 3);
            JScrollPane scrollPanet = new JScrollPane(DATABASE_FIELDt);
            scrollPanet.setBounds(400, 125, 300, 175);
            addTeacher.add(scrollPanet);
            DATABASE_FIELDt.setEditable(true);

            JLabel lTeacherName = new JLabel("Imie : ");
            lTeacherName.setBounds(50, 75, 100, 30);
            addTeacher.add(lTeacherName);
            JTextField tTeacherName = new JTextField();
            tTeacherName.setBounds(170, 75, 100, 30);
            addTeacher.add(tTeacherName);

            JLabel lTeacherSurname = new JLabel("Nazwisko : ");
            lTeacherSurname.setBounds(50, 125, 100, 30);
            addTeacher.add(lTeacherSurname);
            JTextField tlTeacherSurname = new JTextField();
            tlTeacherSurname.setBounds(170, 125, 100, 30);
            addTeacher.add(tlTeacherSurname);

            JLabel lTeacherPhone = new JLabel("Telefon : ");
            lTeacherPhone.setBounds(50, 175, 100, 30);
            addTeacher.add(lTeacherPhone);
            JTextField tTeacherPhone = new JTextField();
            tTeacherPhone.setBounds(170, 175, 100, 30);
            addTeacher.add(tTeacherPhone);

            JLabel lTeacherCourse = new JLabel("Kurs : ");
            lTeacherCourse.setBounds(50, 225, 100, 30);
            addTeacher.add(lTeacherCourse);
            JTextField tTeacherCourse = new JTextField();
            tTeacherCourse.setBounds(170, 225, 100, 30);
            addTeacher.add(tTeacherCourse);

            JLabel lTeacherMail = new JLabel("Email : ");
            lTeacherMail.setBounds(50, 275, 100, 30);
            addTeacher.add(lTeacherMail);
            JTextField tTeacherMail = new JTextField();
            tTeacherMail.setBounds(170, 275, 100, 30);
            addTeacher.add(tTeacherMail);

            JButton bAddTeacher = new JButton("Dodaj nauczyciela");
            bAddTeacher.setBounds(60, 400, 200, 40);
            bAddTeacher.addActionListener(this);
            addTeacher.add(bAddTeacher);

    ///*************************DODAWANIE ZLECENIA **********************//////

            JLabel lNotesZ = new JLabel("DODATKOWE UWAGI");
            lNotesZ.setBounds(490, 75, 200, 40);
            addTranslation.add(lNotesZ);


            JLabel lInfoZ = new JLabel("PODSTAWOWE INFORMACJE");
            lInfoZ.setBounds(75, 75, 200, 40);
            addTranslation.add(lInfoZ);

            JTextArea DATABASE_FIELDZ = new JTextArea(3, 3);
            JScrollPane scrollPaneZ = new JScrollPane(DATABASE_FIELDZ);
            scrollPaneZ.setBounds(400, 125, 300, 175);
            addTranslation.add(scrollPaneZ);
            DATABASE_FIELDZ.setEditable(true);

            JLabel lClientName = new JLabel("Imie : ");
            lClientName.setBounds(50, 125, 100, 30);
            addTranslation.add(lClientName);
            JTextField tClientName = new JTextField();
            tClientName.setBounds(170, 125, 100, 30);
            addTranslation.add(tClientName);

            JLabel lClientSurname = new JLabel("Nazwisko : ");
            lClientSurname.setBounds(50, 175, 100, 30);
            addTranslation.add(lClientSurname);
            JTextField tClientSurname = new JTextField();
            tClientSurname.setBounds(170, 175, 100, 30);
            addTranslation.add(tClientSurname);

            JLabel lClientPhone = new JLabel("Telefon : ");
            lClientPhone.setBounds(50, 175, 100, 30);
            addTeacher.add(lClientPhone);
            JTextField tClientPhone = new JTextField();
            tClientPhone.setBounds(170, 175, 100, 30);
            addTeacher.add(tClientPhone);

            try {
                FORMAT = new MaskFormatter("  ####-##-##");
                FORMAT.setPlaceholderCharacter('_');
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                JOptionPane.showMessageDialog(null,
                        "Niepoprawny format daty (rrrr-mm-dd)");
            }


            JLabel lAddDate = new JLabel("Data zlecenia : ");
            lAddDate.setBounds(50, 225, 100, 30);
            addTranslation.add(lAddDate);
            JFormattedTextField tAddDate = new JFormattedTextField(FORMAT);
            tAddDate.setBounds(170, 225, 100, 30);
            addTranslation.add(tAddDate);

            JButton bAddStudentZ = new JButton("Dodaj zlecenie");
            bAddStudentZ.setBounds(60, 325, 200, 40);
            bAddStudentZ.addActionListener(this);
            addTranslation.add(bAddStudentZ);

            Vector<String> columnNames = new Vector<String>();
            columnNames.add("ID");
            columnNames.add("Imie");
            columnNames.add("Nazwisko");
            columnNames.add("Telefon");
            columnNames.add("Kurs");
            Vector<String> studentVector = new Vector<String>();
            DefaultTableModel model = new DefaultTableModel(studentVector, columnNames);
            JTable table = new JTable(model);
            JScrollPane tableContainer = new JScrollPane(table);
            table.setPreferredScrollableViewportSize(new Dimension(600, 500));
            table.setFillsViewportHeight(true);
            table.setBounds(100, 50, 600, 200);
            table.setVisible(true);
            students.add(tableContainer, BorderLayout.CENTER);
        }

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            MainWindow window = new MainWindow();
            window.setVisible(true);

        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub

            Object event = e.getSource();

            if (event == bAddStudent)
            {

    //          Student patient_1 = new Student(tStudentName.getText(),
    //                  tStudentSurname.getText(), tStudentPhone.getText(),
    //                  tStudentCourse.getText(), "");
                tStudentName.setText("");
                tStudentSurname.setText("");
                tStudentPhone.setText("");
                tStudentCourse.setText("");
    //          DatabaseManagement.getInstance().addStudent(patient_1);
            }

        }

    }

最佳答案

您是shadowing变量bAddStudent。更换

JButton bAddStudent = new JButton("Dodaj ucznia");




bAddStudent = new JButton("Dodaj ucznia");


当然,ActionListener中使用的所有其他组件也是如此

10-07 12:08