- package gui;
- import java.awt.BorderLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- public class NewTestFrame extends FatherFrame{
- private String s="";
- private String[] s1 = new String[2];
- private int[] a = new int[2];
- //******************************************组件方法定义**********************************************************//
- JFrame jf = new JFrame("我的窗体");//创建窗体
- JPanel jp = new JPanel();//创建容器
- JPanel jp1 = new JPanel();
- JButton jb = new JButton("0");//创建按钮
- JButton jb1 = new JButton("1");
- JButton jb2 = new JButton("2");
- JButton jb3 = new JButton("3");
- JButton jb4 = new JButton("4");
- JButton jb5 = new JButton("5");
- JButton jb6 = new JButton("6");
- JButton jb7 = new JButton("7");
- JButton jb8 = new JButton("8");
- JButton jb9 = new JButton("9");
- JButton jb10 = new JButton("+");
- JButton jb11 = new JButton("-");
- JButton jb12 = new JButton("*");
- JButton jb13 = new JButton("/");
- JButton jb14 = new JButton("=");
- JButton jb15 = new JButton("CE");
- JTextField jtf = new JTextField(20);//创建文本
- final JLabel label = new JLabel("=");//内部匿名类只能使用外部类的常量属性
- /**
- * @param args
- */
- public void jiSuanQi(){
-
- }
- //*******************************************运算方法*******************************************//
- public int jiaFa(int a,int b){//加法
- return a+b;
- }
-
- public int jianFa(int a,int b){//减法
- return a-b;
- }
-
- public int chengFa(int a,int b){//乘法
- return a*b;
- }
-
- public int chuFa(int a,int b){//除法
- return a/b;
- }
-
- @Override
- public void initButton() {//添加按钮
- // TODO Auto-generated method stub
- //**********************************************按钮添加****************************************************//
- jp.add(jtf);//向容器中添加文本框
- jtf.setBounds(40, 20,400, 20);
-
- jp.add(label);
- label.setBounds(80, 20, 200, 20);
- jp1.setLayout(new GridLayout(4,4,5,5));//网格布局4*4,间距5*5
- jp1.add(jb);//向容器中添加按钮
- jp1.add(jb1);
- jp1.add(jb2);
- jp1.add(jb3);
- jp1.add(jb4);
- jp1.add(jb5);
- jp1.add(jb6);
- jp1.add(jb7);
- jp1.add(jb8);
- jp1.add(jb9);
- jp1.add(jb10);
- jp1.add(jb11);
- jp1.add(jb12);
- jp1.add(jb13);
- jp1.add(jb14);
- jp1.add(jb15);
- jf.add(jp,BorderLayout.NORTH);//向窗口中添加文本框,并将文本框放在北面
- jf.add(jp1);//
- }
- @Override
- public void initFrame() {//加载窗体
- // TODO Auto-generated method stub
- jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置点击窗体关闭按钮时的效果
- jf.setBounds(100, 100, 350, 350);//设置窗体的大小
- jf.setResizable(false);//设置窗体是否可以拖拽
- jf.setVisible(true);//设置窗体可以显示
- }
- @Override
- public void initLayout() {//设置自定义布局
- // TODO Auto-generated method stub
-
- }
- @Override
- public void initListener() {
- // TODO Auto-generated method stub
- //*******************************************处理事件****************************************************//
- ActionListener listener = new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- //e.getActionCommand()
- //随着按钮点击的不同获取不同按钮上的文字
-
- if(!e.getActionCommand().equals("+")&&!e.getActionCommand().equals("-")&&!e.getActionCommand().equals("/")&&!e.getActionCommand().equals("*")&&!e.getActionCommand().equals("CE")&&!e.getActionCommand().equals("=")){
- System.out.print(e.getActionCommand());//输出选择值
- s += e.getActionCommand();//如果e.getActionCommand()不为=-*/+ce,则为0-9的数字,
- }
-
- if(e.getActionCommand().equals("+")||e.getActionCommand().equals("-")||e.getActionCommand().equals("/")||e.getActionCommand().equals("*")){
- if(a[0] == 0){//如果是第一次计算,或清空后的重新计算,执行
- a[0] = Integer.parseInt(s);
- s = "";
- s1[0] = e.getActionCommand();
- System.out.print(e.getActionCommand());//输出选择值
- }else{//累加计算执行
- a[1] = Integer.parseInt(s);
- if(s1[0].equals("+")){
- a[0] = NewTestFrame.this.jiaFa(a[0], a[1]);
- }else if(s1[0].equals("-")){
- a[0] = NewTestFrame.this.jianFa(a[0], a[1]);
- }else if(s1[0].equals("*")){
- a[0] = NewTestFrame.this.chengFa(a[0], a[1]);
- }else if(s1[0].equals("/")){
- a[0] = NewTestFrame.this.chuFa(a[0], a[1]);
- }
- System.out.println();
- System.out.print(a[0]);
- System.out.print(e.getActionCommand());//输出选择值
- s1[1] = "="+a[0];
- label.setText(s1[1]);
- s1[0] = e.getActionCommand();
- a[1] = 0;
- s="";
- }
- }
-
- if(e.getActionCommand().equals("=")){//如果是等号执行
- if(a[0] == 0){//
- a[0] = Integer.parseInt(s);
- s = "";
- System.out.println();
- System.out.println("结果为:"+a[0]);
- s1[1] = "="+a[0];
- label.setText(s1[1]);
- }else{
- a[1] = Integer.parseInt(s);
- if(s1[0].equals("+")){
- a[0] = NewTestFrame.this.jiaFa(a[0], a[1]);
- }else if(s1[0].equals("-")){
- a[0] = NewTestFrame.this.jianFa(a[0], a[1]);
- }else if(s1[0].equals("*")){
- a[0] = NewTestFrame.this.chengFa(a[0], a[1]);
- }else if(s1[0].equals("/")){
- a[0] = NewTestFrame.this.chuFa(a[0], a[1]);
- }
- System.out.println();
- System.out.print(e.getActionCommand());//输出选择值
- System.out.print(a[0]);
- s1[1] = "="+a[0];
- label.setText(s1[1]);
- s1[0] = e.getActionCommand();
- a[1] = 0;
- s="";
- }
- }
-
- if(e.getActionCommand().equals("CE")){//如果是CE,清空
- a[0] = 0;
- a[1] = 0;
- s = "";
- s1[0] = "";
- s1[1] = "=";
- label.setText(s1[1]);
- }
-
- }
- };
-
- //******************************************加载事件*****************************************************//
- jb.addActionListener(listener);//加载事件
- jb1.addActionListener(listener);
- jb2.addActionListener(listener);
- jb3.addActionListener(listener);
- jb4.addActionListener(listener);
- jb5.addActionListener(listener);
- jb6.addActionListener(listener);
- jb7.addActionListener(listener);
- jb8.addActionListener(listener);
- jb9.addActionListener(listener);
- jb10.addActionListener(listener);
- jb11.addActionListener(listener);
- jb12.addActionListener(listener);
- jb13.addActionListener(listener);
- jb14.addActionListener(listener);
- jb15.addActionListener(listener);
- }
- @Override
- public void initPostion() {
- // TODO Auto-generated method stub
-
- }
-
- public static void main(String[] args) {//主方法
- NewTestFrame ntf = new NewTestFrame();
- ntf.init();
- }
- }
复制代码
|
|