如何获取从 JTextField 收集的变量并将其提供给 getter 方法?

How do I get a variable collected from a JTextField and give it to a getter method?

对使用 Java 很陌生,花了几个小时寻找解决方案,但我无法找到如何使用 JTextField 按钮收集输入,然后 ActionListener用于可以接收 idNum 的 getter 方法,因此我可以在另一个 class.

中使用输入
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Staff extends JFrame implements ActionListener {
    
    private staffBooking staffBooking = new staffBooking();

    String weekArray[] = { "Week1", "Week2" };
    String dayArray[] = { "Monday" , "Tuesday", "Wednesday", "Thursday", "Friday" };
    
    JPanel panel = new JPanel();
    JLabel weekLabel = new JLabel("Week:  ", SwingConstants.RIGHT);
    JLabel dayLabel = new JLabel("Day:  ", SwingConstants.RIGHT);
    JLabel idLabel = new JLabel("ID:  ", SwingConstants.RIGHT);
    static JTextField id = new JTextField("",2);
    JComboBox weekDrop = new JComboBox(weekArray);
    JComboBox dayDrop = new JComboBox(dayArray);
    JButton button = new JButton("Submit");

    private static int idNum;
        
    public Staff() {
        setTitle("Staff");
        setSize(250,250);
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setLocationRelativeTo(null);
        
        setLayout(new GridLayout(4, 2));        
        
        add(weekLabel);
        add(weekDrop);
        
        add(dayLabel);
        add(dayDrop);
        
        add(idLabel);
        add(id);
        
        add(panel);
        add(button);
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(id.getText().equals("")==false) {
                    if (e.getSource() == button) {
                        staffBooking.setEnabled(true);
                        staffBooking.setVisible(true);
                        idNum = Integer.parseInt(id.getText());
                    }
                }
            }    
        });     
    }
    
    public static int getID() {
        
        return idNum;
    }   
}

我相信你想做的是这样的:

import javax.swing.*;
import javax.swing.SwingConstants;

public class Staff extends JFrame /*implements ActionListener*/ {

    private final JPanel panel = new JPanel();
    private final JLabel idLabel = new JLabel("ID:  ", SwingConstants.RIGHT);
    private final JTextField id = new JTextField("",5);
    private final JButton button = new JButton("Submit");
    private int idNum;

    public Staff() {
        StaffBooking staffBooking = new StaffBooking(this);
        setTitle("Staff");
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setLocationRelativeTo(null);
        panel.add(idLabel);
        panel.add(id);
        panel.add(button);
        add(panel);
        button.addActionListener(e -> {
            if(! id.getText().isEmpty()) {
                idNum = Integer.parseInt(id.getText());
                staffBooking.setVisible(true);
            }
        });

        pack();
        setVisible(true);
    }

    public int getID() {
        return idNum;
    }

    public static void main(String[] args) {
        new Staff();
    }
}

class StaffBooking extends JDialog {

    public StaffBooking(Staff staff) {
        JPanel panel = new JPanel();
        JLabel idLabel = new JLabel("  ");
        JButton button = new JButton("Show ID");
        button.addActionListener(e -> {
            idLabel.setText(String.valueOf(staff.getID()));
        });

        panel.add(button);
        panel.add(idLabel);
        add(panel);
        setLocationRelativeTo(null);
        pack();
    }
}

StaffBooking 中使用 setter 在这种情况下会更好:

public class Staff extends JFrame /*implements ActionListener*/ {

    private final JPanel panel = new JPanel();
    private final JLabel idLabel = new JLabel("ID:  ", SwingConstants.RIGHT);
    private final JTextField id = new JTextField("",5);
    private final JButton button = new JButton("Submit");

    public Staff() {
        StaffBooking staffBooking = new StaffBooking();
        setTitle("Staff");
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setLocationRelativeTo(null);
        panel.add(idLabel);
        panel.add(id);
        panel.add(button);
        add(panel);
        button.addActionListener(e -> {
            if(! id.getText().isEmpty()) {
                int idNum = Integer.parseInt(id.getText());
                staffBooking.setID(idNum);
                staffBooking.setVisible(true);
            }
        });

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new Staff();
    }
}

class StaffBooking extends JDialog {

    private int idNum;

    public StaffBooking() {
        JPanel panel = new JPanel();
        JLabel idLabel = new JLabel("   ");
        JButton button = new JButton("Show ID");
        button.addActionListener(e -> {
            idLabel.setText(String.valueOf(idNum));
        });

        panel.add(button);
        panel.add(idLabel);
        add(panel);
        setLocationRelativeTo(null);
        pack();
    }

    public void setID(int idNum) {
        this.idNum = idNum;
    }
}

可以通过使用 StaffStaffBooking 共享的模型 class 来进一步改进结构:

public class Staff extends JFrame /*implements ActionListener*/ {

    private final JPanel panel = new JPanel();
    private final JLabel idLabel = new JLabel("ID:  ", SwingConstants.RIGHT);
    private final JTextField id = new JTextField("",5);
    private final JButton button = new JButton("Submit");

    public Staff() {
        Model model = new Model();
        StaffBooking staffBooking = new StaffBooking(model);
        setTitle("Staff");
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setLocationRelativeTo(null);
        panel.add(idLabel);
        panel.add(id);
        panel.add(button);
        add(panel);
        button.addActionListener(e -> {
            if(! id.getText().isEmpty()) {
                int idNum = Integer.parseInt(id.getText());
                model.setID(idNum);
                staffBooking.setVisible(true);
            }
        });

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new Staff();
    }
}

class StaffBooking extends JDialog {

    public StaffBooking(Model model) {
        JPanel panel = new JPanel();
        JLabel idLabel = new JLabel("   ");
        JButton button = new JButton("Show ID");
        button.addActionListener(e -> {
            idLabel.setText(String.valueOf(model.getID()));
        });

        panel.add(button);
        panel.add(idLabel);
        add(panel);
        setLocationRelativeTo(null);
        pack();
    }
}

class Model{

    private int idNum;

    public int getID() {
        return idNum;
    }

    public void setID(int idNum) {
        this.idNum = idNum;
    }
}