如何妥善处理窗口?

时间:2018-01-03 01:37:50

标签: java swing jframe addeventlistener

我有一个扩展JFrame的类,我有私有int值n = 0.我需要一个监听器或类似的东西,一旦它等于S.lenght就会触发,该触发器应该处置窗口。我该怎么做?

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

public class myGUI extends JFrame {
    private JButton button = new JButton("Add");
    private JTextField name = new JTextField("", 5);
    private JTextField course = new JTextField("", 5);
    private JLabel label1 = new JLabel("Student's name:  ");
    private JLabel label2 = new JLabel("Student's course:");
    public Student S[];
    public int n = 0;

private JCheckBox check = new JCheckBox("Student is lagging behind", false);

public myGUI(int n) {
    super("Student's data");
    this.setBounds(550, 450, 400, 125);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.S = new Student[n];
    Container container = this.getContentPane();
    container.setLayout(new GridLayout(3, 2, 2, 2));
    container.add(label1);
    container.add(name);
    container.add(label2);
    container.add(course);
    container.add(check);
    button.addActionListener(new ButtonEventListener());
    container.add(button);
    //this.dispose();
}

class ButtonEventListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        if ((!name.getText().equals("")) && (!course.getText().equals("")) && (n < S.length)) {
            S[n++] = new Student(name.getText(), Integer.parseInt(course.getText()), check.isSelected());
            String message = "Student #" + n + " has been successfully added to database!";
            JOptionPane.showMessageDialog(null, message,
                    "Output", JOptionPane.PLAIN_MESSAGE);
        }

        name.setText("");
        course.setText("");
        check.setSelected(false);
    }
}
public static void main(String[] args) {
    int l = Integer.parseInt(JOptionPane.showInputDialog("Enter amount of students:"));
    myGUI app = new myGUI(l);
    app.setVisible(true);
    while (1 == 1)
    {
        if (app.n == app.S.length){
            app.setVisible(false);
            break;
        }
    }
    int c = Integer.parseInt(JOptionPane.showInputDialog("Enter the course you want to look through:"));
    int count = 0;
    for (int i = 0; i < app.S.length; i++) {
        if (app.S[i].isDolg(c))
            count++;
    }
    JOptionPane.showMessageDialog(null, "There are " + count + " students lagging behind at " + c + " course!");
}
}

我试图做一个循环,一旦n == S.lenght中断,但它从未开始

1 个答案:

答案 0 :(得分:0)

不需要while循环更改ButtonListner和主方法逻辑,如下所示。

class ButtonEventListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        if ((!name.getText().equals("")) && (!course.getText().equals(""))
                && (n < S.length)) {
            S[n++] = new Student(name.getText(), Integer.parseInt(course
                    .getText()), check.isSelected());
            String message = "Student #" + n
                    + " has been successfully added to database!";
            JOptionPane.showMessageDialog(null, message, "Output",
                    JOptionPane.PLAIN_MESSAGE);
        }

        name.setText("");
        course.setText("");
        check.setSelected(false);

        if (n == S.length) {
            dispose();
            int c = Integer
                    .parseInt(JOptionPane
                            .showInputDialog("Enter the course you want to look through:"));
            int count = 0;
            for (int i = 0; i < S.length; i++) {
                if (S[i].isDolg(c))
                    count++;
            }
            JOptionPane.showMessageDialog(null, "There are " + count
                    + " students lagging behind at " + c + " course!");
        }
    }
}

public static void main(String[] args) {
    int l = Integer.parseInt(JOptionPane
            .showInputDialog("Enter amount of students:"));
    Test app = new Test(l);
    app.setVisible(true);

}