用javaGUI知识编写一个计算机并实现功能
时间: 2023-09-03 20:07:22 浏览: 81
Java项目:考试系统Java基础Gui(java+Gui)
5星 · 资源好评率100%
好的,我可以帮您编写一个简单的计算器,实现加减乘除四则运算的功能。以下是代码实现:
```java
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Calculator extends JFrame {
private JPanel contentPane;
private JTextField textField;
double firstnum, secondnum, result;
String operations, answer;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator frame = new Calculator();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Calculator() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 332, 416);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(10, 11, 296, 46);
contentPane.add(textField);
textField.setColumns(10);
// Number buttons
JButton btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Enternumber = textField.getText() + btn1.getText();
textField.setText(Enternumber);
}
});
btn1.setBounds(10, 77, 60, 50);
contentPane.add(btn1);
JButton btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Enternumber = textField.getText() + btn2.getText();
textField.setText(Enternumber);
}
});
btn2.setBounds(80, 77, 60, 50);
contentPane.add(btn2);
JButton btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Enternumber = textField.getText() + btn3.getText();
textField.setText(Enternumber);
}
});
btn3.setBounds(150, 77, 60, 50);
contentPane.add(btn3);
JButton btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Enternumber = textField.getText() + btn4.getText();
textField.setText(Enternumber);
}
});
btn4.setBounds(10, 138, 60, 50);
contentPane.add(btn4);
JButton btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Enternumber = textField.getText() + btn5.getText();
textField.setText(Enternumber);
}
});
btn5.setBounds(80, 138, 60, 50);
contentPane.add(btn5);
JButton btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Enternumber = textField.getText() + btn6.getText();
textField.setText(Enternumber);
}
});
btn6.setBounds(150, 138, 60, 50);
contentPane.add(btn6);
JButton btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Enternumber = textField.getText() + btn7.getText();
textField.setText(Enternumber);
}
});
btn7.setBounds(10, 199, 60, 50);
contentPane.add(btn7);
JButton btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Enternumber = textField.getText() + btn8.getText();
textField.setText(Enternumber);
}
});
btn8.setBounds(80, 199, 60, 50);
contentPane.add(btn8);
JButton btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Enternumber = textField.getText() + btn9.getText();
textField.setText(Enternumber);
}
});
btn9.setBounds(150, 199, 60, 50);
contentPane.add(btn9);
JButton btn0 = new JButton("0");
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Enternumber = textField.getText() + btn0.getText();
textField.setText(Enternumber);
}
});
btn0.setBounds(80, 260, 60, 50);
contentPane.add(btn0);
// Operator buttons
JButton btnPlus = new JButton("+");
btnPlus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstnum = Double.parseDouble(textField.getText());
textField.setText("");
operations = "+";
}
});
btnPlus.setBounds(221, 77, 85, 50);
contentPane.add(btnPlus);
JButton btnMinus = new JButton("-");
btnMinus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstnum = Double.parseDouble(textField.getText());
textField.setText("");
operations = "-";
}
});
btnMinus.setBounds(221, 138, 85, 50);
contentPane.add(btnMinus);
JButton btnMultiply = new JButton("*");
btnMultiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstnum = Double.parseDouble(textField.getText());
textField.setText("");
operations = "*";
}
});
btnMultiply.setBounds(221, 199, 85, 50);
contentPane.add(btnMultiply);
JButton btnDivide = new JButton("/");
btnDivide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstnum = Double.parseDouble(textField.getText());
textField.setText("");
operations = "/";
}
});
btnDivide.setBounds(221, 260, 85, 50);
contentPane.add(btnDivide);
JButton btnClear = new JButton("C");
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(null);
}
});
btnClear.setBounds(150, 260, 60, 50);
contentPane.add(btnClear);
JButton btnEqual = new JButton("=");
btnEqual.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String answer;
secondnum = Double.parseDouble(textField.getText());
if (operations == "+") {
result = firstnum + secondnum;
answer = String.format("%.2f", result);
textField.setText(answer);
} else if (operations == "-") {
result = firstnum - secondnum;
answer = String.format("%.2f", result);
textField.setText(answer);
} else if (operations == "*") {
result = firstnum * secondnum;
answer = String.format("%.2f", result);
textField.setText(answer);
} else if (operations == "/") {
result = firstnum / secondnum;
answer = String.format("%.2f", result);
textField.setText(answer);
}
}
});
btnEqual.setBounds(10, 260, 60, 50);
contentPane.add(btnEqual);
}
}
```
运行后,您就可以使用这个计算器进行四则运算了。
阅读全文