求一个JAVA应用程序编写的简单ATM代码
2个回答
展开全部
import javax.swing.*;
import java.awt.event.*;
public class SimAtm extends JFrame implements java.awt.event.ActionListener{
private static final long serialVersionUID = 1L;
private JButton[] buts = new JButton[4];
private String [] tils = new String[] {"存款", "取款", "查询", "退出"};
private int money = 0;
private JLabel lab = new JLabel();
public SimAtm() {
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(400, 250, 420, 200);
int w = 100;
int h = 30;
lab.setForeground(java.awt.Color.red);
lab.setBounds(0, 50, 400, 50);
for(int i = 0; i < 4; i ++) {
buts[i] = new JButton(tils[i]);
buts[i].addActionListener(this);
buts[i].setBounds(i * w, 0,w, h );
this.add(buts[i]);
}
this.add(lab);
this.setVisible(true);
}
public static void main(String[] args) {
new SimAtm();
}
public void actionPerformed(ActionEvent e) {
Object op = e.getSource();
if( op == buts[0]){
String is = JOptionPane.showInputDialog(null, "输入存款额:");
if(is != null && is.matches("\\d+")) {
int input = Integer.parseInt(is);
money += input;
lab.setText("你已经存入" + input + "元");
}
} else if(op == buts[1] ){
String is = JOptionPane.showInputDialog(null, "输入取款额:");
if(is != null && is.matches("\\d+")) {
int input = Integer.parseInt(is);
input = input >= money ? money : input;
money -= input;
lab.setText("你已经取走" + input + "元");
}
} else if(op == buts[2]) {
JOptionPane.showMessageDialog(null, "当前余额是: " + money);
lab.setText("当前余额: " + money);
} else{
System.exit(0);
}
}
}
import java.awt.event.*;
public class SimAtm extends JFrame implements java.awt.event.ActionListener{
private static final long serialVersionUID = 1L;
private JButton[] buts = new JButton[4];
private String [] tils = new String[] {"存款", "取款", "查询", "退出"};
private int money = 0;
private JLabel lab = new JLabel();
public SimAtm() {
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(400, 250, 420, 200);
int w = 100;
int h = 30;
lab.setForeground(java.awt.Color.red);
lab.setBounds(0, 50, 400, 50);
for(int i = 0; i < 4; i ++) {
buts[i] = new JButton(tils[i]);
buts[i].addActionListener(this);
buts[i].setBounds(i * w, 0,w, h );
this.add(buts[i]);
}
this.add(lab);
this.setVisible(true);
}
public static void main(String[] args) {
new SimAtm();
}
public void actionPerformed(ActionEvent e) {
Object op = e.getSource();
if( op == buts[0]){
String is = JOptionPane.showInputDialog(null, "输入存款额:");
if(is != null && is.matches("\\d+")) {
int input = Integer.parseInt(is);
money += input;
lab.setText("你已经存入" + input + "元");
}
} else if(op == buts[1] ){
String is = JOptionPane.showInputDialog(null, "输入取款额:");
if(is != null && is.matches("\\d+")) {
int input = Integer.parseInt(is);
input = input >= money ? money : input;
money -= input;
lab.setText("你已经取走" + input + "元");
}
} else if(op == buts[2]) {
JOptionPane.showMessageDialog(null, "当前余额是: " + money);
lab.setText("当前余额: " + money);
} else{
System.exit(0);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询