對使用 Java 非常陌生,花了幾個小時尋找解決方案,但我不知道如何JTextField使用按鈕獲取 a 收集的輸入,ActionListener然后將其用于可以接收的 getter 方法,idNum以便我可以在另一個類中使用輸入。
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;
}
}
uj5u.com熱心網友回復:
我相信您正在嘗試做的是這樣的事情:
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;
}
}
可以通過使用在Staff和之間共享的模型類來進一步改進結構StaffBooking:
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;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/406412.html
標籤:
