黑马程序员技术交流社区
标题:
计算器的小实验,欢迎大家交流
[打印本页]
作者:
马也keyboard
时间:
2015-6-23 23:01
标题:
计算器的小实验,欢迎大家交流
package com.wipe.zc.GUIDemoTest;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CaculateTest {
static JFrame f;
static JPanel p1,p2;
static JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b_and,b_cut,b_mul,b_div,b_equ;
static JButton[] btn = {b7,b8,b9,b_mul,b_div,b4,b5,b6,b_and,b_cut,b1,b2,b3,b0,b_equ};
static JButton btn_ce;
static JTextField t;
static String str = "";
static float count = 0;
public static void main(String[] args)
{
init();
}
public static void init()
{
f = new JFrame("myCaculate");
f.setBounds(300, 200, 300, 200);
f.setLayout(new BorderLayout());
p1 = new JPanel();
p1.setLayout(new FlowLayout());
p2 = new JPanel();
p2.setLayout(new GridLayout(3,5));
btn[0] = new JButton("7");
btn[1] = new JButton("8");
btn[2] = new JButton("9");
btn[3] = new JButton("*");
btn[4] = new JButton("/");
btn[5] = new JButton("4");
btn[6] = new JButton("5");
btn[7] = new JButton("6");
btn[8] = new JButton("+");
btn[9] = new JButton("-");
btn[10] = new JButton("1");
btn[11] = new JButton("2");
btn[12] = new JButton("3");
btn[13] = new JButton("0");
btn[14] = new JButton("=");
t = new JTextField(20);
btn_ce = new JButton("ce");
p1.add(t);
p1.add(btn_ce);
for(int i = 0;i<15;i++)
{
p2.add(btn[i]);
}
f.add(p1,BorderLayout.NORTH);
f.add(p2,BorderLayout.CENTER);
f.setResizable(false);
p1.setVisible(true);
p2.setVisible(true);
f.setVisible(true);
myEvent();
}
public static void myEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//按键7
btn[0].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
str = str + "7";
t.setText(str);
}
});
//按键8
btn[1].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
str = str + "8";
t.setText(str);
}
});
//按键9
btn[2].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
str = str + "9";
t.setText(str);
}
});
//按键x
btn[3].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(str.contains("+")||str.contains("-")||str.contains("*")||str.contains("/"))
{
if(str.endsWith("+")||str.endsWith("-")||str.endsWith("*")||str.endsWith("/"))
{
}else
{
equal();
str = count + "*";
t.setText(str);
}
}else
{
if(str.length()>=1)
{
str = str + "+";
t.setText(str);
}
}
}
});
//按键÷
btn[4].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(str.contains("+")||str.contains("-")||str.contains("*")||str.contains("/"))
{
if(str.endsWith("+")||str.endsWith("-")||str.endsWith("*")||str.endsWith("/"))
{
}else
{
equal();
str = count + "/";
t.setText(str);
}
}else
{
if(str.length()>=1)
{
str = str + "+";
t.setText(str);
}
}
}
});
//按键4
btn[5].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
str = str + "4";
t.setText(str);
}
});
//按键5
btn[6].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
str = str + "5";
t.setText(str);
}
});
//按键6
btn[7].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
str = str + "6";
t.setText(str);
}
});
//按键+
btn[8].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(str.contains("+")||str.contains("-")||str.contains("*")||str.contains("/"))
{
if(str.endsWith("+")||str.endsWith("-")||str.endsWith("*")||str.endsWith("/"))
{
}else
{
equal();
str = count + "+";
t.setText(str);
}
}else
{
if(str.length()>=1)
{
str = str + "+";
t.setText(str);
}
}
}
});
//按键-
btn[9].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(str.contains("+")||str.contains("-")||str.contains("*")||str.contains("/"))
{
if(str.endsWith("+")||str.endsWith("-")||str.endsWith("*")||str.endsWith("/"))
{
}else
{
equal();
str = count + "-";
t.setText(str);
}
}else
{
if(str.length()>=1)
{
str = str + "+";
t.setText(str);
}
}
}
});
//按键1
btn[10].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
str = str + "1";
t.setText(str);
}
});
//按键2
btn[11].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
str = str + "2";
t.setText(str);
}
});
//按键3
btn[12].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
str = str + "3";
t.setText(str);
}
});
//按键0
btn[13].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
str = str + "0";
t.setText(str);
}
});
//按键=
btn[14].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
equal();
}
});
btn_ce.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
str = "";
count = 0;
t.setText(str);
}
});
}
public static void equal()
{
if(str.endsWith("+") == false&&str.endsWith("-") == false&&str.endsWith("*") == false&&str.endsWith("/") == false)
{
if(str.contains("*"))
{
System.out.println(str);
String[] s = str.split("[*]");
float n1 = Float.parseFloat(s[0]);
float n2 = Float.parseFloat(s[1]);
count = n1*n2;
if((int)count == count)
{
int n = (int)count;
System.out.println(n);
str = n + "";
t.setText(str);
}else
{
str = count+"";
t.setText(str);
}
}
if(str.contains("/"))
{
String[] s = str.split("[/]");
float n1 = Float.parseFloat(s[0]);
float n2 = Float.parseFloat(s[1]);
count = n1/n2;
if((int)count == count)
{
int n = (int)count;
System.out.println(n);
str = n + "";
t.setText(str);
}else
{
str = count+"";
t.setText(str);
}
}
if(str.contains("+"))
{
String[] s = str.split("[+]");
float n1 = Float.parseFloat(s[0]);
float n2 = Float.parseFloat(s[1]);
count = n1+n2;
if((int)count == count)
{
int n = (int)count;
System.out.println(n);
str = n + "";
t.setText(str);
}else
{
str = count+"";
t.setText(str);
}
}
if(str.contains("-"))
{
String[] s = str.split("[-]");
float n1 = Float.parseFloat(s[0]);
float n2 = Float.parseFloat(s[1]);
count = n1-n2;
if((int)count == count)
{
int n = (int)count;
System.out.println(n);
str = n + "";
t.setText(str);
}else
{
str = count+"";
t.setText(str);
}
}
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2