Home > Back-end >  Calculator error resolution, trouble great god to have a look at where there are problems
Calculator error resolution, trouble great god to have a look at where there are problems

Time:09-18

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
Public class calculate extends JFrame {
JButton [] b=new JButton [10].
JPanel panel1=new JPanel ();//instantiate the panel
JPanel panel2=new JPanel ();
JPanel panel3=new JPanel ();
JTextField answer=new JTextField (10);
JButton clear=new JButton (" remove ");
JButton equal=new JButton ("=");
JButton point=new JButton (". ");
JButton plus=new JButton (" + ");
JButton minus=new JButton (" - ");
JButton multi=new JButton (" * ");
JButton division=new JButton (" present ");
String foreText=new String (" ");
String backText=new String (" ");
String s=new String();
String s1=new String ();
Object the op=new String ();
The Object o=new String ();
Double d, dFore dBack;
Int I=9;
Public static void main (String [] args) {
//TODO Auto - generated method stub
JFrame calculator=new calculate ();
The calculator. SetVisible (true);

}
Public calculate () {
SetTitle (" calculator ");
SetSize (300300);
SetDefaultCloseOperation (JFrame. EXIT_ON_CLOSE);
The Toolkit kit=Toolkit. GetDefaultToolkit ();
Dimension screenSize=kit. GetScreenSize ();//get screen resolution
SetLocation (screenSize. Width/4, screenSize height/4);//position
For (I=9. I>=0; I -) {
[I] b=new JButton (Integer. ToString (I));//instantiate the number button
B [I] addActionListener (new MyListener1 ());
Panel2. Add [I] (b);//add button to panel

}

SetLayout (new FlowLayout ());//set the layout manager
Panel1. SetLayout (new FlowLayout ());
Panel2. SetLayout (new GridLayout (4,3,10,8));
Panel3. SetLayout (new GridLayout (4,1,8,12));
The clear. The setFont (new Font (" official script ", the Font. PLAIN, 15));
The clear setForeground (Color. RED);
The clear addActionListener (new MyListener1 ());
Equal. SetForeground (Color. RED);
Equal. AddActionListener (new MyListener1 ());
Plus. SetForeground (Color. RED);
Plus. AddActionListener (new MyListener1 ());
Minus. SetForeground (Color. RED);
Minus. AddActionListener (new MyListener1 ());
Multi setForeground (Color. RED);
Multi addActionListener (new MyListener1 ());
Division. SetForeground (Color. RED);
Division. AddActionListener (new MyListener1 ());
Equal. The setFont (new Font (" official script ", the Font. PLAIN, 20));
Plus the setFont (new Font (" official script ", the Font. PLAIN, 18));
Minus the setFont (new Font (" official script ", the Font. PLAIN, 18));
Multi setFont (new Font (" official script ", the Font. PLAIN, 18));
Division. The setFont (new Font (" official script ", the Font. PLAIN, 18));
Point. AddActionListener (new MyListener1 ());
Panel1. Add (answer);
Panel1. Add (clear);
Panel2. Add (point);
Panel2. Add (equal);
Panel3. Add (plus);
Panel3. Add (minus);
Panel3. Add (multi);
Panel3. Add (division);
add(panel1);
Add (panel2);
Add (panel3);
}

The class MyListener1 implements ActionListener {
Public void actionPerformed (an ActionEvent e) {

for(int i=0; i<=9; I++) {
If (um participant etSource ()==b [I] | | um participant etSource ()==point) {
Answer. The setText (" ");
If (um participant etSource ()==point) {

S=foreText + ". ";
Answer. The setText (s);
} else {
if(s! {
="")Answer. The setText (s + b [I] getText ());
ForeText=s + b [I] getText ();//should be modified here,
} else {
Answer. SetText (foreText + b [I] getText ());
ForeText=foreText + b [I] getText ();
}
}
}
}
If (um participant etSource ()==clear) {
ForeText="";
S="";
Answer. The setText (" ");
}
If (um participant etSource ()==plus | | um participant etSource ()==minus | | um participant etSource ()==multi | | um participant etSource ()==division) {
DFore=Double. ParseDouble (foreText);
S="";
ForeText="";
Answer. The setText (" ");
Op=um participant etSource ();
}
If (um participant etSource ()==equal) {
DBack=Double. ParseDouble (foreText);
ForeText="";
Answer. The setText (" ");
If (op==plus) {
D=dFore + dBack;
}
If (op==minus) {
D=dFore - dBack;
}
If (op==multi) {
D=dFore * dBack;
}
If (op==division) {
D=dFore/dBack;
}
Answer. The setText (" "+ d);
}
}
}
}
  • Related