java eclipse计算器_用eclipse制作计算器。

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

我做的计算器代码怎样加上老师给的代码才能正常使用?

我做的计算器代码:

package aa;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import javax.swing.JTextField;

import javax.swing.JLabel;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class ad extends JFrame {

private JPanel contentPane;

private JTextField textField;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

ad frame = new ad();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the frame.

*/

public ad() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 381, 241);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

textField = new JTextField();

textField.setBounds(10, 10, 303, 21);

contentPane.add(textField);

textField.setColumns(10);

JButton button = new JButton("7");

button.setBounds(20, 61, 71, 23);

contentPane.add(button);

JButton button_1 = new JButton("8");

button_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

}

});

button_1.setBounds(98, 61, 60, 23);

contentPane.add(button_1);

JButton button_2 = new JButton("9");

button_2.setBounds(168, 61, 75, 23);

contentPane.add(button_2);

JButton button_3 = new JButton("4");

button_3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

}

});

button_3.setBounds(20, 94, 71, 23);

contentPane.add(button_3);

JButton button_4 = new JButton("1");

button_4.setBounds(20, 127, 71, 23);

contentPane.add(button_4);

JButton button_5 = new JButton(".\r\n");

button_5.setBounds(20, 161, 71, 23);

contentPane.add(button_5);

JButton button_6 = new JButton("2");

button_6.setBounds(98, 127, 60, 23);

contentPane.add(button_6);

JButton button_7 = new JButton("5");

button_7.setBounds(98, 94, 60, 23);

contentPane.add(button_7);

JButton button_8 = new JButton("0");

button_8.setBounds(98, 161, 60, 23);

contentPane.add(button_8);

JButton button_9 = new JButton("3");

button_9.setBounds(168, 127, 75, 23);

contentPane.add(button_9);

JButton button_10 = new JButton("6");

button_10.setBounds(168, 94, 75, 23);

contentPane.add(button_10);

JButton button_11 = new JButton("=");

button_11.setBounds(168, 161, 75, 23);

contentPane.add(button_11);

JButton button_12 = new JButton("+");

button_12.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

}

});

button_12.setBounds(265, 61, 75, 23);

contentPane.add(button_12);

JButton button_13 = new JButton("-");

button_13.setBounds(265, 94, 75, 23);

contentPane.add(button_13);

JButton btnX = new JButton("x");

btnX.setBounds(265, 127, 75, 23);

contentPane.add(btnX);

JButton button_15 = new JButton("/");

button_15.setBounds(265, 161, 75, 23);

contentPane.add(button_15);

}

}

老师给的代码;

private ActionListener insert = new InsertAction();

private ActionListener command = new CommandAction();

private double result = 0;

private String lastCommand = "=";

private boolean start = true;

private class InsertAction implements ActionListener {

public void actionPerformed(ActionEvent e) {

String input = e.getActionCommand();

String text = display.getText();

if (start) {

display.setText("");

start = false;

}

if (text.startsWith(".")) {

display.setText("0" + display.getText() + input);

} else if (text.startsWith("-0.") || text.startsWith("0.")) {

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

} else if (text.startsWith("-0")) {

display.setText("-" + input);

} else if (text.startsWith("0")) {

display.setText(input);

} else {

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

}

}

}

private class CommandAction implements ActionListener {

public void actionPerformed(ActionEvent e) {

String command = e.getActionCommand();

if (start) {

if (command.equals("-")) {

display.setText(command);

start = false;

} else {

lastCommand = command;

}

} else {

calculate(Double.parseDouble(display.getText()));

lastCommand = command;

start = true;

}

}

}

public void calculate(double x) {

char operator = lastCommand.charAt(0);

switch (operator) {

case '+':

result += x;

break;

case '-':

result -= x;

break;

case '*':

result *= x;

break;

case '/':

result /= x;

break;

case '=':

result = x;

break;

}

display.setText("" + result);

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值