基于GUI的简易计算器,能够对两个正整数进行相加或相减,并把结果输出到界面。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends JFrame {
private final Container container = getContentPane();
public Calculator(){
container.setLayout(null);
setContent();
setWindow();
}
private void setWindow(){
this.setVisible(true); //窗口可见
this.setSize(400,450); //窗口大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //窗口可关闭
this.setLocationRelativeTo(null); //窗口居中
this.setTitle("简易计算器"); //窗口标题
}
private void setContent(){
/* 标签 */
JLabel jLabel1 = new JLabel("计算器");
jLabel1.setBounds(140,30,100,50);
jLabel1.setFont(new Font("楷体",Font.PLAIN,20));
JLabel jLabel2 = new JLabel("运算数一");
jLabel2.setBounds(60,70,100,50);
jLabel2.setFont(new Font("楷体",Font.PLAIN,20));
JLabel jLabel3 = new JLabel("运算数二");
jLabel3.setBounds(60,110,100,50);
jLabel3.setFont(new Font("楷体",Font.PLAIN,20));
JLabel jLabel4 = new JLabel("运算结果");
jLabel4.setBounds(60,150,100,50);
jLabel4.setFont(new Font("楷体",Font.PLAIN,20));
/* 文本域 */
JTextField jTextField1 = new JTextField(20);
jTextField1.setBounds(170,80,150,30);
JTextField jTextField2 = new JTextField(20);
jTextField2.setBounds(170,120,150,30);
JTextField jTextField3 = new JTextField(20);
jTextField3.setBounds(170,160,150,30);
/* 按钮 */
JButton jButton1 = new JButton("相加");
jButton1.setBounds(80,220,80,40);
jButton1.setFont(new Font("楷体",Font.PLAIN,20));
JButton jButton2 = new JButton("相减");
jButton2.setBounds(220,220,80,40);
jButton2.setFont(new Font("楷体",Font.PLAIN,20));
JButton jButton3 = new JButton("清零");
jButton3.setBounds(120,300,140,40);
jButton3.setFont(new Font("楷体",Font.PLAIN,20));
/* 相加 按钮的监听事件 */
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String text1 = jTextField1.getText();
if (text1.equals("")){
new ErrorPrompt();
}
int num1 = Integer.parseInt(text1);
String text2 = jTextField2.getText();
if (text2.equals("")){
new ErrorPrompt();
}
int num2 = Integer.parseInt(text2);
jTextField3.setText("" + (num1+num2));
}catch (NumberFormatException a){
new ErrorPrompt();
}
}
});
/* 相减 按钮的监听事件 */
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String text1 = jTextField1.getText();
if (text1.equals("")){
new ErrorPrompt();
}
int num1 = Integer.parseInt(text1);
String text2 = jTextField2.getText();
if (text2.equals("")){
new ErrorPrompt();
}
int num2 = Integer.parseInt(text2);
jTextField3.setText("" + (num1-num2));
}catch (NumberFormatException a){
new ErrorPrompt();
}
}
});
/* 全部清零 按钮的监听事件 */
jButton3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextField1.setText(""); //文本域全部清零
jTextField2.setText("");
jTextField3.setText("");
}
});
container.add(jLabel1);
container.add(jLabel2);
container.add(jLabel3);
container.add(jLabel4);
container.add(jTextField1);
container.add(jTextField2);
container.add(jTextField3);
container.add(jButton1);
container.add(jButton2);
container.add(jButton3);
}
}
class ErrorPrompt extends JFrame {
Container container = getContentPane();
public ErrorPrompt(){
container.setLayout(null);
setWindow();
setContent();
}
private void setWindow(){
this.setVisible(true);
this.setSize(300,200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //关闭当前窗口
this.setTitle("错误提示");
}
private void setContent(){
JLabel jLabel = new JLabel("请输入正整数!");
jLabel.setBounds(70,30,140,80);
jLabel.setFont(new Font("楷体",Font.PLAIN,20));
jLabel.setForeground(Color.red); //设置字体为红色
container.add(jLabel);
}
}
public class Main {
public static void main(String[] args) {
new Calculator();
}
}