标签:btn 创建 tca and row menu null menuitem alc
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TestCalculator1 extends JFrame implements ActionListener {
private JTextField outputField = new JTextField();
private JButton button;
private double result;
private String lastCommand = "";
private String[] buttonName = { "MC","MR","MS","M+","M-",
"<-", "CE","C","±", "√",
"7", "8", "9", "/", "%",
"4", "5", "6", "*", "1/x",
"1", "2", "3", "-", "=" ,
"0",".","+"};
private JMenuBar bar;
private JMenu menu_file, menu_help;
private JMenuItem item_log,item_close;
public static void main(String[] args) {
new TestCalculator1();
}
public TestCalculator1() {
this.setTitle("计算器");
this.setLocationRelativeTo(null); //居中显示
this.setSize(228,324);
this.add(getNorth(), BorderLayout.NORTH);
this.add(getCenter(), BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initMenu();
this.setVisible(true);
}
public void initMenu(){
bar = new JMenuBar(); //创建菜单条
menu_file = new JMenu("文件"); //创建菜单
menu_help = new JMenu("帮助");
bar.add(menu_file); //将菜单加入到菜单栏中
bar.add(menu_help);
item_log = new JMenuItem("历史记录"); //创建菜单项
item_close = new JMenuItem("关闭");
menu_file.add(item_log); //将菜单项加入到菜单里
menu_file.add(item_close);
this.setJMenuBar(bar); //将菜单条加入窗体
}
public JPanel getNorth() {
JPanel jpanel = new JPanel(new GridLayout());
jpanel.add(outputField);
return jpanel;
}
public JPanel getCenter() {
JPanel jpanel = new JPanel(new GridLayout(0, 5));
for (String btncode : buttonName) {
jpanel.add(button = new JButton(btncode));
button.addActionListener(this);
}
return jpanel;
}
public void actionPerformed(ActionEvent e) // 监听按键
{
String command = e.getActionCommand();
if (lastCommand.equals("=")) {
this.result = 0;
outputField.setText("");
lastCommand = "";
}
if (command.equals("C")) {
outputField.setText("");
this.result = 0;
} else if (command.equals("<-")) {
this.calculator(command);
} else if (command.equals("%")) {
this.calculator(command);
} else if (command.equals("/")) {
this.calculator(command);
} else if (command.equals("+")) {
this.calculator(command);
} else if (command.equals("-")) {
this.calculator(command);
} else if (command.equals("*")) {
this.calculator(command);
} else if (command.equals("=")) {
this.calculator(command);
} else {
outputField.setText(outputField.getText() + command);
}
}
public void calculator(String currentCommand) {
try {
if (!lastCommand.equals("")) {
switch (lastCommand) {
case "+":
if (!outputField.getText().equals("")) {
result += Double.parseDouble(outputField.getText());
}
break;
case "-":
if (!outputField.getText().equals("")) {
result -= Double.parseDouble(outputField.getText());
}
break;
case "*":
if (!outputField.getText().equals("")) {
result *= Double.parseDouble(outputField.getText());
}
break;
case "/":
if (!outputField.getText().equals("")) {
if (!outputField.getText().equals("0")) {
result /= Double.parseDouble(outputField.getText());
} else {
lastCommand = "=";
outputField.setText("除数不能为零");
throw new Exception("除数不能为零");
}
}
break;
default:
if (!outputField.getText().equals("")) {
result = Double.parseDouble(outputField.getText());
}
break;
}
} else {
if (outputField.getText().equals("")) {
result = 0;
} else {
result = Double.parseDouble(outputField.getText());
}
}
if (!currentCommand.equals("<-")) {
this.lastCommand = currentCommand;
}
if(!currentCommand.equals("")){
System.out.println("currentCommand: " + currentCommand);
switch (currentCommand) {
case "=":
outputField.setText(result + "");
break;
case "%":
if (!outputField.getText().equals("")) {
result = (Double.parseDouble(outputField.getText()) / 100);
outputField.setText(result + "");
}
break;
case "<-":
if (!outputField.getText().equals("")) {
outputField.setText(outputField.getText().substring(0,
outputField.getText().length() - 1));
if (!outputField.getText().equals("")) {
result = Double.parseDouble(outputField.getText());
}
}
break;
default:
outputField.setText("");
break;
}
}
} catch (Exception e) {
//e.printStackTrace();
}
}
}
标签:btn 创建 tca and row menu null menuitem alc
原文地址:https://www.cnblogs.com/vickytjh/p/9095205.html