|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这个是源代码,家人们,怎么实现计算器里面的等号按钮
package com;
import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Case2 extends JFrame implements ActionListener {
JTextField textField1 = new JTextField();
JButton buttondele = new JButton("C");
JButton buttonBackpace = new JButton("Backpace");
JButton buttonDiv = new JButton("/");
JButton button7= new JButton("7");
JButton button8 = new JButton("8");
JButton button9= new JButton("9");
JButton buttonMul = new JButton("*");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6= new JButton("6");
JButton buttonSub = new JButton("-");
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton buttonAdd = new JButton("+");
JButton button0 = new JButton("0");
JButton buttonPanel = new JButton("+/-");
JButton buttonPoint= new JButton(".");
JButton buttonequl = new JButton("=");
JButton buttonreciprocal = new JButton("1/x");
JButton buttonmod = new JButton("%") ;
JButton buttonsqrt = new JButton("sqrt") ;
private StringBuilder inputBuffer = new StringBuilder();
public Case2() {
JFrame jf = new JFrame("计算器");
jf.setLayout(null);
JMenuBar jb = new JMenuBar();
this.setJMenuBar(jb);
JMenu update = new JMenu("编辑");
jb.add(update);
button0.setBounds(40,400, 50, 50);
jf.add(button0);
buttonreciprocal.setBounds(40,460, 50, 50);
jf.add(buttonreciprocal);
button1.setBounds(40, 340, 50, 50);
jf.add(button1);
button2.setBounds(100, 340, 50, 50);
jf.add(button2);
button3.setBounds(160, 340, 50, 50);
jf.add(button3);
buttonPanel.setBounds(100,400,50,50);
jf.add(buttonPanel);
button4.setBounds(40, 280, 50, 50);
jf.add(button4);
button5.setBounds(100, 280, 50, 50);
jf.add(button5);
button6.setBounds(160, 280, 50, 50);
jf.add(button6);
button7.setBounds(40, 220, 50, 50);
jf.add(button7);
button8.setBounds(100, 220, 50, 50);
jf.add(button8);
button9.setBounds(160, 220, 50, 50);
jf.add(button9);
buttonPoint.setBounds(160, 400, 50, 50);
jf.add(buttonPoint);
buttonsqrt.setBounds(160, 460,60, 50);
jf.add(buttonsqrt);
buttonAdd.setBounds(220, 400, 50, 50);
jf.add(buttonAdd);
buttonSub.setBounds(220, 340, 50, 50);
jf.add(buttonSub);
buttonMul.setBounds(220, 280, 50, 50);
jf.add(buttonMul);
buttonDiv.setBounds(220, 220, 50, 50);
jf.add(buttonDiv);
buttonequl.setBounds(220,460,50,50);
jf.add(buttonequl);
buttonmod.setBounds(100,460,50,50);
jf.add(buttonmod);
buttondele.setBounds(150, 170, 110, 50);
jf.add(buttondele);
buttonBackpace.setBounds(60, 170, 110, 50);
jf.add(buttonBackpace);
textField1.setBounds(40, 20, 350, 60);
jf.add(textField1);
textField1.addActionListener(this);
buttondele.addActionListener(this);
buttonBackpace.addActionListener(this);
buttonDiv.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
buttonAdd.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
buttonSub.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
buttonMul.addActionListener(this);
buttonequl.addActionListener(this);
button0.addActionListener(this);
buttonPoint.addActionListener(this);
buttonmod.addActionListener(this);
buttonPanel.addActionListener(this);
buttonreciprocal.addActionListener(this);
buttonsqrt.addActionListener(this);
jf.setBounds(0, 0, 500, 600);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void main(String[] args) {
new Case2();
}
@Override
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
String command = source.getText();
switch (command) {
case "=":
calculateResult();
break;
case "C":
clearInput();
break;
case "Backpace":
backpace();
break;
case "+/-":
changeSign();
break;
case "1/x":
reciprocal();
break;
case "%":
calculateMod();
break;
case "sqrt":
calculateSqrt();
break;
case "+":
performAddition();
break;
default:
addToInput(command);
break;
}
}
private void addToInput(String command) {
String currentInput = textField1.getText();
textField1.setText(currentInput + command);
}
private void calculateResult() {
String expression = textField1.getText();
}
private void clearInput() {
textField1.setText("");
}
private void changeSign() {
String currentInput = textField1.getText();
if (!currentInput.isEmpty() && !currentInput.equals("0")) {
if (currentInput.charAt(0) == '-') {
textField1.setText(currentInput.substring(1));
} else {
textField1.setText("-" + currentInput);
}
}
}
private void reciprocal() {
String currentInput = textField1.getText();
try {
double value = Double.parseDouble(currentInput);
if (value != 0) {
double reciprocal = 1 / value;
textField1.setText(Double.toString(reciprocal));
} else {
textField1.setText("Error");
}
} catch (NumberFormatException e) {
textField1.setText("Error");
}
}
private void calculateMod() {
String currentInput = textField1.getText();
try {
double value = Double.parseDouble(currentInput);
textField1.setText(Double.toString(value % 2));
} catch (NumberFormatException e) {
textField1.setText("Error");
}
}
private void calculateSqrt() {
String currentInput = textField1.getText();
try {
double value = Double.parseDouble(currentInput);
if (value >= 0) {
double sqrtResult = Math.sqrt(value);
textField1.setText(Double.toString(sqrtResult));
} else {
textField1.setText("Error");
}
} catch (NumberFormatException e) {
textField1.setText("Error");
}
}
private void performAddition() {
String currentInput = textField1.getText();
if (!currentInput.isEmpty()) {
textField1.setText(currentInput + " + ");
}
}
private void backpace() {
String currentInput = textField1.getText();
if (currentInput.length() > 0) {
textField1.setText(currentInput.substring(0, currentInput.length() - 1));
}
}
}
|
|