gpt4 book ai didi

java - 如何使 txPanel 仅在单击 'enter pin ok' 按钮后才可见?

转载 作者:行者123 更新时间:2023-11-29 03:48:48 25 4
gpt4 key购买 nike

public class ATMgui extends JFrame implements ActionListener {

/**
*
*/
private static final long serialVersionUID = 1L;
public static final int WIDTH = 500;
public static final int HEIGHT = 200;
private ATMbizlogic theBLU;// short for the Business Logic Unit
public JLabel totalBalanceLabel;
public JTextField withdrawTextField;
public JTextField depositTextField;
public JTextField pinTextField;

/**
* Creates a new instance of ATMgui
*/
public ATMgui() {
setTitle("ATM Transactions");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

Container contentPane = getContentPane();
contentPane.setBackground(Color.BLACK);
contentPane.setLayout(new BorderLayout());


// Do the panel for the rest stop
JLabel start = new JLabel("Welcome To Your Account", JLabel.CENTER);
Font curFont = start.getFont();
start.setFont(new Font(curFont.getFontName(), curFont.getStyle(), 25));
start.setForeground(Color.BLUE);
start.setOpaque(true);
start.setBackground(Color.BLACK);


pinTextField = new JTextField();
JLabel pinLabel = new JLabel("Enter your PIN below:", JLabel.CENTER);
pinLabel.setForeground(Color.RED);
pinLabel.setOpaque(true);
pinLabel.setBackground(Color.WHITE);



JButton pinButton = new JButton("Enter Pin OK");
pinButton.addActionListener(this);
pinButton.setBackground(Color.red);

JPanel pinPanel = new JPanel();
pinPanel.setLayout(new GridLayout(3, 1, 100, 0));

pinPanel.add(pinLabel);
pinPanel.add(pinTextField);
pinPanel.add(pinButton);

contentPane.add(pinPanel, BorderLayout.WEST);


JPanel headingPanel = new JPanel();
headingPanel.setLayout(new GridLayout());
headingPanel.add(start);

contentPane.add(headingPanel, BorderLayout.NORTH);


// Do the panel for the amount & type of transactions
withdrawTextField = new JTextField();
JLabel withdrawLabel = new JLabel("Withdraw (0.00)", JLabel.CENTER);
withdrawLabel.setForeground(Color.RED);
withdrawLabel.setOpaque(true);
withdrawLabel.setBackground(Color.WHITE);



depositTextField = new JTextField();
JLabel depositLabel = new JLabel("Deposit (0.00)", JLabel.CENTER);
depositLabel.setForeground(Color.RED);
depositLabel.setOpaque(true);
depositLabel.setBackground(Color.WHITE);



JButton txButton = new JButton("Transactions OK");
txButton.addActionListener(this);
txButton.setBackground(Color.red);




JPanel txPanel = new JPanel();
txPanel.setLayout(new GridLayout(5, 1, 30, 0));

txPanel.add(withdrawLabel);
txPanel.add(withdrawTextField);
txPanel.add(depositLabel);
txPanel.add(depositTextField);
txPanel.add(txButton);

contentPane.add(txPanel, BorderLayout.EAST);
txPanel.setVisible(true);


totalBalanceLabel = new JLabel("Your balance after transactions: ", JLabel.CENTER);
totalBalanceLabel.setForeground(Color.BLUE);
totalBalanceLabel.setOpaque(true);
totalBalanceLabel.setBackground(Color.BLACK);


contentPane.add(totalBalanceLabel, BorderLayout.SOUTH);


theBLU = new ATMbizlogic();
}

public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();

// Container contentPane = getContentPane();

if (actionCommand.equals("Transactions OK")) {
try {
double deposit = Double.parseDouble(depositTextField.getText().trim());
double withdraw = Double.parseDouble(withdrawTextField.getText().trim());
theBLU.computeBalance(withdraw, deposit);
totalBalanceLabel.setText("Your balance after transactions: " + theBLU.getBalance());

} catch (ATMexception ex) {
totalBalanceLabel.setText("Error: " + ex.getMessage());
} catch (Exception ex) {
totalBalanceLabel.setText("Error in deposit or withdraw amount: " + ex.getMessage());
}
} else if (actionCommand.equals("Enter Pin OK")) {

try {

double pin = Double.parseDouble(pinTextField.getText().trim());
theBLU.checkPin(pin);

totalBalanceLabel.setText("Your balance after transactions: " + theBLU.getBalance());



} catch (ATMexception ex) {
totalBalanceLabel.setText("Error: " + ex.getMessage());
} catch (Exception ex) {
totalBalanceLabel.setText("Error in pin: " + ex.getMessage());

}
} else {
System.out.println("Error in button interface.");
}
}

public static void main(String[] args) {
ATMgui gui = new ATMgui();
gui.setVisible(true);
}
}

最佳答案

我不认为这是为按钮实现 ActionListeners 的正确方法。

public void actionPerformed(ActionEvent e) 
{
String actionCommand = e.getActionCommand();

// Container contentPane = getContentPane();
if (actionCommand.equals("Transactions OK"))
else ...
}

在方法 actionPerformed 中使用 if-else stamements,每次按下任何按钮时,您的程序都必须检查要调用的监听器,这样一来,您的代码就不容易编辑和重用。此外,GUI 容器充当事件的接收者,那么你应该避免

pinButton.addActionListener(this);

尝试为每个按钮实现自己的内部类,如下所示:

JButton pinButton = new JButton("Enter Pin OK");
pinButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae){
//enter here your action
txPanel.setVisible(true);
}
});

通过这种方式,您不需要为您的类实现 ActionListener 接口(interface),因为您正在为您的 pinButton 实现接口(interface)的内部实例。检查 SO 的 this old question。此外,您应该避免在类构造函数中实现所有 GUI 元素,最好在单独的方法中实现 GUI,例如 createAndShowGui(),并在构造函数中调用它,以尊重 Java Swing conventions并在与应用程序主线程不同的名为 Event Dispatch Thread 的不同线程中运行 Swing 组件。读取 this question

然后在 createAndShowGui() 方法中包含 txPanel.setVisible(false);

请记住,Swing 组件不是线程安全的。

关于java - 如何使 txPanel 仅在单击 'enter pin ok' 按钮后才可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9680017/

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