5. 自主编程:计算器,使用恰当的组件和容器,加完成软件界面设计
时间: 2024-03-06 21:48:20 浏览: 69
好的,我可以帮你完成这个任务。我可以使用Java编写一个简单的计算器,并使用Swing GUI库来创建界面。我会使用JFrame作为主容器,然后添加JPanel来容纳各个组件,如JButtons和JTextField。
以下是我实现的代码示例:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JPanel panel;
private JTextField display;
private JButton button0;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
private JButton button7;
private JButton button8;
private JButton button9;
private JButton buttonPlus;
private JButton buttonMinus;
private JButton buttonMultiply;
private JButton buttonDivide;
private JButton buttonEquals;
private JButton buttonClear;
private double num1, num2, result;
private String operator;
public Calculator() {
super("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel(new GridLayout(4, 4));
display = new JTextField();
display.setEditable(false);
display.setHorizontalAlignment(SwingConstants.RIGHT);
button0 = new JButton("0");
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
buttonPlus = new JButton("+");
buttonMinus = new JButton("-");
buttonMultiply = new JButton("*");
buttonDivide = new JButton("/");
buttonEquals = new JButton("=");
buttonClear = new JButton("C");
button0.addActionListener(this);
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);
buttonPlus.addActionListener(this);
buttonMinus.addActionListener(this);
buttonMultiply.addActionListener(this);
buttonDivide.addActionListener(this);
buttonEquals.addActionListener(this);
buttonClear.addActionListener(this);
panel.add(button7);
panel.add(button8);
panel.add(button9);
panel.add(buttonDivide);
panel.add(button4);
panel.add(button5);
panel.add(button6);
panel.add(buttonMultiply);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(buttonMinus);
panel.add(button0);
panel.add(buttonClear);
panel.add(buttonEquals);
panel.add(buttonPlus);
add(display, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button0) {
display.setText(display.getText() + "0");
} else if (e.getSource() == button1) {
display.setText(display.getText() + "1");
} else if (e.getSource() == button2) {
display.setText(display.getText() + "2");
} else if (e.getSource() == button3) {
display.setText(display.getText() + "3");
} else if (e.getSource() == button4) {
display.setText(display.getText() + "4");
} else if (e.getSource() == button5) {
display.setText(display.getText() + "5");
} else if (e.getSource() == button6) {
display.setText(display.getText() + "6");
} else if (e.getSource() == button7) {
display.setText(display.getText() + "7");
} else if (e.getSource() == button8) {
display.setText(display.getText() + "8");
} else if (e.getSource() == button9) {
display.setText(display.getText() + "9");
} else if (e.getSource() == buttonPlus) {
num1 = Double.parseDouble(display.getText());
operator = "+";
display.setText("");
} else if (e.getSource() == buttonMinus) {
num1 = Double.parseDouble(display.getText());
operator = "-";
display.setText("");
} else if (e.getSource() == buttonMultiply) {
num1 = Double.parseDouble(display.getText());
operator = "*";
display.setText("");
} else if (e.getSource() == buttonDivide) {
num1 = Double.parseDouble(display.getText());
operator = "/";
display.setText("");
} else if (e.getSource() == buttonClear) {
display.setText("");
} else if (e.getSource() == buttonEquals) {
num2 = Double.parseDouble(display.getText());
if (operator.equals("+")) {
result = num1 + num2;
} else if (operator.equals("-")) {
result = num1 - num2;
} else if (operator.equals("*")) {
result = num1 * num2;
} else if (operator.equals("/")) {
result = num1 / num2;
}
display.setText(Double.toString(result));
}
}
public static void main(String[] args) {
Calculator calc = new Calculator();
}
}
```
这个计算器支持加减乘除四则运算,以及清空和计算结果。你可以尝试使用这个计算器进行简单的运算。
阅读全文