这是我找的一个计算器的程序,可是有点地方我看不明白,求哪位大神帮我注释下啊!package awt;import java.awt.*;
import java.awt.event.*;
import javax.swing.*; public class Calculator1
{
public static void main(String[] args)
{
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
setTitle("Calculator");
CalculatorPanel panel = new CalculatorPanel();
add(panel);
pack();
}
}
class CalculatorPanel extends JPanel
{
JButton display;
JPanel panel;
double result;
String lastCommand;
boolean start;
public CalculatorPanel()
{
setLayout(new BorderLayout()); result = 0;
lastCommand = "=";
start = true;
display = new JButton("0");
display.setEnabled(false);
add(display,BorderLayout.NORTH);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction(); panel = new JPanel();
panel.setLayout(new GridLayout(4,4)); addButton( "7", insert);
addButton( "8", insert);
addButton( "9", insert);
addButton( "/", command); addButton( "4", insert);
addButton( "5", insert);
addButton( "6", insert);
addButton( "*", command); addButton( "1", insert);
addButton( "2", insert);
addButton( "3", insert);
addButton( "-", command); addButton( "0", insert);
addButton( ".", insert);
addButton( "=", command);
addButton( "+", command); add(panel,BorderLayout.CENTER);
}
void addButton(String label, ActionListener listener)
{
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input = event.getActionCommand();
if(start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
} class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if(start)
{
if(command.equals("-"))
{
display.setText(command);
start = false;
}
else
lastCommand = command;
}
else
{
calculate(Double.parseDouble(display.getText()));
lastCommand = command;
start = true;
}
}
}
public void calculate(double x)
{
if(lastCommand.equals("+"))
result += x;
else
if(lastCommand.equals("-"))
result -= x;
else
if(lastCommand.equals("*"))
result *= x;
else
if(lastCommand.equals("/"))
result /= x;
else
if(lastCommand.equals("="))
result = x;
display.setText("" + result);
}
}
import java.awt.event.*;
import javax.swing.*; public class Calculator1
{
public static void main(String[] args)
{
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
setTitle("Calculator");
CalculatorPanel panel = new CalculatorPanel();
add(panel);
pack();
}
}
class CalculatorPanel extends JPanel
{
JButton display;
JPanel panel;
double result;
String lastCommand;
boolean start;
public CalculatorPanel()
{
setLayout(new BorderLayout()); result = 0;
lastCommand = "=";
start = true;
display = new JButton("0");
display.setEnabled(false);
add(display,BorderLayout.NORTH);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction(); panel = new JPanel();
panel.setLayout(new GridLayout(4,4)); addButton( "7", insert);
addButton( "8", insert);
addButton( "9", insert);
addButton( "/", command); addButton( "4", insert);
addButton( "5", insert);
addButton( "6", insert);
addButton( "*", command); addButton( "1", insert);
addButton( "2", insert);
addButton( "3", insert);
addButton( "-", command); addButton( "0", insert);
addButton( ".", insert);
addButton( "=", command);
addButton( "+", command); add(panel,BorderLayout.CENTER);
}
void addButton(String label, ActionListener listener)
{
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input = event.getActionCommand();
if(start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
} class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if(start)
{
if(command.equals("-"))
{
display.setText(command);
start = false;
}
else
lastCommand = command;
}
else
{
calculate(Double.parseDouble(display.getText()));
lastCommand = command;
start = true;
}
}
}
public void calculate(double x)
{
if(lastCommand.equals("+"))
result += x;
else
if(lastCommand.equals("-"))
result -= x;
else
if(lastCommand.equals("*"))
result *= x;
else
if(lastCommand.equals("/"))
result /= x;
else
if(lastCommand.equals("="))
result = x;
display.setText("" + result);
}
}
import java.awt.event.*;import javax.swing.*;public class JiSuanQi
{
public static void main(String[] args)
{
new Calculator("Calculator");
}
}
@SuppressWarnings("serial")
class Calculator extends JFrame implements ActionListener
{
JTextField text;
JPanel jp;
JButton a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,dian,dengyu,jia;
public Calculator()
{}
public Calculator(String telie)
{
super(telie);
this.setBounds(430, 270, 240, 210);
this.setBackground(Color.lightGray);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
text = new JTextField("0.",20);
text.setHorizontalAlignment(JTextField.RIGHT);
this.add(text);
jp = new JPanel(new GridLayout(5,4));
this.add(jp);
a7 =new JButton("7");
a8 =new JButton("8");
a9 =new JButton("9");
a4 =new JButton("4");
a5 =new JButton("5");
a6 =new JButton("6");
a1 =new JButton("1");
a2 =new JButton("2");
a3 =new JButton("3");
a0 =new JButton("0");
dian =new JButton(".");
dengyu =new JButton("=");
jia =new JButton("+");
jp.add(new JButton("sqrt"));
jp.add(new JButton("+/-"));
jp.add(new JButton("CE"));
jp.add(new JButton("C"));
jp.add(a7);
jp.add(a8);
jp.add(a9);
jp.add(new JButton("/"));
jp.add(a4);
jp.add(a5);
jp.add(a6);
jp.add(new JButton("*"));
jp.add(a1);
jp.add(a2);
jp.add(a3);
jp.add(new JButton("-"));
jp.add(a0);
jp.add(dian);
jp.add(dengyu);
jp.add(jia);
a0.addActionListener(this);
a1.addActionListener(this);
a2.addActionListener(this);
a3.addActionListener(this);
a4.addActionListener(this);
a5.addActionListener(this);
a6.addActionListener(this);
a7.addActionListener(this);
a8.addActionListener(this);
a9.addActionListener(this);
dian.addActionListener(this);
dengyu.addActionListener(this);
jia.addActionListener(this);
this.setVisible(true);
this.setResizable(false);
}
@Override
public void actionPerformed(ActionEvent e)
{
String s1="";
boolean bo=true;
double d1 = 0,d2 = 0,d3 = 0;
if(bo)
{
text.setText(null);
bo=false;
}
try
{
if(e.getSource()==a0)
{
if(s1.charAt(0)!='0')
s1+="0";
text.setText(s1);
}
if(e.getSource()==a1)
{
s1+="1";
text.setText(s1);
}
if(e.getSource()==a2)
{
s1+="2";
text.setText(s1);
}
if(e.getSource()==a3)
{
s1+="3";
text.setText(s1);
}
if(e.getSource()==a4)
{
s1+="4";
text.setText(s1);
}
if(e.getSource()==a5)
{
s1+="5";
text.setText(s1);
}
if(e.getSource()==a6)
{
s1+="6";
text.setText(s1);
}
if(e.getSource()==a7)
{
s1+="7";
text.setText(s1);
}
if(e.getSource()==a8)
{
s1+="8";
text.setText(s1);
}
if(e.getSource()==a9)
{
s1+="9";
text.setText(s1);
}
if(e.getSource()==dian)
{
int j=0;
for(int i=0;i<s1.length();i++)
if(s1.charAt(i)!='.')
j++;
if(j==s1.length())
s1+=".";
text.setText(s1);
}
if(e.getSource()==dengyu)
{
text.setText(String.valueOf(d3));
bo=true;
}
if(e.getSource()==jia)
{
boolean bo2=true;
if(bo2)
{
d1=Double.parseDouble(s1);
bo2=false;
}
else
{
d2=Double.parseDouble(s1);
bo2=true;
}
d3=d1+d2;
text.setText(s1);
}
}
catch(NumberFormatException e1)
{
JOptionPane.showMessageDialog(this, "输入错误,请重新输入!");
}
}
}
这是我以前写的 加了详细的注释,你去看看吧