求java高手给这段代码注释
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassCaculator{publ...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Caculator
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Caculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new CaculatorPanel());
frame.pack();
frame.setVisible(true);
}
}
class CaculatorPanel extends JPanel
{
private JLabel namelabel,resultlabel;
private JTextField resultField;
private JPanel labelPanel,numberPanel,toolPanel;
private JButton s1,s2,s3,s4,s5,s6,s7, s8, s9,s0,
p1,p2,t1,t2,t3,t4,clear;
private boolean end,add,sub,mul,div;
private String str;
private double num1,num2;
public CaculatorPanel()
{
namelabel = new JLabel("计算器");
resultField = new JTextField(10);
ButtonListener listener = new ButtonListener();
clear = new JButton("clear");
clear.addActionListener(listener);
s1 = new JButton("1"); s1.addActionListener(listener);
s2 = new JButton("2"); s2.addActionListener(listener);
s3 = new JButton("3"); s3.addActionListener(listener);
s4 = new JButton("4"); s4.addActionListener(listener);
s5 = new JButton("5"); s5.addActionListener(listener);
s6 = new JButton("6"); s6.addActionListener(listener);
s7 = new JButton("7"); s7.addActionListener(listener);
s8 = new JButton("8"); s8.addActionListener(listener);
s9 = new JButton("9"); s9.addActionListener(listener);
s0 = new JButton("0"); s0.addActionListener(listener);
p1 = new JButton("."); p1.addActionListener(listener);
p2 = new JButton("="); p2.addActionListener(listener);
t1 = new JButton("+"); t1.addActionListener(listener);
t2 = new JButton("-"); t2.addActionListener(listener);
t3 = new JButton("*"); t3.addActionListener(listener);
t4 = new JButton("/"); t4.addActionListener(listener);
labelPanel = new JPanel();
labelPanel.add(namelabel);
labelPanel.add(resultField);
labelPanel.add(clear);
numberPanel = new JPanel();
numberPanel.setLayout(new GridLayout(4,3));
numberPanel.add(s1);
numberPanel.add(s2);
numberPanel.add(s3);
numberPanel.add(s4);
numberPanel.add(s5);
numberPanel.add(s6);
numberPanel.add(s7);
numberPanel.add(s8);
numberPanel.add(s9);
numberPanel.add(s0);
numberPanel.add(p1);
numberPanel.add(p2);
toolPanel = new JPanel();
toolPanel.setLayout(new GridLayout(4,1));
toolPanel.add(t1);
toolPanel.add(t2);
toolPanel.add(t3);
toolPanel.add(t4);
setPreferredSize(new Dimension(500,200));
setBackground(Color.cyan);
add(labelPanel);
add(numberPanel);
add(toolPanel);
} public void num(int i){
String s = null;
s=String.valueOf(i);
if(end){
//如果数字输入结束,则将文本框置零,重新输入
resultField.setText("0");
end=false;
}
if((resultField.getText()).equals("0")){
//如果文本框的内容为零,则覆盖文本框的内容
resultField.setText(s);
}没分追问了,谢谢了 展开
import java.awt.event.*;
import javax.swing.*;
public class Caculator
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Caculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new CaculatorPanel());
frame.pack();
frame.setVisible(true);
}
}
class CaculatorPanel extends JPanel
{
private JLabel namelabel,resultlabel;
private JTextField resultField;
private JPanel labelPanel,numberPanel,toolPanel;
private JButton s1,s2,s3,s4,s5,s6,s7, s8, s9,s0,
p1,p2,t1,t2,t3,t4,clear;
private boolean end,add,sub,mul,div;
private String str;
private double num1,num2;
public CaculatorPanel()
{
namelabel = new JLabel("计算器");
resultField = new JTextField(10);
ButtonListener listener = new ButtonListener();
clear = new JButton("clear");
clear.addActionListener(listener);
s1 = new JButton("1"); s1.addActionListener(listener);
s2 = new JButton("2"); s2.addActionListener(listener);
s3 = new JButton("3"); s3.addActionListener(listener);
s4 = new JButton("4"); s4.addActionListener(listener);
s5 = new JButton("5"); s5.addActionListener(listener);
s6 = new JButton("6"); s6.addActionListener(listener);
s7 = new JButton("7"); s7.addActionListener(listener);
s8 = new JButton("8"); s8.addActionListener(listener);
s9 = new JButton("9"); s9.addActionListener(listener);
s0 = new JButton("0"); s0.addActionListener(listener);
p1 = new JButton("."); p1.addActionListener(listener);
p2 = new JButton("="); p2.addActionListener(listener);
t1 = new JButton("+"); t1.addActionListener(listener);
t2 = new JButton("-"); t2.addActionListener(listener);
t3 = new JButton("*"); t3.addActionListener(listener);
t4 = new JButton("/"); t4.addActionListener(listener);
labelPanel = new JPanel();
labelPanel.add(namelabel);
labelPanel.add(resultField);
labelPanel.add(clear);
numberPanel = new JPanel();
numberPanel.setLayout(new GridLayout(4,3));
numberPanel.add(s1);
numberPanel.add(s2);
numberPanel.add(s3);
numberPanel.add(s4);
numberPanel.add(s5);
numberPanel.add(s6);
numberPanel.add(s7);
numberPanel.add(s8);
numberPanel.add(s9);
numberPanel.add(s0);
numberPanel.add(p1);
numberPanel.add(p2);
toolPanel = new JPanel();
toolPanel.setLayout(new GridLayout(4,1));
toolPanel.add(t1);
toolPanel.add(t2);
toolPanel.add(t3);
toolPanel.add(t4);
setPreferredSize(new Dimension(500,200));
setBackground(Color.cyan);
add(labelPanel);
add(numberPanel);
add(toolPanel);
} public void num(int i){
String s = null;
s=String.valueOf(i);
if(end){
//如果数字输入结束,则将文本框置零,重新输入
resultField.setText("0");
end=false;
}
if((resultField.getText()).equals("0")){
//如果文本框的内容为零,则覆盖文本框的内容
resultField.setText(s);
}没分追问了,谢谢了 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询