大家好这是我的第一篇文章!
我正在使用这个计算器项目作为自学 java 的工具。
我通常试图重现 Microsoft Windows 7 计算器程序。
到目前为止一切顺利,所有按钮都按预期的方式工作,除了 Microsoft Calculator 中更复杂的按钮功能之外,一切都几乎相同。
这是我的问题,在使用以下代码为 JMenuBar 实现动作侦听器/事件后
String menuText = ((JMenuItem) evt.getSource()).getText();
if(menuText.equals("info")){
System.out.println("it works");
JOptionPane.showMessageDialog(null, "aha it works");
我周围的一切都崩溃了!
这是完整的代码
public class Calculator extends JFrame implements ActionListener {
JButton backSpace, clear, one, two, three, four, five, six, seven, eight,
nine, zero, period, devision, modulas, subtraction, addition,
negative, equals;
Font myFont = new Font("ARIAL", Font.BOLD, 40);
JTextArea answerBox = new JTextArea("");
static double numberOne = 0;
static double numberTwo = 0;
static double result;
static String operator = null;
public Calculator() {
super("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// set up components
addition = new JButton("+");
backSpace = new JButton("BACKSPACE");
clear = new JButton("CLEAR");
one = new JButton("1");
two = new JButton("2");
three = new JButton("3");
four = new JButton("4");
five = new JButton("5");
six = new JButton("6");
seven = new JButton("7");
eight = new JButton("8");
nine = new JButton("9");
zero = new JButton("0");
period = new JButton(".");
devision = new JButton("/");
modulas = new JButton("*");
subtraction = new JButton("-");
negative = new JButton("-N");
equals = new JButton("=");
// set up answerbox
answerBox.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
answerBox.setFont(myFont);
answerBox.setEditable(false);
// set up actionListener
backSpace.addActionListener(this);
clear.addActionListener(this);
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
zero.addActionListener(this);
period.addActionListener(this);
devision.addActionListener(this);
addition.addActionListener(this);
modulas.addActionListener(this);
subtraction.addActionListener(this);
negative.addActionListener(this);
equals.addActionListener(this);
// menu
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("About");
JMenuItem info = new JMenuItem("Info");
info.addActionListener(this);
menu.add(info);
menuBar.add(menu);
setJMenuBar(menuBar);
// set up layout
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
JPanel pane = new JPanel();
pane.setLayout(gridBag);
// default
constraints.fill = GridBagConstraints.BOTH;
constraints.anchor = GridBagConstraints.WEST;
constraints.gridy = 0;
constraints.gridx = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 10;
constraints.weighty = 10;
constraints.insets = new Insets(2, 2, 2, 2);
// answer box
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 6;
answerBox.append("0");
gridBag.setConstraints(answerBox, constraints);
pane.add(answerBox);
// backSPace
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 3;
gridBag.setConstraints(backSpace, constraints);
pane.add(backSpace);
// clear
constraints.anchor = GridBagConstraints.EAST;
constraints.gridx = 3;
constraints.gridy = 1;
constraints.weightx = 10;
gridBag.setConstraints(clear, constraints);
pane.add(clear);
// nine
constraints.gridwidth = 1;
constraints.weightx = 1;
constraints.gridx = 0;
constraints.gridy = 3;
gridBag.setConstraints(nine, constraints);
pane.add(nine);
// eight
constraints.gridx = 1;
constraints.gridy = 3;
gridBag.setConstraints(eight, constraints);
pane.add(eight);
// seven
constraints.gridx = 2;
constraints.gridy = 3;
gridBag.setConstraints(seven, constraints);
pane.add(seven);
// devision
constraints.gridwidth = 1;
constraints.weightx = 1;
constraints.gridx = 3;
constraints.gridy = 3;
constraints.anchor = GridBagConstraints.SOUTH;
gridBag.setConstraints(devision, constraints);
pane.add(devision);
// EQUALS
constraints.gridx = 4;
constraints.gridy = 3;
constraints.gridwidth = 2;
constraints.gridheight = 4;
constraints.weighty = 30;
constraints.anchor = GridBagConstraints.SOUTH;
gridBag.setConstraints(equals, constraints);
pane.add(equals);
// six
constraints.weighty = 10;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.gridx = 0;
constraints.gridy = 4;
gridBag.setConstraints(six, constraints);
pane.add(six);
// five
constraints.gridx = 1;
constraints.gridy = 4;
gridBag.setConstraints(five, constraints);
pane.add(five);
// four
constraints.gridwidth = 1;
constraints.gridx = 2;
constraints.gridy = 4;
gridBag.setConstraints(four, constraints);
pane.add(four);
// Addition
constraints.gridwidth = 1;
constraints.weightx = 1;
constraints.gridx = 3;
constraints.gridy = 4;
gridBag.setConstraints(addition, constraints);
pane.add(addition);
// three
constraints.gridx = 0;
constraints.gridy = 5;
gridBag.setConstraints(three, constraints);
pane.add(three);
// two
constraints.gridx = 1;
constraints.gridy = 5;
gridBag.setConstraints(two, constraints);
pane.add(two);
// one
constraints.gridx = 2;
constraints.gridy = 5;
gridBag.setConstraints(one, constraints);
pane.add(one);
// subtraction
constraints.gridx = 3;
constraints.gridy = 5;
gridBag.setConstraints(subtraction, constraints);
pane.add(subtraction);
// zero
constraints.gridx = 0;
constraints.gridy = 6;
constraints.gridwidth = 2;
gridBag.setConstraints(zero, constraints);
pane.add(zero);
// period
constraints.gridx = 2;
constraints.gridy = 6;
constraints.gridwidth = 1;
gridBag.setConstraints(period, constraints);
pane.add(period);
// multiplication
constraints.gridx = 3;
constraints.gridy = 6;
constraints.gridwidth = 1;
gridBag.setConstraints(modulas, constraints);
pane.add(modulas);
pack();
add(pane);
}
public static void main(String[] args) {
Calculator frame = new Calculator();
frame.setSize(300, 350);
}
public void actionPerformed(ActionEvent evt){
Object source = evt.getSource();
String menuText = ((JMenuItem) evt.getSource()).getText();
if(menuText.equals("info")){
System.out.println("it works");
JOptionPane.showMessageDialog(null, "aha it works");
}
else if(source == one){
answerBox.append("1");
}else if (source == two){
answerBox.append("2");
}
else if (source == three){
answerBox.append("3");
}
else if (source == four){
answerBox.append("4");
}
else if (source == five){
answerBox.append("5");
}
else if (source == six){
answerBox.append("6");
}
else if (source == seven){
answerBox.append("7");
}
else if (source == eight){
answerBox.append("8");
}
else if (source == nine){
answerBox.append("9");
}
else if (source == zero){
answerBox.append("0");
}
else if (source == addition){
numberOne=Double.parseDouble(answerBox.getText());
operator="+";
answerBox.setText("");
}
else if (source == subtraction){
numberOne=Double.parseDouble(answerBox.getText());
operator="-";
answerBox.setText("");
}
else if (source == modulas){
numberOne=Double.parseDouble(answerBox.getText());
operator="*";
answerBox.setText("");
}
else if (source == devision){
numberOne=Double.parseDouble(answerBox.getText());
operator="/";
answerBox.setText("");
}
else if (source == period){
answerBox.append(".");
}
else if(source == equals){
numberTwo=Double.parseDouble(answerBox.getText());
if(operator.equals("+"))
result=numberOne+numberTwo;
else if(operator.equals("-"))
result=numberOne-numberTwo;
else if(operator.equals("*"))
result=numberOne-numberTwo;
else if(operator.equals("/"))
result=numberOne/numberTwo;
answerBox.setText(" "+result);
operator="=";
}
else if(source == clear){
numberOne=0;
numberTwo=0;
answerBox.setText("");
}
}
}