java算术计算机swing
时间: 2023-09-09 14:12:15 浏览: 84
如果你想要开发一个基于Java的算术计算器,可以使用Swing作为GUI框架。Swing是Java编程语言中的一种GUI工具包,它提供了丰富的组件和工具来创建图形用户界面。
以下是一个简单的Swing算术计算器示例:
1. 首先,创建一个JFrame对象并设置其标题。
```java
import javax.swing.*;
import java.awt.*;
public class Calculator extends JFrame {
public Calculator() {
super("Simple Calculator");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(4, 4));
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
2. 然后,创建JButton对象并将其添加到JFrame中。
```java
import javax.swing.*;
import java.awt.*;
public class Calculator extends JFrame {
public Calculator() {
super("Simple Calculator");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(4, 4));
// Create buttons
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("+");
JButton button5 = new JButton("4");
JButton button6 = new JButton("5");
JButton button7 = new JButton("6");
JButton button8 = new JButton("-");
JButton button9 = new JButton("7");
JButton button10 = new JButton("8");
JButton button11 = new JButton("9");
JButton button12 = new JButton("*");
JButton button13 = new JButton(".");
JButton button14 = new JButton("0");
JButton button15 = new JButton("C");
JButton button16 = new JButton("/");
// Add buttons to frame
add(button1);
add(button2);
add(button3);
add(button4);
add(button5);
add(button6);
add(button7);
add(button8);
add(button9);
add(button10);
add(button11);
add(button12);
add(button13);
add(button14);
add(button15);
add(button16);
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
3. 最后,在按钮上添加ActionListener,当用户点击按钮时进行相应的操作。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
public Calculator() {
super("Simple Calculator");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
// Create text field
textField = new JTextField();
add(textField, BorderLayout.NORTH);
// Create buttons
JPanel panel = new JPanel(new GridLayout(4, 4));
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("+");
JButton button5 = new JButton("4");
JButton button6 = new JButton("5");
JButton button7 = new JButton("6");
JButton button8 = new JButton("-");
JButton button9 = new JButton("7");
JButton button10 = new JButton("8");
JButton button11 = new JButton("9");
JButton button12 = new JButton("*");
JButton button13 = new JButton(".");
JButton button14 = new JButton("0");
JButton button15 = new JButton("C");
JButton button16 = new JButton("/");
// Add action listeners to buttons
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
button10.addActionListener(this);
button11.addActionListener(this);
button12.addActionListener(this);
button13.addActionListener(this);
button14.addActionListener(this);
button15.addActionListener(this);
button16.addActionListener(this);
// Add buttons to panel
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
panel.add(button6);
panel.add(button7);
panel.add(button8);
panel.add(button9);
panel.add(button10);
panel.add(button11);
panel.add(button12);
panel.add(button13);
panel.add(button14);
panel.add(button15);
panel.add(button16);
// Add panel to frame
add(panel, BorderLayout.CENTER);
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String button = e.getActionCommand();
if (button.equals("C")) {
textField.setText("");
} else {
textField.setText(textField.getText() + button);
}
}
}
```
这个简单的Swing算术计算器可以让用户输入数字,并在文本字段中显示结果。你可以根据需要添加更多的功能,例如计算器运算符、计算等等。