Import the Java. The awt. Color;
Import the Java. The awt. Container;
Import the Java. The awt. GridLayout;
Import the Java. The awt. Event. An ActionEvent;
Import the Java. The awt. Event. ActionListener;
import javax.swing.*;
Public class Cal extends JFrame {
Private JTextField m_jtData;
Private String strName []={" 1 ", "2", "3", "+",
"4", "5", "6", "-",
"7", "eight" and "9", "*",
". ", "0", "=", "/"
};
Private JButton [] m_jbOperate;
Public Cal () {
Enclosing setBounds (300, 100, 300, 350);
Enclosing setTitle (" calculator ");
Enclosing setDefaultCloseOperation (EXIT_ON_CLOSE);
CreatePane ();
this.setVisible(true);
}
Private void CreatePane () {
//
the text box input datam_jtData=https://bbs.csdn.net/topics/new JTextField ();
M_jtData. SetBackground (Color. Yellow);
M_jtData. SetEditable (false);
M_jtData. SetHorizontalAlignment (JTextField. RIGHT);
M_jtData. SetText (" 0 ");
//action button
//JPanel opPane=new JPanel ();
.//opPane setLayout (new GridLayout (4,4,10,10));
JPanel opPane=new JPanel (new GridLayout (4,4,30,20));//intermediate container
M_jbOperate=new JButton [16].
MyEvent my=new MyEvent ();//action button action to monitor objects
for(int i=0; I
X.a ddActionListener (my);
OpPane. Add (x);
M_jbOperate [I]=x;
}
//assemble
The Container ca=this. GetContentPane ();
Ca. SetLayout (new BorderLayout (10, 10));
Ca. The add (m_jtData, BorderLayout. NORTH);
Ca. The add (opPane, BorderLayout. CENTER);
Ca. The add (new JLabel (" "), BorderLayout. EAST);
Ca. The add (new JLabel (" "), BorderLayout. WEST);
Ca. The add (new JLabel (" "), BorderLayout. SOUTH);
}
The class MyEvent implements ActionListener {
Long data1, data2;
Int opType;
Void setData (num) {
Long a, b;
String TXT.
TXT=m_jtData. GetText ();
A=Long. ParseLong (TXT);
B=a * 10 + num;
M_jtData. SetText (" "+ b);
}
@ Override
Public void actionPerformed (an ActionEvent e) {
JButton jb=(JButton) um participant etSource ();
Long a, b;
String TXT.
If (jb==m_jbOperate [0]) {//1
SetData (1);
} else if (jb==m_jbOperate [1]) {//2
SetData (2);
} else if (jb==m_jbOperate [2]) {//3
SetData (3);
} else if (jb==m_jbOperate [3]) {//+
Enclosing opType=1;
This. Data1=Long. ParseLong (m_jtData getText ());
M_jtData. SetText (" 0 ");
} else if (jb==m_jbOperate [4]) {//4
SetData (4);
} else if (jb==m_jbOperate [5]) {//5
SetData (5);
} else if (jb==m_jbOperate [6]) {//6
SetData (6);
} else if (jb==m_jbOperate [7]) {//-
Enclosing opType=2;
This. Data1=Long. ParseLong (m_jtData getText ());
M_jtData. SetText (" 0 ");
} else if (jb==m_jbOperate [8]) {//7
SetData (7);
} else if (jb==m_jbOperate [9]) {//8
SetData (8);
} else if (jb==m_jbOperate [10]) {//9
SetData (9);
} else if (jb==m_jbOperate [11]) {//*
Enclosing opType=3;
This. Data1=Long. ParseLong (m_jtData getText ());
M_jtData. SetText (" 0 ");
} else if (jb==m_jbOperate [12]) {//.
} else if (jb==m_jbOperate [13]) {//0
SetData (0);
} else if (jb==m_jbOperate [14]) {//=
This. Data2=Long. ParseLong (m_jtData getText ());
The switch (enclosing opType) {
Case 1:
M_jtData. SetText (enclosing data1 + enclosing data2 + "");
break;
Case 2:
M_jtData. SetText (enclosing data1 - this. Data2 + "");
break;
Case 3:
M_jtData. SetText (enclosing data1. * this data2 + "");
break;
Case 4:
If (this. Data2==0) {
M_jtData. SetText (" ERROR ");
} else {
M_jtData. SetText (enclosing data1/enclosing data2 + "");
}
break;
}
} else if (jb==m_jbOperate [15]) {///division
Enclosing opType=4;
This. Data1=Long. ParseLong (m_jtData getText ());
M_jtData. SetText (" 0 ");
} else {
}
}
}