import java.awt.*
import java.awt.event.*
public class Calculator implements ActionListener
{
String s="",s1
double d1,d2
JFrame jf = new JFrame("小计算器by Graduate")
JTextField tf = new JTextField()
public void init()//实现计算器界面
{
Container c=jf.getContentPane()
tf.setHorizontalAlignment(JTextField.RIGHT)//文本框
c.add(tf,"North")
JPanel pn3 = new JPanel(new BorderLayout())
c.add(pn3,"Center")
JPanel pn2 = new JPanel()//功能键界面(清除键和关闭键)
pn2.setLayout(new BorderLayout())
JPanel pn1 = new JPanel()//运算界面
pn1.setLayout(new GridLayout(4,4))
pn3.add(pn2,"North")
pn3.add(pn1)
//设置按钮
JButton b = new JButton("CLEAR")
b.setToolTipText("请按清除键!")//设置清零键
b.setForeground(Color.RED)//设置字体颜色
b.setBackground(Color.YELLOW)//设置背景色
b.addActionListener(this)
pn2.add(b,"Center")
b = new JButton("OFF")
b.setToolTipText("请按退出键!")//设置off键,点击退出应用程序b.addActionListener(this)
b.setForeground(Color.RED)//字体颜色
b.setBackground(Color.ORANGE)//背景色
pn2.add(b,"East")
b = new JButton("1")//add butten 1
b.addActionListener(this)
pn1.add(b)
b = new JButton("2")//add butten 2
b.addActionListener(this)
pn1.add(b)
b = new JButton("3")//add butten 3
b.addActionListener(this)
pn1.add(b)
b = new JButton("+")//add butten +
b.setForeground(Color.BLUE)//设置字体颜色
b.addActionListener(this)
pn1.add(b)
b = new JButton("4")//add butten 4
b.addActionListener(this)
pn1.add(b)
b = new JButton("5")//add butten 5
b.addActionListener(this)
pn1.add(b)
b = new JButton("6")//add button 6
b.addActionListener(this)
pn1.add(b)
b = new JButton("-")//add button -
b.setForeground(Color.BLUE)//设置字体颜色
b.addActionListener(this)
pn1.add(b)
b = new JButton("7")//add button 7
b.addActionListener(this)
pn1.add(b)
b = new JButton("8")//add button 8
b.addActionListener(this)
pn1.add(b)
b = new JButton("9")//add button 9
b.addActionListener(this)
pn1.add(b)
b = new JButton("*")//add button *
b.setForeground(Color.BLUE)//设置字体颜色
b.addActionListener(this)
pn1.add(b)
b = new JButton("0")//add button 0
b.addActionListener(this)
pn1.add(b)
b = new JButton(".")//add button .
b.addActionListener(this)
pn1.add(b)
b = new JButton("=")//add button =
b.setForeground(Color.RED)//设置字体颜色
b.addActionListener(this)
pn1.add(b)
b = new JButton("\\")//add button \
b.setForeground(Color.BLUE)//设置字体颜色
b.addActionListener(this)
pn1.add(b)
jf.setSize(300,300)//设置大小
jf.setVisible(true)//设置为可视
}
//处理按钮按下时的动作,进行相应的处理
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand()
tf.setText(tf.getText()+command)
if(command.equals("CLEAR")) //清零键 按下时返回初始状态
{
s1=null
s=""
tf.setText("")//记录输入值的变量清空
}
else if(command.equals("OFF")) System.exit(0)//off键 关闭应用程序
else if(!command.equals("*")&&!command.equals("\\")
&&!command.equals("+")&&!command.equals("-")
&&!command.equals("="))//判断输入是否为数字
{
if(s1==null)//判断输入是否为第一个
s1 = command
else s1+=command
d1 = new Double(s1).doubleValue()//字符串型转换为双精度型,还原输入数字
try
{
if(s.equals("+")) d1 = d1+d2//加法运算
else if(s.equals("-")) d1 = d2-d1//减法运算
else if(s.equals("*")) d1 = d1*d2//乘法运算
else if(s.equals("\\"))d1 = d2/d1//除法运算
}
catch(Exception ex)
{
tf.setText("Error")//错误显示"Error"
System.out.println(ex.getMessage())
}
}
else if(!command.equals("=")) //判断输入是否为+ - * \
{
s = command
s1 = null
d2 = d1
}
else//输入=时,显示运算结果
{
tf.setText(tf.getText()+d1)
}
}
public static void main(String [] args)
{
new Calculator().init()
}
}
无聊写了个,修复了下Bug:
import java.awt.event.ActionEventimport java.awt.event.ActionListener
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JOptionPane
import javax.swing.JTextField
public class Calculate extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L
private JButton plus, reduce, multiply, divice, reset
private JTextField one, two, result
private boolean device_falg = false
private final int width = 400, height = 300
public Calculate() {
super("修改密码")
this.setLayout(null)
this.setSize(width, height)
init()
Layout()
}
public void init() {
plus = new JButton("加 ")
reduce = new JButton("减 ")
multiply = new JButton("乘 ")
divice = new JButton("除 ")
reset = new JButton("清空")
one = new JTextField()
two = new JTextField()
result = new JTextField()
}
public void Layout() {
this.add(new JLabel("第一个数")).setBounds(20, 10, 60, 80)
this.add(one).setBounds(100, 38, 100, 25)
this.add(new JLabel("第二个数")).setBounds(20, 40, 60, 80)
this.add(two).setBounds(100, 70, 100, 25)
this.add(new JLabel("结果")).setBounds(20, 85, 60, 80)
this.add(result).setBounds(100, 110, 100, 25)
this.add(plus).setBounds(70, 170, 80, 25)
this.add(reduce).setBounds(200, 170, 80, 25)
this.add(multiply).setBounds(70, 200, 80, 25)
this.add(divice).setBounds(200, 200, 80, 25)
this.add(reset).setBounds(300, 220, 80, 25)
plus.addActionListener(this)
reduce.addActionListener(this)
multiply.addActionListener(this)
divice.addActionListener(this)
reset.addActionListener(this)
this.setVisible(true)
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE)
}
public boolean Format() {
boolean FLAG = false
boolean flag = false
String one = this.one.getText().toString().trim()
String two = this.two.getText().toString().trim()
if (one == null || one.equals("") || two == null || two.equals("")) {
JOptionPane.showMessageDialog(getParent(), "请输入完整信息!")
FLAG = false
flag = true
}
boolean boll_1 = one.matches("[\\d]{1,100}")
boolean boll_2 = two.matches("[\\d]{1,100}")
boolean boll_3 = one.matches("[\\d]{1,100}+[.]+[\\d]{1,100}")
boolean boll_4 = two.matches("[\\d]{1,100}+[.]+[\\d]{1,100}")
if (flag) {
return false
}
if ((boll_1 && boll_2) || (boll_3 && boll_4) || (boll_1 && boll_4)
|| (boll_3 && boll_2)) {
FLAG = true
} else {
JOptionPane.showMessageDialog(getParent(), "请输入数字!")
FLAG = false
}
if (FLAG && device_falg) {
if (Double.parseDouble(two) == 0) {
JOptionPane.showMessageDialog(getParent(), "被除数不能为0!")
FLAG = false
device_falg=false
}
}
return FLAG
}
public double Plus(double one, double two) {
return one + two
}
public double Multiply(double one, double two) {
return one * two
}
public double Divice(double one, double two) {
return one / two
}
public double Reduce(double one, double two) {
return one - two
}
public void Clear() {
one.setText("")
two.setText("")
result.setText("")
}
@Override
public void actionPerformed(ActionEvent e) {
Object o = e.getSource()
if (o == reset) {
Clear()
return
}
if (o == divice) {
device_falg = true
}
if (!Format()) {
return
}
double one = Double.parseDouble(this.one.getText())
double two = Double.parseDouble(this.two.getText())
double result = 0
if (o == plus) {
result = Plus(one, two)
} else if (o == reduce) {
result = Reduce(one, two)
} else if (o == multiply) {
result = Multiply(one, two)
} else if (o == divice) {
result = Divice(one, two)
}
this.result.setText("" + result)
}
public static void main(String[] args) {
new Calculate()
}
}
//布局没有调整,需要你自己去调整代码如下:
import java.awt.FlowLayout
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import javax.swing.*
public class JieMian extends JFrame implements ActionListener{
JTextField c1
JTextField c2
JLabel c
JLabel equal=new JLabel("=")
JLabel result
JLabel choose=new JLabel("请选择")
JButton add=new JButton("+")
JButton sub=new JButton("-")
JButton mul=new JButton("×")
JButton div=new JButton("÷")
JButton cal=new JButton("计算")
JButton clear=new JButton("清除")
public JieMian(){
setLayout(new FlowLayout())
c1=new JTextField(5)
c2=new JTextField(5)
c=new JLabel("+")
result=new JLabel("")
add(c1)
add(c)
add(c2)
add(equal)
add(result)
add(choose)
add(add)
add(sub)
add(mul)
add(div)
add(cal)
add(clear)
add.addActionListener(this)
sub.addActionListener(this)
mul.addActionListener(this)
div.addActionListener(this)
cal.addActionListener(this)
clear.addActionListener(this)
setVisible(true)
pack()
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==add){
c.setText("+")
}else if(e.getSource()==sub){
c.setText("-")
}else if(e.getSource()==mul){
c.setText("×")
}else if(e.getSource()==div){
c.setText("÷")
}else if(e.getSource()==cal){
double cc1,cc2
try{
cc1=Double.parseDouble(c1.getText())
}catch(Exception ex){
cc1=0
}try{
cc2=Double.parseDouble(c2.getText())
}catch(Exception ex){
cc2=0
}
if(c.getText()=="+"){
result.setText(String.valueOf(cc1+cc2))
}else if(c.getText()=="-"){
result.setText(String.valueOf(cc1-cc2))
}else if(c.getText()=="×"){
result.setText(String.valueOf(cc1*cc2))
}else if(c.getText()=="÷"){
if(cc2!=0){
result.setText(String.valueOf(cc1/cc2))
}else{
result.setText("NAN")
}
}
}else if(e.getSource()==clear){
c1.setText("")
c2.setText("")
result.setText("")
}
}
public static void main(String args[]){
JieMian jiemian=new JieMian()
}
}