java swing atm机
时间: 2023-12-14 21:05:08 浏览: 36
Java Swing 是一个用于构建 GUI 的 Java 库,可以用它来构建 ATM 机的用户界面。以下是一个简单的 Java Swing ATM 机示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ATM extends JFrame implements ActionListener {
private JLabel headerLabel, accountLabel, pinLabel;
private JTextField accountTextField, pinTextField;
private JButton loginButton;
public ATM() {
setTitle("ATM Machine");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
headerLabel = new JLabel("ATM Machine", JLabel.CENTER);
accountLabel = new JLabel("Account Number:", JLabel.LEFT);
pinLabel = new JLabel("PIN:", JLabel.LEFT);
accountTextField = new JTextField();
pinTextField = new JPasswordField();
loginButton = new JButton("Login");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
panel.add(accountLabel);
panel.add(accountTextField);
panel.add(pinLabel);
panel.add(pinTextField);
panel.add(new JLabel(""));
panel.add(loginButton);
loginButton.addActionListener(this);
add(headerLabel, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String accountNumber = accountTextField.getText();
String pin = pinTextField.getText();
// TODO: Implement login functionality
}
}
public static void main(String[] args) {
new ATM();
}
}
```
这段代码创建了一个包含账户号码和 PIN 码输入框以及一个登录按钮的窗口。当用户点击登录按钮时,程序将获取输入框中的值,并根据这些值执行相应的操作。你可以根据自己的需要来完善这个程序,例如添加转账、取款、查询余额等功能。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)