任务二: 计算器
(1)编写一个计算器的程序,主界面由多个按钮组建、文本框组建构成。运行结果如图所示:
(2)同时,使用相应的事件对这个组件进行监听工作,达到计算的功能。
package second;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.SwingConstants;
import java.awt.Toolkit;
import java.awt.SystemColor;
public class Calculate extends JFrame {
private JPanel contentPane;
private JTextField textField;
public static char ch = '\0';
public static char ch1 = '\0';
public static String str = "\0";
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculate frame = new Calculate();
frame.setVisible(true);
frame.setResizable(false);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Calculate() {
setForeground(new Color(255, 0, 255));
setBackground(Color.GRAY);
setFont(new Font("楷体", Font.PLAIN, 18));
setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Administrator\\Pictures\\Camera Roll\\icon.jpg"));
setTitle("\u7B80\u6613\u8BA1\u7B97\u5668");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 375);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setBackground(new Color(240, 248, 255));
textField.setForeground(new Color(0, 255, 0));
textField.setFont(new Font("黑体", Font.PLAIN, 30));
textField.setBounds(0, 0, 586, 50);
contentPane.add(textField);
textField.setColumns(10);
textField.requestFocus();
JButton btnNewButton = new JButton("Back");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1 = textField.getText();
s1 = s1.substring(0, s1.length()-1);
textField.setText(s1);
}
});
btnNewButton.setForeground(new Color(0, 0, 255));
btnNewButton.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton.setBounds(10, 57, 135, 50);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("9");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str = textField.getText();
if (ch != '\0') {
ch1 = ch;
ch = '\0';
textField.setText("");
}
String s1 = textField.getText();
String s2 = s1 + "9";
textField.setText(s2);
}
});
btnNewButton_1.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_1.setBounds(10, 114, 97, 50);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("CE");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
ch = '\0';
}
});
btnNewButton_2.setForeground(new Color(0, 0, 255));
btnNewButton_2.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_2.setBounds(154, 57, 135, 50);
contentPane.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("C");
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
ch = '\0';
}
});
btnNewButton_3.setForeground(new Color(0, 0, 255));
btnNewButton_3.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_3.setBounds(299, 57, 135, 50);
contentPane.add(btnNewButton_3);
JButton btnNewButton_4 = new JButton("About");
btnNewButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"作者:罗浩\n日期:2021-11-12");
}
});
btnNewButton_4.setForeground(new Color(0, 0, 255));
btnNewButton_4.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_4.setBounds(450, 57, 135, 50);
contentPane.add(btnNewButton_4);
JButton btnNewButton_5 = new JButton("8");
btnNewButton_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str = textField.getText();
if (ch != '\0') {
ch1 = ch;
ch = '\0';
textField.setText("");
}
String s1 = textField.getText();
String s2 = s1 + "8";
textField.setText(s2);
}
});
btnNewButton_5.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_5.setBounds(110, 114, 97, 50);
contentPane.add(btnNewButton_5);
JButton btnNewButton_6 = new JButton("6");
btnNewButton_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str = textField.getText();
if (ch != '\0') {
ch1 = ch;
ch = '\0';
textField.setText("");
}
String s1 = textField.getText();
String s2 = s1 + "6";
textField.setText(s2);
}
});
btnNewButton_6.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_6.setBounds(10, 170, 97, 50);
contentPane.add(btnNewButton_6);
JButton btnNewButton_7 = new JButton("3");
btnNewButton_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str = textField.getText();
if (ch != '\0') {
ch1 = ch;
ch = '\0';
textField.setText("");
}
String s1 = textField.getText();
String s2 = s1 + "3";
textField.setText(s2);
}
});
btnNewButton_7.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_7.setBounds(10, 225, 97, 50);
contentPane.add(btnNewButton_7);
JButton btnNewButton_8 = new JButton("0");
btnNewButton_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str = textField.getText();
if (ch != '\0') {
ch1 = ch;
ch = '\0';
textField.setText("");
}
String s1 = textField.getText();
String s2 = s1 + "0";
textField.setText(s2);
}
});
btnNewButton_8.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_8.setBounds(10, 280, 97, 50);
contentPane.add(btnNewButton_8);
JButton btnNewButton_9 = new JButton("2");
btnNewButton_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str = textField.getText();
if (ch != '\0') {
ch1 = ch;
ch = '\0';
textField.setText("");
}
String s1 = textField.getText();
String s2 = s1 + "2";
textField.setText(s2);
}
});
btnNewButton_9.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_9.setBounds(110, 225, 97, 50);
contentPane.add(btnNewButton_9);
JButton btnNewButton_10 = new JButton("5");
btnNewButton_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str = textField.getText();
if (ch != '\0') {
ch1 = ch;
ch = '\0';
textField.setText("");
}
String s1 = textField.getText();
String s2 = s1 + "5";
textField.setText(s2);
}
});
btnNewButton_10.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_10.setBounds(110, 170, 97, 50);
contentPane.add(btnNewButton_10);
JButton btnNewButton_11 = new JButton("+/-");
btnNewButton_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1 = textField.getText();
double num1 = Double.parseDouble(s1);
if (num1 > 0) {
String s2 = "-" + s1 + "";
textField.setText(s2);
}
else {
s1 = s1.substring(1,s1.length());
textField.setText(s1);
}
}
});
btnNewButton_11.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_11.setBounds(110, 280, 97, 50);
contentPane.add(btnNewButton_11);
JButton btnNewButton_12 = new JButton("7");
btnNewButton_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str = textField.getText();
if (ch != '\0') {
ch1 = ch;
ch = '\0';
textField.setText("");
}
String s1 = textField.getText();
String s2 = s1 + "7";
textField.setText(s2);
}
});
btnNewButton_12.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_12.setBounds(210, 114, 97, 50);
contentPane.add(btnNewButton_12);
JButton btnNewButton_13 = new JButton("4");
btnNewButton_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str = textField.getText();
if (ch != '\0') {
ch1 = ch;
ch = '\0';
textField.setText("");
}
String s1 = textField.getText();
String s2 = s1 + "4";
textField.setText(s2);
}
});
btnNewButton_13.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_13.setBounds(210, 170, 97, 50);
contentPane.add(btnNewButton_13);
JButton btnNewButton_14 = new JButton("1");
btnNewButton_14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str = textField.getText();
if (ch != '\0') {
ch1 = ch;
ch = '\0';
textField.setText("");
}
String s1 = textField.getText();
String s2 = s1 + "1";
textField.setText(s2);
}
});
btnNewButton_14.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_14.setBounds(210, 225, 97, 50);
contentPane.add(btnNewButton_14);
JButton btnNewButton_15 = new JButton(".");
btnNewButton_15.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str = textField.getText();
if (ch != '\0') {
ch1 = ch;
ch = '\0';
textField.setText("");
}
if (textField.getText() =="") return;
String s1 = textField.getText();
String s2 = s1 + ".";
textField.setText(s2);
}
});
btnNewButton_15.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_15.setBounds(210, 280, 97, 50);
contentPane.add(btnNewButton_15);
JButton btnNewButton_17 = new JButton("/");
btnNewButton_17.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ch = '/';
}
});
btnNewButton_17.setForeground(new Color(255, 0, 0));
btnNewButton_17.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_17.setBounds(313, 114, 130, 50);
contentPane.add(btnNewButton_17);
JButton btnNewButton_18 = new JButton("*");
btnNewButton_18.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ch = '*';
}
});
btnNewButton_18.setForeground(new Color(255, 0, 0));
btnNewButton_18.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_18.setBounds(313, 170, 130, 50);
contentPane.add(btnNewButton_18);
JButton btnNewButton_19 = new JButton("-");
btnNewButton_19.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ch = '-';
}
});
btnNewButton_19.setForeground(new Color(255, 0, 0));
btnNewButton_19.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_19.setBounds(313, 225, 130, 50);
contentPane.add(btnNewButton_19);
JButton btnNewButton_20 = new JButton("+");
btnNewButton_20.setForeground(new Color(255, 0, 0));
btnNewButton_20.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_20.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ch = '+';
}
});
btnNewButton_20.setBounds(313, 280, 130, 50);
contentPane.add(btnNewButton_20);
JButton btnNewButton_21 = new JButton("sqrt");
btnNewButton_21.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1 = textField.getText();
double num2 = Double.parseDouble(s1);
double num3 = num2 * num2;
String s2 = "" + num3;
textField.setText(s2);
}
});
btnNewButton_21.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_21.setBounds(456, 114, 130, 50);
contentPane.add(btnNewButton_21);
JButton btnNewButton_22 = new JButton("%");
btnNewButton_22.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ch = '%';
}
});
btnNewButton_22.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_22.setBounds(456, 170, 130, 50);
contentPane.add(btnNewButton_22);
JButton btnNewButton_23 = new JButton("1/x");
btnNewButton_23.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1 = textField.getText();
double num2 = Double.parseDouble(s1);
double num3 = 1.0 / num2;
String s2 = "" + num3;
textField.setText(s2);
}
});
btnNewButton_23.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_23.setBounds(456, 225, 130, 50);
contentPane.add(btnNewButton_23);
JButton btnNewButton_24 = new JButton("=");
btnNewButton_24.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double num = Double.parseDouble(str);
String s1 = textField.getText();
double num2 = Double.parseDouble(s1);
double num3 = 0;
switch(ch1) {
case '+':
num3 = num + num2;
ch1 = '\0';
break;
case '-':
num3 = num - num2;
ch1 = '\0';
break;
case '*':
num3 = num * num2;
ch1 = '\0';
break;
case '/':
try {
num3 = num / num2;
ch1 = '\0';
} catch (ArithmeticException sq) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null,"出错啦! 除数不能为零!!");
s1 = "";
textField.setText(s1);
break;
}
break;
case '%':
num3 = num % num2;
ch1 = '\0';
break;
default:
ch1 = '\0';
textField.setText("");
return;
}
String s2 = "" + num3;
textField.setText(s2);
}
});
btnNewButton_24.setForeground(new Color(255, 0, 0));
btnNewButton_24.setFont(new Font("黑体", Font.PLAIN, 24));
btnNewButton_24.setBounds(456, 280, 130, 50);
contentPane.add(btnNewButton_24);
}
}