btnStraight = new JButton("Straight");
btnStraight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
/* Delay if statement until an ArrayList reaches a certain size */
if(RouletteHelperMethods.doBet("Straight") == 1){
textArea.setText("Congrats, you won!");
} else {
textArea.setText("Sorry, you lost.");
}
}
});
用户应该单击一个按钮,然后被告知单击 JTextFields 将它们添加到 ArrayList。如何延迟 if 语句直到发生这种情况?
最佳答案
您需要重新考虑程序的运行方式,因为您编写的代码就像编写了线性控制台程序一样,但事实并非如此。相反,您正在编写一个事件驱动 GUI 程序,因此您应该对事件使用react并改变状态,而不是让代码“等待”。
如果您需要有关此问题的更多详细信息,请充实您的问题,包括提供有关您的程序应该执行的操作、您的 ArrayList 含义的更多信息,并包括在此处(而不是在链接中)提供有效的 minimal example program (请阅读链接)。
我试图满足您的逻辑需求,并想出了一个创建一堆 JTextField 的小程序。单击时,它们保存的文本将添加到 JTextArea,并且字段将添加到选定字段 ArrayList。当添加到列表中的字段数为 4 时,逻辑将运行。这是从添加到所有文本字段的 FocusListener 调用的。例如:
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
@SuppressWarnings("serial")
public class EventDrivenGui extends JPanel {
private static final int FIELD_SIZE = 10;
private static final int SELECTED_FIELD_SIZE = 4;
protected static final Color SELECTED_COLOR = Color.PINK;
private JButton btnStraight;
private JTextArea textArea = new JTextArea(20, 40);
private List<JTextField> selectedFieldList = new ArrayList<>();
private List<JTextField> fieldList = new ArrayList<>();
public EventDrivenGui() {
btnStraight = new JButton("Straight");
btnStraight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (selectedFieldList.size() > 0) {
return;
}
for (JTextField textField : fieldList) {
textField.setEnabled(true);
}
JOptionPane.showMessageDialog(btnStraight, "Please Select a text field",
"Make Selection", JOptionPane.INFORMATION_MESSAGE);
}
});
textArea.setFocusable(false);
JPanel fieldPanel = new JPanel(new GridLayout(0, 1, 5, 5));
for (int i = 0; i < FIELD_SIZE; i++) {
JTextField field = new JTextField("Field " + (i + 1), 10);
field.setEnabled(false); // so they can't select it prematurely
fieldList.add(field);
fieldPanel.add(field);
field.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
JTextField tf = (JTextField) e.getComponent();
if (!selectedFieldList.contains(tf)) {
textArea.append(tf.getText() + "\n");
tf.setBackground(SELECTED_COLOR);
selectedFieldList.add(tf);
if (selectedFieldList.size() >= SELECTED_FIELD_SIZE) {
if (RouletteHelperMethods.doBet("Straight") == 1) {
textArea.append("Congrats, you won!\n");
} else {
textArea.append("Sorry, you lost.\n");
}
reset();
}
}
}
});
}
add(btnStraight);
add(new JScrollPane(textArea));
add(fieldPanel);
}
protected void reset() {
selectedFieldList.clear();
for (JTextField jTextField : fieldList) {
jTextField.setEnabled(false);
jTextField.setBackground(null);
}
}
private static void createAndShowGui() {
EventDrivenGui mainPanel = new EventDrivenGui();
JFrame frame = new JFrame("EventDrivenGui");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
class RouletteHelperMethods {
public static int doBet(String bet) {
return Math.random() > 0.5 ? 1 : 0;
}
}
关于java - 如何等待达到某个值才能继续该方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37635211/