该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
我做的计算器代码怎样加上老师给的代码才能正常使用?
我做的计算器代码:
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);
}