gpt4 book ai didi

java - 如何使用 Action 监听器中设置的变量

转载 作者:行者123 更新时间:2023-12-02 00:00:49 25 4
gpt4 key购买 nike

我在理解如何使用 Action 监听器来更改变量的值时遇到问题。

在我的程序中,我需要存储用户通过选择一些单选按钮所做的选择。

我有一个带有卡片布局的主类,然后是几个类,每个类都是不同的面板。在其中一个面板中,我有一些单选按钮,其中一个 Action 监听器作为内部类。

当我尝试在主类中打印变量值时,它会在用户做出选择之前立即打印,因为我实例化面板类并从中获取变量,所以我在更改变量之前就获取了变量由用户。

我知道我不应该用Java以线性方式思考,但是我如何确保在用户更改变量之后而不是之前获取变量?我将无法做到这一点,对吗?我知道我的想法有一些缺陷,但我已经很久没有好好 sleep 了,我就是无法解决这个问题。

public class Screen3 extends JPanel{

JRadioButton addition = new JRadioButton("Addition");
JRadioButton subtraction = new JRadioButton("Subtraction");
JRadioButton multiplication = new JRadioButton("Multiplication");
JRadioButton division = new JRadioButton("Division");
JRadioButton all = new JRadioButton("All");

JRadioButton single = new JRadioButton("Single");
JRadioButton two = new JRadioButton("Double");
JRadioButton triple = new JRadioButton("Triple");
JRadioButton mix = new JRadioButton("Mix");

JRadioButton five = new JRadioButton("5");
JRadioButton ten = new JRadioButton("10");

private int type, digit, rounds;

public Screen3() {

JPanel firstButtonPanel = new JPanel();
JPanel secondButtonPanel = new JPanel();
ButtonGroup myFirstGroup = new ButtonGroup();
ButtonGroup mySecondGroup = new ButtonGroup();

myFirstGroup.add(addition);
myFirstGroup.add(subtraction);
myFirstGroup.add(multiplication);
myFirstGroup.add(division);
//myFirstGroup.add(all);

mySecondGroup.add(single);
mySecondGroup.add(two);
mySecondGroup.add(triple);
//mySecondGroup.add(mix);

firstButtonPanel.setLayout(new FlowLayout());
firstButtonPanel.add(addition);
firstButtonPanel.add(subtraction);
firstButtonPanel.add(multiplication);
firstButtonPanel.add(division);
//firstButtonPanel.add(all);

secondButtonPanel.setLayout(new FlowLayout());
secondButtonPanel.add(single);
secondButtonPanel.add(two);
secondButtonPanel.add(triple);
//secondButtonPanel.add(mix);

JPanel buttons = new JPanel();
buttons.setLayout(new BorderLayout());
buttons.add(selectionLabel, BorderLayout.NORTH);
buttons.add(firstButtonPanel, BorderLayout.CENTER);
buttons.add(secondButtonPanel, BorderLayout.SOUTH);

ButtonGroup myThirdGroup = new ButtonGroup();
JPanel endButtons = new JPanel();

myThirdGroup.add(five);
myThirdGroup.add(ten);

endButtons.add(five);
endButtons.add(ten);

endPanel.setLayout(new BorderLayout());
endPanel.add(rounds, BorderLayout.NORTH);
endPanel.add(endButtons, BorderLayout.CENTER);

setLayout(new BorderLayout());
add(buttons, BorderLayout.NORTH);

Selection sn = new Selection();

addition.addActionListener(sn);
subtraction.addActionListener(sn);
multiplication.addActionListener(sn);
division.addActionListener(sn);

single.addActionListener(sn);
two.addActionListener(sn);
triple.addActionListener(sn);

five.addActionListener(sn);
ten.addActionListener(sn);
}

public int getType() {
return type;
}

public int getDigit() {
return digit;
}

public int getRounds() {
return rounds;
}

public class Selection implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(addition.isSelected()) {
type = 1;
}
else if(subtraction.isSelected()) {
type = 2;
}
else if(multiplication.isSelected())
type = 3;
else if(division.isSelected())
type = 4;
//else if(all.isSelected())
//type = 5;

if(single.isSelected()) {
digit = 1;
System.out.println("single");
}
else if(two.isSelected())
digit = 2;
else if(triple.isSelected())
digit = 3;

if(five.isSelected())
rounds = 5;
else if(ten.isSelected())
rounds = 10;
}
}

}

这是主类:

public class Driver {

public JFrame frame = new JFrame("Math Game");

public JPanel screens = new JPanel(new CardLayout());

int digit = 1;
int rounds = 1;
int type = 1;

Driver() {

}

public void show() {

JPanel buttonPanel = new JPanel();
JButton next = new JButton("Next");
JButton previous = new JButton("Previous");
buttonPanel.add(previous);
buttonPanel.add(next);

Screen1 screen1 = new Screen1();
Screen2 screen2 = new Screen2();
Screen3 screen3 = new Screen3();

screens.add(screen1, "welcome");
screens.add(screen2, "next");
screens.add(screen3, "selection");

frame.add(screens);
frame.add(buttonPanel, BorderLayout.PAGE_END);
frame.setSize(400, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

next.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
CardLayout cl = (CardLayout)(screens.getLayout());
cl.next(screens);
}
});

previous.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
CardLayout cl = (CardLayout)(screens.getLayout());
cl.previous(screens);
}
});
}

public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Driver dr = new Driver();
dr.show();
}
});
}

}

我只是尝试 System.out.println(screen3.getType()); 的测试打印在 show() 或 main 中

最佳答案

使用 JOptionPane/JDialog 其中有 modality .

阅读How to Make Dialogs

在示例中,这里仅在JDialog关闭后打印:

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JDialog jd = new JDialog();
jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
jd.setModal(true);
jd.pack();
jd.setVisible(true);

System.out.println("Here");
}
});
}

在此示例中,此处仅在 JOptionPane 关闭后打印:

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JPanel panel=new JPanel();
panel.add(new JLabel("Hello, world!"));

JOptionPane.showMessageDialog(null, panel, "Panel Message",JOptionPane.PLAIN_MESSAGE);

System.out.println("Here");
}
});
}

I know I should not think in a linear manner with Java, but how can I make sure that I fetch the variable after it has been changed by the user and not before?

使用模式JDialog/JOptionPane后,您只需使用公共(public)getters来访问类实例中包含的变量:

public class Test {
public static void main(String[] args) {
X x= new X();//will only return after dialog closed

System.out.println(x.getY());
}
}

class X {

private int y=0;//will be assigned during dialog/joptionpanes life span

public X() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
//creates and shows the modal dialog/optionpane which will allow modification of variable y through some input/controls
}
});
}

public int getY() {
return y;
}
}

关于java - 如何使用 Action 监听器中设置的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14895768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com