|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这个是源代码,我布局好了界面,不会把我设置的按钮实现它的功能,后面的那些方法是我用GPT生成的,家人们,可以帮我看看吗
package com;
import javax.script.*;
import javax.script.ScriptEngineManager;
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();
private JTextComponent inputField;
public Case2() {
JFrame frame = new JFrame("计算器");
frame.setLayout(null);
JMenuBar jb = new JMenuBar();
this.setJMenuBar(jb);
JMenu update = new JMenu("编辑");
jb.add(update);
button0.setBounds(40,400, 50, 50);
frame.add(button0);
buttonreciprocal.setBounds(40,460, 50, 50);
frame.add(buttonreciprocal);
button1.setBounds(40, 340, 50, 50);
frame.add(button1);
button2.setBounds(100, 340, 50, 50);
frame.add(button2);
button3.setBounds(160, 340, 50, 50);
frame.add(button3);
buttonPanel.setBounds(100,400,50,50);
frame.add(buttonPanel);
button4.setBounds(40, 280, 50, 50);
frame.add(button4);
button5.setBounds(100, 280, 50, 50);
frame.add(button5);
button6.setBounds(160, 280, 50, 50);
frame.add(button6);
button7.setBounds(40, 220, 50, 50);
frame.add(button7);
button8.setBounds(100, 220, 50, 50);
frame.add(button8);
button9.setBounds(160, 220, 50, 50);
frame.add(button9);
buttonPoint.setBounds(160, 400, 50, 50);
frame.add(buttonPoint);
buttonsqrt.setBounds(160, 460,60, 50);
frame.add(buttonsqrt);
buttonAdd.setBounds(220, 400, 50, 50);
frame.add(buttonAdd);
buttonSub = new JButton("-");
buttonSub.setBounds(220, 340, 50, 50);
frame.add(buttonSub);
buttonMul.setBounds(220, 280, 50, 50);
frame.add(buttonMul);
buttonDiv.setBounds(220, 220, 50, 50);
frame.add(buttonDiv);
buttonequl.setBounds(220,460,50,50);
frame.add(buttonequl);
buttonmod.setBounds(100,460,50,50);
frame.add(buttonmod);
buttondele.setBounds(150, 170, 110, 50);
frame.add(buttondele);
buttonBackpace.setBounds(60, 170, 110, 50);
frame.add(buttonBackpace);
textField1.setBounds(40, 20, 350, 60);
frame.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);
frame.setBounds(0, 0, 500, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.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 "+/-":
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 = inputField.getText();
inputField.setText(currentInput + command);
}
private void calculateResult() {
String input = inputField.getText();
try {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
Object result = engine.eval(input);
inputField.setText(result.toString());
} catch (ScriptException e) {
inputField.setText("Error");
}
}
private void clearInput() {
inputField.setText("");
}
private void changeSign() {
String currentInput = inputField.getText();
if (!currentInput.isEmpty() && !currentInput.equals("0")) {
if (currentInput.charAt(0) == '-') {
inputField.setText(currentInput.substring(1));
} else {
inputField.setText("-" + currentInput);
}
}
}
private void reciprocal() {
String currentInput = inputField.getText();
try {
double value = Double.parseDouble(currentInput);
if (value != 0) {
double reciprocal = 1 / value;
inputField.setText(Double.toString(reciprocal));
} else {
inputField.setText("Error");
}
} catch (NumberFormatException e) {
inputField.setText("Error");
}
}
private void calculateMod() {
String currentInput = inputField.getText();
try {
double value = Double.parseDouble(currentInput);
inputField.setText(Double.toString(value % 2));
} catch (NumberFormatException e) {
inputField.setText("Error");
}
}
private void calculateSqrt() {
String currentInput = inputField.getText();
try {
double value = Double.parseDouble(currentInput);
if (value >= 0) {
double sqrtResult = Math.sqrt(value);
inputField.setText(Double.toString(sqrtResult));
} else {
inputField.setText("Error");
}
} catch (NumberFormatException e) {
inputField.setText("Error");
}
}
private void performAddition() {
String currentInput = inputField.getText();
if (!currentInput.isEmpty() && !currentInput.equals("Error")) {
inputField.setText(currentInput + " + ");
}
}
}
你好!我看到你的代码中已经创建了按钮,并且给每个按钮都设置了相应的文本。现在你想要实现这些按钮的功能对吗?
首先,你需要在按钮的监听事件中编写对应按钮的功能代码。根据你的需求,我为你提供了一些示例代码。
1. 对于数字按钮,你可以将按钮上的数字添加到输入区域中。例如: private void addToInput(String command) { String currentInput = textField1.getText(); textField1.setText(currentInput + command);}
2. 对于运算符按钮,你可以在输入区域中添加相应的运算符。例如: private void performAddition() { String currentInput = textField1.getText(); if (!currentInput.isEmpty()) { textField1.setText(currentInput + " + "); }}
3. 对于等号按钮,你可以计算输入表达式的结果并显示在输入区域中。这里使用了JavaScript引擎来计算表达式的结果。 private void calculateResult() { String input = textField1.getText(); try { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("javascript"); Object result = engine.eval(input); textField1.setText(result.toString()); } catch (ScriptException e) { textField1.setText("Error"); }}
4. 对于清除按钮,你可以清空输入区域的内容。例如: private void clearInput() { textField1.setText("");}
5. 其他功能按钮的实现方式类似,你可以根据需求编写相应的功能代码。
希望这些示例代码可以帮助你实现按钮的功能。如果还有其他问题,请随时提问!
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
|
|