图形用户界面是使用图形的方式借助窗口、文本行、按钮
实现代码:
package mypackage;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class caculator extends JFrame{
//用来显示数值
JLabel show = new JLabel();
JPanel main=new JPanel();
JPanel numPanel= new JPanel();
boolean start;//判断是否重新开始
int count;//小数点出现次数
double result;//结果接收结果
String comd;//接收符号
public static void main(String[] args) {
new caculator();
}
public caculator(){
init();
}
//布局的方法
private void init(){
//给窗体的大小
this.setSize(500,300);
//窗体可见
this.setBackground(java.awt.Color.lightGray);
this.setVisible(true);
//让窗体居中的方法
this.setLocationRelativeTo(null);
//设置计算器点击退出则程序关闭
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//设置标题
this.setTitle("Lzz的第一个计算器");
//给显示框设置默认值0
show.setText("0");
//默认开始
start=true;
//默认小数点出现次数
count=0;
//默认为等
comd="=";
//给按钮面板设置布局
numPanel.setLayout(new GridLayout(5,4));
//准备监听对象
Actionl actionl=new Actionl();
Action2 action2=new Action2();
Action3 action3=new Action3();
//添加按钮
this.addButton("Off", action3);
this.addButton("TIME", action3);
this.addButton("BREAKSPACE", action3);
this.addButton("C", action3);
this.addButton("7", actionl);
this.addButton("8", actionl);
this.addButton("9", actionl);
this.addButton("+", action2);
this.addButton("4", actionl);
this.addButton("5", actionl);
this.addButton("6", actionl);
this.addButton("-", action2);
this.addButton("1", actionl);
this.addButton("2", actionl);
this.addButton("3", actionl);
this.addButton("*", action2);
this.addButton(".", actionl);
this.addButton("0", actionl);
this.addButton("=", action2);
this.addButton("/", action2);
//给主面版设为边框布局
main.setLayout(new BorderLayout());
//让显示框位于主面版北方
main.add(show ,"North");
main.add(numPanel);
this.add(main);
}
//添加按钮并给给设置监听按钮的方法
public void addButton(String num,ActionListener al) {
JButton button=new JButton(num);
button.addActionListener(al);
numPanel.add(button);
}
//数字键的监听方法
class Actionl implements ActionListener{
public void actionPerformed(ActionEvent e) {
// 监听者监听到之后要做的事
String input = e.getActionCommand();
if(show.getText().equals("0")){
start=true;
}
if(start){
count=0;//小数点归0重新开始
show.setText("");
start=false;
}
if(input.equals(".")){
if(count==0){
show.setText(show.getText()+input);
}else{
show.setText(show.getText()+"");
}
count++;
}
if(!input.equals(".")){
show.setText(show.getText()+input);
}
}
}
//符号键的监听方法
class Action2 implements ActionListener{
public void actionPerformed(ActionEvent e) {
String input=e.getActionCommand();
if(start){
comd=input;
}else{
if(comd.equals("+")){
//将String类型的数据转换成double类型的数字
result += Double.parseDouble(show.getText());
}
else if(comd.equals("-")){
//将String类型的数据转换成double类型的数字
result -= Double.parseDouble(show.getText());
}
else if(comd.equals("*")){
//将String类型的数据转换成double类型的数字
result *= Double.parseDouble(show.getText());
}
else if(comd.equals("/")){
//将String类型的数据转换成double类型的数字
result /= Double.parseDouble(show.getText());
}else{
result = Double.parseDouble(show.getText());
}
}
show.setText(result+"");
comd=input;
start=true;
}
}
//功能键的监听方法
class Action3 implements ActionListener{
public void actionPerformed(ActionEvent e) {
String input=e.getActionCommand();
if(input.equals("TIME")){
show.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
start=true;
}
if(input.equals("C")){
show.setText("0");
}
if(input.equals("Off")){
show.setText("");
start =true;
}
if(input.equals("BREAKSPACE")){
if(show.getText().length()>1){
show.setText(show.getText().substring(0,show.getText().length()-1));//截取之前的
}else{
show.setText("0");
}
}
}
}
}