JAVA实现简易计算器
利用AWT和swing实现可视化简易计算器
/*
Author:cxc
Date:2021.04.07
Func:简易计算器
*/
package com.company.test0407;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
public static void main(String[] arg) {
// write your code here
myFarm farm = new myFarm();
}
}
class myFarm extends JFrame{
//网格化布局
JPanel jP1 = new JPanel();
JPanel jP2 = new JPanel();
JPanel jP3 = new JPanel();
JTextField jTF1 = new JTextField();
JTextField jTF2 = new JTextField();
JButton button1 = new MyButton("+");
JButton button2 = new MyButton("-");
JButton button3 = new MyButton("*");
JButton button4 = new MyButton("/");
JButton button5 = new MyButton("%");
JLabel jL = new JLabel("结果为:");
JTextArea jTF3 = new JTextArea();
ButtonListener1 listener1= new ButtonListener1();
ButtonListener2 listener2= new ButtonListener2();
ButtonListener3 listener3= new ButtonListener3();
ButtonListener4 listener4= new ButtonListener4();
ButtonListener5 listener5= new ButtonListener5();
private String s1;
private String s2;
private String s3;
private float a;
private float b;
private float c;
myFarm() {
this.setTitle("计算器V1.0 Author:cxc");
this.setSize(400,200);
this.setLocationRelativeTo(getOwner());
this.setLayout(new GridLayout(3,5,5,5));
jP1.setLayout(new GridLayout(1,2,5,5));
jP2.setLayout(new GridLayout(1,5,5,5));
jP3.setLayout(new GridLayout(1,2,5,5));
this.add(jP1);
this.add(jP2);
this.add(jP3);
jP1.add(jTF1);
jP1.add(jTF2);
jP2.add(button1);
jP2.add(button2);
jP2.add(button3);
jP2.add(button4);
jP2.add(button5);
jP3.add(jL);
jTF3.setForeground(Color.BLUE);
jP3.add(jTF3);
button1.addActionListener(listener1);
button2.addActionListener(listener2);
button3.addActionListener(listener3);
button4.addActionListener(listener4);
button5.addActionListener(listener5);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//新建操作符监听器,重写监听器执行动作actionPerformed
class ButtonListener1 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
try {
s1 = jTF1.getText();
s2 = jTF2.getText();
a = Float.parseFloat(s1);
b = Float.parseFloat(s2);
c = a + b;
jTF3.setText(String.format("%.3f", c));
} catch (Exception e1) {
}
}
}
class ButtonListener2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
try {
s1 = jTF1.getText();
s2 = jTF2.getText();
a = Float.parseFloat(s1);
b = Float.parseFloat(s2);
c = a - b;
jTF3.setText(String.format("%.3f", c));
} catch (Exception e1) {
}
}
}
class ButtonListener3 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e)
{
try{
s1 = jTF1.getText();
s2 = jTF2.getText();
a = Float.parseFloat(s1);
b = Float.parseFloat(s2);
c = a * b;
jTF3.setText(String.format("%.3f",c));
}
catch(Exception e1)
{
}
}
}
class ButtonListener4 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
try {
s1 = jTF1.getText();
s2 = jTF2.getText();
a = Float.parseFloat(s1);
b = Float.parseFloat(s2);
c = a / b;
jTF3.setText(String.format("%.3f", c));
} catch (Exception e1) {
}
}
}
class ButtonListener5 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e)
{
try {
s1 = jTF1.getText();
s2 = jTF2.getText();
a = Float.parseFloat(s1);
b = Float.parseFloat(s2);
c = a % b;
jTF3.setText(String.format("%.3f", c));
}
catch(Exception e1)
{
}
}
}
}
//自定义按钮
class MyButton extends JButton{
Font f = new Font("楷体",Font.BOLD,20);
MyButton(String s)
{
this.setForeground(Color.RED);
this.setFont(f);
this.setText(s);
}
}
效果如下图所示: