用GUI实现的简易计算器,不是很完善,但可以实现简单的加减乘除的基本运算。
代码如下:
package GUI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
public class Demo8 extends JFrame {
public Demo8() {
this.setTitle("叶羽i制作的简易计算器");
Frame MistakeWindons = new JFrame();
this.setIconImage(new ImageIcon("tubiao.png").getImage());
this.setSize(400, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
// this.setResizable(false);
JPanel jp = new JPanel(new GridLayout(5, 2));
JPanel jPanel1 = new JPanel();
JLabel jLabel1 = new JLabel("第一个数");
JTextField jTextField1 = new JTextField(20);
JPanel jPanel2 = new JPanel();
JButton SubBtn = new JButton("-");
SubBtn.setBackground(Color.CYAN);
JButton MulBtn = new JButton("*");
MulBtn.setBackground(Color.ORANGE);
JButton DivBtn = new JButton("/");
DivBtn.setBackground(Color.GREEN);
JButton Reset = new JButton("+");
Reset.setBackground(Color.magenta);
// JLabel jLabel2 = new JLabel("运算符");
// JTextField jTextField2 = new JTextField(20);
JPanel jPanel3 = new JPanel();
JLabel jLabel3 = new JLabel("第二个数");
JTextField jTextField3 = new JTextField(20);
JPanel jPanel4 = new JPanel();
JLabel jLabel4 = new JLabel("按下符号出结果");
JButton jButton1 = new JButton("");
JPanel jPanel5 = new JPanel();
JLabel jLabel5 = new JLabel("结果");
JTextField jTextField4 = new JTextField(20);
jPanel1.add(jLabel1);
jPanel1.add(jTextField1);
jPanel2.add(Reset);
jPanel2.add(SubBtn);
jPanel2.add(MulBtn);
jPanel2.add(DivBtn);
// jPanel2.add(jLabel2);
// jPanel2.add(jTextField2);
jPanel3.add(jLabel3);
jPanel3.add(jTextField3);
jPanel4.add(jLabel4);
jPanel4.add(jButton1);
jPanel5.add(jLabel5);
jPanel5.add(jTextField4);
{//事件处理函数
Reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {//异常处理,如果输入的不是数字,则提示输入错误
String text0 = jTextField1.getText();//将JTextFiled转换String
String text1 = jTextField3.getText();
double a = Double.valueOf(text0);//将String类型转换为double
double b = Double.valueOf(text1);
jTextField4.setText(a + b + "");
} catch (Exception e1) {
MistakeWindons.setResizable(false);
MistakeWindons.setLayout(null);
MistakeWindons.setTitle("Wrong Warning!");
MistakeWindons.setSize(200, 100);
MistakeWindons.setLocation(550, 550);
JLabel Mistake = new JLabel("Wrong Input!!!");
//Mistake.setFont(Ft);
Mistake.setBounds(50, 20, 100, 20);
MistakeWindons.add(Mistake);
MistakeWindons.setVisible(true);
}
}
});
SubBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String text0 = jTextField1.getText();//将JTextFiled转换String
String text1 = jTextField3.getText();
double a = Double.valueOf(text0);//将String类型转换为double
double b = Double.valueOf(text1);
jTextField4.setText(a - b + "");
} catch (Exception e1) {
MistakeWindons.setResizable(false);
MistakeWindons.setLayout(null);
MistakeWindons.setTitle("Wrong Warning!");
MistakeWindons.setSize(200, 100);
MistakeWindons.setLocation(550, 550);
JLabel Mistake = new JLabel("Wrong Input!!!");
//Mistake.setFont(Ft);
Mistake.setBounds(50, 20, 100, 20);
MistakeWindons.add(Mistake);
MistakeWindons.setVisible(true);
}
}
});
MulBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String text0 = jTextField1.getText();//将JTextFiled转换String
String text1 = jTextField3.getText();
double a = Double.valueOf(text0);//将String类型转换为double
double b = Double.valueOf(text1);
jTextField4.setText(a * b + "");
} catch (Exception e1) {
MistakeWindons.setResizable(false);
MistakeWindons.setLayout(null);
MistakeWindons.setTitle("Wrong Warning!");
MistakeWindons.setSize(200, 100);
MistakeWindons.setLocation(550, 550);
JLabel Mistake = new JLabel("Wrong Input!!!");
Mistake.setBounds(50, 20, 100, 20);
MistakeWindons.add(Mistake);
MistakeWindons.setVisible(true);
}
}
});
DivBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String text0 = jTextField1.getText();//将JTextFiled转换String
String text1 = jTextField3.getText();
double a = Double.valueOf(text0);//将String类型转换为double
double b = Double.valueOf(text1);
jTextField4.setText(a / b + "");
double c = a / b;
NumberFormat Nf = NumberFormat.getNumberInstance();//保留小数
Nf.setMaximumFractionDigits(6);
jTextField4.setText(Nf.format(c));
} catch (Exception e1) {
MistakeWindons.setResizable(false);
MistakeWindons.setLayout(null);
MistakeWindons.setTitle("Wrong Warning!");
MistakeWindons.setSize(200, 100);
MistakeWindons.setLocation(550, 550);
JLabel Mistake = new JLabel("Wrong Input!!!");
//Mistake.setFont(Ft);
Mistake.setBounds(50, 20, 100, 20);
MistakeWindons.add(Mistake);
MistakeWindons.setVisible(true);
}
}
});
jp.add(jPanel1);
jp.add(jPanel2);
jp.add(jPanel3);
jp.add(jPanel4);
jp.add(jPanel5);
this.add(jp);
this.setVisible(true);//!!!!!
}
}
public static void main(String[] args) {
new Demo8();
}
}
里面插入了图标,即第十一行代码的“tubiao.png”,这个图标没有什么重要作用,此行代码可以选择性删除。
运行效果:
ps:这里举了个8/4=2的例子