类似java制作计算器的游戏_求一java小程序,,像计算器,小游戏,或者什么的。...

展开全部

public class Test {

double result = 0;

boolean start = true;

String lastCommand = "+";// 最终的符号

Font font = new Font(null, Font.PLAIN, 25);

Font font2 = new Font(null, Font.PLAIN, 20);

JFrame frame = new JFrame("简易计算器32313133353236313431303231363533e4b893e5b19e31333335323532");

JPanel panel1 = new JPanel();

JPanel panel2 = new JPanel();

JPanel panel3 = new JPanel();

JPanel panel4 = new JPanel();

JPanel panel5 = new JPanel();

JTextField text = new JTextField("");

JButton b0 = new JButton("0");

JButton b1 = new JButton("1");

JButton b2 = new JButton("2");

JButton b3 = new JButton("3");

JButton b4 = new JButton("4");

JButton b5 = new JButton("5");

JButton b6 = new JButton("6");

JButton b7 = new JButton("7");

JButton b8 = new JButton("8");

JButton b9 = new JButton("9");

JButton a1 = new JButton("+");

JButton a2 = new JButton("-");

JButton a3 = new JButton("*");

JButton a4 = new JButton("/");

JButton a5 = new JButton("=");

JButton a6 = new JButton(".");

JButton a7 = new JButton("清零");

JButton a8 = new JButton("取消");

JButton a9 = new JButton("sqrt");

JButton a10 = new JButton("+/-");

public void set() {

frame.setSize(400, 350);

frame.setLayout(new GridLayout(5, 1, 2, 2));

panel1.setBackground(Color.black);

panel2.setBackground(Color.red);

panel3.setBackground(Color.blue);

panel4.setBackground(Color.green);

panel5.setBackground(Color.yellow);

panel1.setLayout(new BorderLayout());

panel1.add(text, "Center");

panel2.setLayout(new GridLayout(1, 5, 1, 1));

panel3.setLayout(new GridLayout(1, 5, 1, 1));

panel4.setLayout(new GridLayout(1, 5, 1, 1));

panel5.setLayout(new GridLayout(1, 5, 1, 1));

text.setHorizontalAlignment(JTextField.RIGHT);

b0.setFont(font);

b1.setFont(font);

b2.setFont(font);

b3.setFont(font);

b4.setFont(font);

b5.setFont(font);

b6.setFont(font);

b7.setFont(font);

b8.setFont(font);

b9.setFont(font);

a1.setFont(font);

a2.setFont(font);

a3.setFont(font);

a4.setFont(font);

a5.setFont(font);

a6.setFont(font);

a7.setFont(font2);

a8.setFont(font2);

a9.setFont(font);

a10.setFont(font);

frame.setVisible(true);

}

public void addComponent() {

panel2.add(b7);

panel2.add(b8);

panel2.add(b9);

panel2.add(a4);

panel2.add(a7);

panel3.add(b4);

panel3.add(b5);

panel3.add(b6);

panel3.add(a3);

panel3.add(a8);

panel4.add(b1);

panel4.add(b2);

panel4.add(b3);

panel4.add(a2);

panel4.add(a9);

panel5.add(b0);

panel5.add(a10);

panel5.add(a6);

panel5.add(a1);

panel5.add(a5);

frame.add(panel1);

frame.add(panel2);

frame.add(panel3);

frame.add(panel4);

frame.add(panel5);

}

public void addListener() {

b0.addActionListener(new InsertAction());

b1.addActionListener(new InsertAction());

b2.addActionListener(new InsertAction());

b3.addActionListener(new InsertAction());

b4.addActionListener(new InsertAction());

b5.addActionListener(new InsertAction());

b6.addActionListener(new InsertAction());

b7.addActionListener(new InsertAction());

b8.addActionListener(new InsertAction());

b9.addActionListener(new InsertAction());

a1.addActionListener(new CommandAction());

a2.addActionListener(new CommandAction());

a3.addActionListener(new CommandAction());

a4.addActionListener(new CommandAction());

a5.addActionListener(new CommandAction());

a6.addActionListener(new InsertAction());

a7.addActionListener(new InsertAction());

a8.addActionListener(new InsertAction());

a9.addActionListener(new CommandAction());

a10.addActionListener(new InsertAction());

}

public void go() {

this.set();

this.addComponent();

this.addListener();

}

class InsertAction implements ActionListener {

public void actionPerformed(ActionEvent event1) {

String input = event1.getActionCommand();

if (start) {

text.setText("");

start = false;

if (input.equals("+/-"))

text.setText(text.getText() + "-");

}

if (!input.equals("+/-")) {

if (input.equals("取消")) {

String str = text.getText();

if (str.length() > 0)

text.setText(str.substring(0, str.length() - 1));

} else if (input.equals("清零")) {

text.setText("0");

lastCommand = "+";

start = true;

result = 0;

} else

text.setText(text.getText() + input);

}

}

}

class CommandAction implements ActionListener {

public void actionPerformed(ActionEvent event2) {

String command = event2.getActionCommand();

if (start) {

lastCommand = command;

} else { // Double data=new Double(text.getText());

calculator(Double.parseDouble(text.getText()));

lastCommand = command;

start = true;

}

}

}

public void calculator(double x) {

if (lastCommand.equals("+"))

result += x;

else if (lastCommand.equals("-"))

result -= x;

else if (lastCommand.equals("*"))

result *= x;

else if (lastCommand.equals("/"))

result /= x;

else if (lastCommand.equals("="))

result = x;

else if (lastCommand.equals("sqrt"))

result = Math.sqrt(x);

text.setText("" + result);

}

public static void main(String[] args) {

Test a = new Test();

a.go();

}

}

本回答被提问者和网友采纳

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值