This program is an instance tried to understand the swing application. Although it is a simple calculator program, three classes are designed.
In the design process, data is completely separated from the interface, and the interface and user event processing are also completely separated. Therefore
Personally, I think the design is good. If anyone has a better design, share it and let me improve it. Here
In the same design, some details have not met my ideal requirements, but due to the rush of time, I will not think further.
The following describes the concept:
The overall design is divided into three categories. calcgui is responsible for displaying the interface, calcevent is responsible for user-triggered events, and calclogic is responsible
Processing Algorithm. These three categories are responsible for completing the computing process collaboratively.
I. The calcgui class contains four panels
, One root panel and three container panels. The root panel is packed with three container panels. The following describes
Container panel.
1. Top panel Packaging
Memory status, display, and backspace of the rollback key, error clearing key ce and clearing key C
2. left panel packaging 4 memory-related key MC MS
Mr M +
3. right panel Packaging
Remaining number keys and symbol keys
All events triggered by a user are key events, so each key must have a corresponding event processing process, and so many buttons cannot be added one by one, therefore, the calcevent class is specially designed to handle key events. When a button is generated in the calcgui class, a calcevent instance is created to process the corresponding event. In order to separate the interface from the event, the calcevent class stores the handle of each key. Therefore, all key events are listened to and processed in their own calcevent. The core code of this part is
Buttons [I]
= New jbutton (calcevent. actioncommands [I]);
Buttons [I]. setborder (borderfactory. createemptyborder (5,
5, 5, 5 ));
New
Calcevent (buttons [I], displaytext, memorylabel );
To dynamically refresh the screen, you also need to update the screen and status in event processing. Therefore, calcevent also includes
Handle of the screen text and status label. When an event instance is generated, the buttons, text, and label handles in the GUI are passed in. New
Calcevent (buttons [I], displaytext, memorylabel );
2. The calcevent class designs the numbers or symbols on each key as commands. When constructing an instance, it associates the corresponding commands with the symbols based on the commands passed in by the GUI, as shown in figure
This. Button = button;
This. Text
= Text;
This. Label
= Label;
Setcommand ();
Because the computing logic is for the entire calculator program, only one calclogic instance needs to be static, because all events share a calclogic instance for computing.
Therefore, save a static calclogic instance in the calcevent class.
Private Static calclogic calculate = new
Calclogic ();
III. The calclogic class executes all computing functions. This part mainly includes important algorithms. Here I will not explain specific algorithms, because they are completely benevolent, wise, and not excellent, if you are interested, you can refer to it.
The following code is used:
Calcgui class
Package calculator;
Import java. AWT .*;
Import javax. Swing .*;
// Import java. Text. decimalformat;
// Import java. Text. parseexception;
Public class calcgui extends jpanel {
Jtextfield displaytext;
Jlabel memorylabel;
Private Static final int button_num = 27;
Jbutton [] buttons = new jbutton [button_num];
Public calcgui (){
// Displaytext = new jformattedtextfield (New decimalformat ("#0 .#"));
Displaytext = new jtextfield ("0.", 17 );
// Displaytext. setfocuslostbehavior (jformattedtextfield. commit_or_revert );
Displaytext. sethorizontalalignment (jtextfield. Right );
// Displaytext. seteditable (false );
/* Try {
Displaytext. settext ("0 .");
Displaytext. commitedit ();
} Catch (parseexception e ){
E. printstacktrace ();
}*/
Memorylabel = new jlabel ();
Memorylabel. setborder (borderfactory. createemptyborder (5, 10, 10, 5 ));
Memorylabel. sethorizontalalignment (jlabel. Right );
For (INT I = 0; I <button_num; I ++ ){
Buttons [I] = new jbutton (calcevent. actioncommands [I]);
Buttons [I]. setborder (borderfactory. createemptyborder (5, 5, 5 ));
// Calcevent event = new calcevent (buttons [I], displaytext );
New calcevent (buttons [I], displaytext, memorylabel );
}
// Toppanel packaging result display, memory screen,
// And the rollback key. The error clearing key is backspace ce c.
Jpanel toppanel = new jpanel ();
Toppanel. setlayout (New gridbaglayout ());
Toppanel. setborder (borderfactory. createemptyborder (10, 10, 10 ));
Gridbagconstraints c = new gridbagconstraints ();
C. Fill = gridbagconstraints. Both;
C. weightx = 0.5;
C. gridx = 0;
C. gridy = 0;
Toppanel. Add (memorylabel, C );
C. gridx = 1;
C. gridwidth = 3;
Toppanel. Add (displaytext, C );
C. gridx = 1;
C. gridy = 1;
C. gridwidth = 1;
C. weightx = 0.5;
C. weighty = 1.0;
Toppanel. Add (buttons [20], C );
C. gridx = 2;
C. gridy = 1;
Toppanel. Add (buttons [21], C );
C. gridx = 3;
C. gridy = 1;
Toppanel. Add (buttons [22], C );
// The four keys of the memory function of the leftpanel packaging are MC Mr MS m +
Jpanel leftpanel = new jpanel ();
Leftpanel. setlayout (New boxlayout (leftpanel, boxlayout. page_axis ));
Leftpanel. setborder (borderfactory. createemptyborder (10, 10, 10 ));
Leftpanel. Add (buttons [23]);
Leftpanel. Add (buttons [24]);
Leftpanel. Add (buttons [25]);
Leftpanel. Add (buttons [26]);
// Rightpanel packs the number key and some computing keys
Jpanel rightpanel = new jpanel ();
Rightpanel. setlayout (New gridlayout (4, 5 ));
Rightpanel. setborder (borderfactory. createemptyborder (10, 10, 10 ));
For (INT I = 0; I <button_num-7; I ++ ){
Rightpanel. Add (buttons [I]);
}
// The above three slave panels are finally loaded by the main panel
Setlayout (New borderlayout ());
Setborder (borderfactory. createemptyborder (10, 10, 10, 10 ));
Add (toppanel, borderlayout. page_start );
Add (leftpanel, borderlayout. line_start );
Add (rightpanel, borderlayout. center );
}
Private Static void createandshowgui (){
Jframe. setdefalooklookandfeeldecorated (true );
// New A calcgui instance here
// Calcgui calculator = new calcgui ();
Jframe frame = new jframe ("Calculator ");
Frame. setdefaclocloseoperation (jframe. exit_on_close );
// Add my component here
Frame. getcontentpane (). Add (New calcgui ());
Frame. Pack ();
Frame. setvisible (true );
}
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Swingutilities. invokelater (New runnable (){
Public void run (){
Createandshowgui ();
}
});
}
}
Calcevent class
Package calculator;
Import javax. Swing .*;
// Import java. AWT .*;
Import java. AWT. event .*;
Public class calcevent implements actionlistener {
Private jbutton button;
Private jtextfield text;
Private jlabel label;
Private string command;
Private Static calclogic calculate = new calclogic ();
Final Static int command_num = 27;
Static string [] actioncommands = new string []
{
New String ("7 "),
// 0
New String ("8 "),
// 1
New String ("9 "),
// 2
New String ("/"),
// 3
New String ("SQRT "),
// 4
New String ("4 "),
// 5
New String ("5 "),
// 6
New String ("6 "),
// 7
New String ("*"),
// 8
New String ("% "),
// 9
New String ("1 "),
// 10
New String ("2 "),
// 11
New String ("3 "),
// 12
New String ("-"),
// 13
New String ("1/X "),
// 14
New String ("0 "),
// 15
New String ("+ /-"),
// 16
New String ("."),
// 17
New String ("+ "),
// 18
New String ("= "),
// 19
New String ("backspace"), // 20
New
String ("CE"), // 21
New
String ("C"), // 22
New
String ("MC"), // 23
New
String ("Mr"), // 24
New
String ("Ms"), // 25
New
String ("m +"), // 26
};
Public calcevent (jbutton, jtextfield text, jlabel
Label ){
This. Button = button;
This. Text = text;
This. Label = label;
Setcommand ();
}
Private void setcommand (){
For (INT I = 0; I <command_num; I ++)
{
If
(Actioncommands [I]. Equals (button. gettext ())){
Button. setactioncommand (actioncommands [I]);
Button. addactionlistener (this );
Command = actioncommands [I];
Break;
}
}
}
Public void actionreceivmed (actionevent e ){
If
(".". Equals (E. getactioncommand () |
"0". Equals (E. getactioncommand () |
"1". Equals (E. getactioncommand () |
"2". Equals (E. getactioncommand () |
"3". Equals (E. getactioncommand () |
"4". Equals (E. getactioncommand () |
"5". Equals (E. getactioncommand () |
"6". Equals (E. getactioncommand () |
"7". Equals (E. getactioncommand () |
"8". Equals (E. getactioncommand () |
"9". Equals (E. getactioncommand ())){
Text. settext (calculate. procnum (command, text. gettext ()));
} Else {
Text. settext (calculate. procsym (command ));
Label. settext (calculate. returnmemory ());
}
}
}