gpt4 book ai didi

java - 执行的操作不改变文本

转载 作者:行者123 更新时间:2023-12-01 18:05:46 24 4
gpt4 key购买 nike

我有一个 actionPerformed ,其中一个 if/else 是,如果用户按下“A”键,它会将文本设置为不同的内容。程序不会在按“A”后设置文本,而是跳过设置文本并向下移动到下面的 if 语句。我的问题是,如何让程序在 if 语句之前设置文本?我的代码如下,谢谢!

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

public class ATMProject extends JPanel implements ActionListener {
private JPanel mainPanel = null;
private JPanel btnPanel = null;
private JPanel userBtns = null;
private JTextArea textArea = null;
private JPanel keyPanel = null;
private JTextField numField = null;
private JPanel numpadPanel = null;
private JButton[] userButtons = null;
private JButton[] keypadButtons = null;
private String[] btnPanelbtns = { "A", "B", "C" };
private String[] numpadPanelbtns = { "7", "8", "9", "4", "5", "6", "1", "2", "3", "0", ".", "CE" };
private boolean isuserButtonPressed = false;
private boolean isAccEntered = true;
private FileReader file = null;
private String custAcc;
private String custPin;
private String userInputAcc;
private String userInputPin;

public ATMProject() {
super();
mainPanel = new JPanel();
this.setLayout(new BorderLayout());
this.add(mainPanel);

btnPanel = new JPanel();
btnPanel.setLayout(new GridLayout(3, 1));
this.add(btnPanel, BorderLayout.EAST);

textArea = new JTextArea();
this.add(textArea, BorderLayout.CENTER);

keyPanel = new JPanel();
keyPanel.setLayout(new BorderLayout());
this.add(keyPanel, BorderLayout.WEST);

numpadPanel = new JPanel();
numpadPanel.setLayout(new GridLayout(4, 3));
keyPanel.add(numpadPanel, BorderLayout.CENTER);

numField = new JTextField();
keyPanel.add(numField, BorderLayout.NORTH);

userButtons = new JButton[btnPanelbtns.length];
for (int i = 0; i < userButtons.length; i++) {
userButtons[i] = new JButton(btnPanelbtns[i]);
userButtons[i].addActionListener(this);
btnPanel.add(userButtons[i]);
}

keypadButtons = new JButton[numpadPanelbtns.length];
for (int i = 0; i < numpadPanelbtns.length; i++) {
keypadButtons[i] = new JButton(numpadPanelbtns[i]);
keypadButtons[i].addActionListener(this);
numpadPanel.add(keypadButtons[i]);
}

textArea.setText("Enter customer number: --> \nPress A when done \nA = OK");
userInputAcc = numField.getText();

// reading
try {
file = new FileReader("ConfidentialCustInfo.txt");
Scanner input = new Scanner(file);
custAcc = input.next();
custPin = input.next();
System.out.println(custAcc);
System.out.println(custPin);
input.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}

}

@Override
public void actionPerformed(ActionEvent e) {

if (e.getActionCommand().equals("CE")) {
numField.setText("");
} else if (e.getActionCommand().equals("1")) {
numField.setText(numField.getText() + "1");
} else if (e.getActionCommand().equals("2")) {
numField.setText(numField.getText() + "2");
} else if (e.getActionCommand().equals("3")) {
numField.setText(numField.getText() + "3");
} else if (e.getActionCommand().equals("4")) {
numField.setText(numField.getText() + "4");
} else if (e.getActionCommand().equals("5")) {
numField.setText(numField.getText() + "5");
} else if (e.getActionCommand().equals("6")) {
numField.setText(numField.getText() + "6");
} else if (e.getActionCommand().equals("7")) {
numField.setText(numField.getText() + "7");
} else if (e.getActionCommand().equals("8")) {
numField.setText(numField.getText() + "8");
} else if (e.getActionCommand().equals("9")) {
numField.setText(numField.getText() + "9");
} else if (e.getActionCommand().equals("A")) {
if (numField.getText().isEmpty()) {
textArea.setText("error");
} else {
numField.setText("");
textArea.setText("Enter PIN: \n A = OK");
userInputPin = (numField.getText());

}
}
}

public static void main(String[] args) {
MyFrame mf = new MyFrame();
}

}

最佳答案

您需要为程序提供一个状态变量,该变量将告诉程序它处于什么状态,然后允许监听器根据该状态做出不同的响应。

例如,您可以给它一个或多个 boolean 变量,例如

private boolean enteringAcctNumber = true;

然后在 ActionListener 中放置一个 if block 来检查此 boolean 值并做出适当的响应。

例如

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class ATMProj2 extends JPanel {
private static final int COLS = 30;
private static final String ACCOUNT_PROMPT = "Enter Account Number";
protected static final String ENTER_PIN = "Account number accepted. Enter PIN";
protected static final String PIN_ACCEPTED = "PIN has been accepted. Have fun.";
private boolean enteringAcctNumber = true;
private JTextField textField = new JTextField(COLS);
private JTextArea textArea = new JTextArea(6, COLS);
private JButton okButton = new JButton("OK");

public ATMProj2() {
textArea.setText(ACCOUNT_PROMPT);
textArea.setFocusable(false); // so user can't mess with it
okButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// test state variable, and base behavior on its state
if (enteringAcctNumber) {
textArea.setText(ENTER_PIN);
textField.setText("");
enteringAcctNumber = false; // ok now not entering account
} else {
textArea.setText(PIN_ACCEPTED);
}
}
});

JPanel btnPanel = new JPanel();
btnPanel.add(okButton);

setLayout(new BorderLayout());
add(textField, BorderLayout.PAGE_START);
add(new JScrollPane(textArea), BorderLayout.CENTER);
add(btnPanel, BorderLayout.PAGE_END);
}

private static void createAndShowGui() {
ATMProj2 mainPanel = new ATMProj2();

JFrame frame = new JFrame("ATMProj2");
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();
});
}
}

关于java - 执行的操作不改变文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36550765/

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