请教大家,书上有一个使用applet编写计算器的例子,但是运行时,窗口刷屏,是什么原因?是不是resize()方法用的不对?
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.*;
public class CalculatorApplet extends Applet implements ActionListener{
    private String lastOptr;
    private String Filler="     ";
    private boolean sawDecimal,newOpd,sawAnOpd;
    private int appletWidth,appletHeight;
    private double opd1;
    private Button c,eq,eq2,div,d1,d2,d3,d4,d5,d6,d7,d8,d9,d0,plus,minus,times,dp;
    private Color backgroundColor; 
    private Label result;
    private Panel row1,row2,row3,p12,p120,p3p,p3peq; 
    static final String initialString="0.00000";
    static final String digits="0123456789";
    static final String operators="+-*/=";
        private Button makeButton(String label,Color color){
Button b=new Button(label);
b.setBackground(color);
b.setFont(new Font("Courtie",Font.BOLD,10));
return b;
}
private void makeButtons(){
Color lightRed=new Color(255,100,100);
Color lightBlue=new Color(100,255,255);
Color yellow=new Color(255,255,0);
c=makeButton("C",lightRed);
eq=makeButton("=",lightBlue);
div=makeButton("/",lightRed);
times=makeButton("*",lightBlue);
d7=makeButton("7",yellow);
d8=makeButton("8",yellow);
d9=makeButton("9",yellow);
minus=makeButton("-",lightBlue);
d4=makeButton("4",yellow);
d5=makeButton("5",yellow);
d6=makeButton("6",yellow); 
plus=makeButton("+",lightBlue);
d1=makeButton("1",yellow);
d2=makeButton("2",yellow);
d3=makeButton("3",yellow);
dp=makeButton(".",yellow);
d0=makeButton("0",yellow);
eq2=makeButton("=",lightBlue);
}
private Panel makePanel(LayoutManager lm,Color c){
Panel p=new Panel();
p.setLayout(lm);
p.setBackground(c);
return p;
}
public void init() {
// Put your code here
backgroundColor=new Color(200,255,255);
this.setLayout(new FlowLayout(FlowLayout.CENTER,4,1));
result=new Label("0.0000",Label.RIGHT);
result.setBackground(new Color(255,255,255));
add(result);
makeButtons();
row1=makePanel(new FlowLayout(FlowLayout.LEFT,4,2),
       backgroundColor);
row1.add(c);
row1.add(eq);
row1.add(div);
row1.add(times);
row2=makePanel(new FlowLayout(FlowLayout.LEFT,4,2),
           backgroundColor);
row2.add(d7);
    row2.add(d8);
    row2.add(d9);
    row2.add(minus);
    row3=makePanel(new FlowLayout(FlowLayout.LEFT,4,2),
       backgroundColor);
    row3.add(d4);
    row3.add(d5);
    row3.add(d6);
    row3.add(plus);
    add(row1);
    add(row2);
    add(row3);
    p12=makePanel(new BorderLayout(2,2),backgroundColor);
    p12.add("West",d1);
    p12.add("East",d2);
    p120=makePanel(new BorderLayout(2,2),backgroundColor);
    p120.add("North",p12);
    p120.add("South",d0);
    p3p=makePanel(new BorderLayout(2,2),backgroundColor);
    p3p.add("North",d3);
    p3p.add("South",dp);
    p3peq=makePanel(new BorderLayout(2,2),backgroundColor);
    p3peq.add("West",p3p);
    p3peq.add("East",eq2);
    add(p120);
    add(p3peq);
    setBackground(backgroundColor);
    c.addActionListener(this);
    eq.addActionListener(this);
    eq2.addActionListener(this);
    div.addActionListener(this);
    times.addActionListener(this);
    minus.addActionListener(this);
    plus.addActionListener(this);
    d0.addActionListener(this);
    d1.addActionListener(this);
    d2.addActionListener(this);
    d3.addActionListener(this);
    d4.addActionListener(this);
    d5.addActionListener(this);
    d6.addActionListener(this);
    d7.addActionListener(this);
    d8.addActionListener(this);
    d9.addActionListener(this);
    clearCalc();
   
}
public void start() {
// Put your code here
}
private double getDisplay() {
return Double.parseDouble(result.getText());
}
public void actionPerformed(ActionEvent e){
String s=(String)e.getActionCommand();
if(digits.indexOf(s)!=-1)
 handleDigit(s);
else if(s.equals("."))
 handleDecimal();
else if(operators.indexOf(s)!=-1)
 handleOperator(s);
else if(s.equals("C"))
 handleClear();
}
private void handleDigit(String s){
if(newOpd){
result.setText(s);
newOpd=false;
}
else 
result.setText(result.getText()+s);
sawAnOpd=true;
} private void handleDecimal() {
if(!sawDecimal){
if(newOpd){
result.setText("0.");
newOpd=false;
}
else 
result.setText(result.getText()+".");
sawDecimal=true;
}
}
    private void handleOperator(String s){
     if(!sawAnOpd)
     return;
     if(lastOptr.equals("+"))
         opd1+=getDisplay();
         else if(lastOptr.equals("-"))
          opd1-=getDisplay();
         else if(lastOptr.equals("*")) 
          opd1*=getDisplay();
         else if(lastOptr.equals("/"))
                opd1/=getDisplay();
         else 
          opd1=getDisplay();
         result.setText(opd1+"");
         lastOptr=s;
         sawDecimal=false;
         newOpd=true;
      }    private void handleClear(){
clearCalc();
}
private void clearCalc() {
sawDecimal=false;
newOpd=true;
sawAnOpd=false;
lastOptr="";
result.setText(initialString);
}
    public void paint(Graphics g){
     resize(row1.getWidth(),8*d1.getHeight());
     validate();
    }

public void stop() {
// Put your code here
}}

解决方案 »

  1.   

    package tt;import java.applet.Applet;
    import java.awt.BorderLayout;
    import java.awt.Button;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Label;
    import java.awt.LayoutManager;
    import java.awt.Panel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;public class CalculatorApplet extends Applet implements ActionListener {
        private String lastOptr;    private String Filler = "     ";    private boolean sawDecimal, newOpd, sawAnOpd;    private int appletWidth, appletHeight;    private double opd1;    private Button c, eq, eq2, div, d1, d2, d3, d4, d5, d6, d7, d8, d9, d0, plus, minus, times, dp;    private Color backgroundColor;    private Label result;    private Panel row1, row2, row3, p12, p120, p3p, p3peq;    static final String initialString = "0.00000";    static final String digits = "0123456789";    static final String operators = "+-*/=";    private Button makeButton(String label, Color color) {
            Button b = new Button(label);
            b.setBackground(color);
            b.setFont(new Font("Courtie", Font.BOLD, 10));
            return b;
        }    private void makeButtons() {
            Color lightRed = new Color(255, 100, 100);
            Color lightBlue = new Color(100, 255, 255);
            Color yellow = new Color(255, 255, 0);
            c = makeButton("C", lightRed);
            eq = makeButton("=", lightBlue);
            div = makeButton("/", lightRed);
            times = makeButton("*", lightBlue);
            d7 = makeButton("7", yellow);
            d8 = makeButton("8", yellow);
            d9 = makeButton("9", yellow);
            minus = makeButton("-", lightBlue);
            d4 = makeButton("4", yellow);
            d5 = makeButton("5", yellow);
            d6 = makeButton("6", yellow);
            plus = makeButton("+", lightBlue);
            d1 = makeButton("1", yellow);
            d2 = makeButton("2", yellow);
            d3 = makeButton("3", yellow);
            dp = makeButton(".", yellow);
            d0 = makeButton("0", yellow);
            eq2 = makeButton("=", lightBlue);
        }    private Panel makePanel(LayoutManager lm, Color c) {
            Panel p = new Panel();
            p.setLayout(lm);
            p.setBackground(c);
            return p;
        }    public void init() {
            // Put your code here
            backgroundColor = new Color(200, 255, 255);
            this.setLayout(new FlowLayout(FlowLayout.CENTER, 4, 1));
            result = new Label("0.0000", Label.RIGHT);
            result.setBackground(new Color(255, 255, 255));
            add(result);
            makeButtons();
            row1 = makePanel(new FlowLayout(FlowLayout.LEFT, 4, 2), backgroundColor);
            row1.add(c);
            row1.add(eq);
            row1.add(div);
            row1.add(times);
            row2 = makePanel(new FlowLayout(FlowLayout.LEFT, 4, 2), backgroundColor);
            row2.add(d7);
            row2.add(d8);
            row2.add(d9);
            row2.add(minus);
            row3 = makePanel(new FlowLayout(FlowLayout.LEFT, 4, 2), backgroundColor);
            row3.add(d4);
            row3.add(d5);
            row3.add(d6);
            row3.add(plus);
            add(row1);
            add(row2);
            add(row3);
            p12 = makePanel(new BorderLayout(2, 2), backgroundColor);
            p12.add("West", d1);
            p12.add("East", d2);
            p120 = makePanel(new BorderLayout(2, 2), backgroundColor);
            p120.add("North", p12);
            p120.add("South", d0);
            p3p = makePanel(new BorderLayout(2, 2), backgroundColor);
            p3p.add("North", d3);
            p3p.add("South", dp);
            p3peq = makePanel(new BorderLayout(2, 2), backgroundColor);
            p3peq.add("West", p3p);
            p3peq.add("East", eq2);
            add(p120);
            add(p3peq);
            setBackground(backgroundColor);
            c.addActionListener(this);
            eq.addActionListener(this);
            eq2.addActionListener(this);
            div.addActionListener(this);
            times.addActionListener(this);
            minus.addActionListener(this);
            plus.addActionListener(this);
            d0.addActionListener(this);
            d1.addActionListener(this);
            d2.addActionListener(this);
            d3.addActionListener(this);
            d4.addActionListener(this);
            d5.addActionListener(this);
            d6.addActionListener(this);
            d7.addActionListener(this);
            d8.addActionListener(this);
            d9.addActionListener(this);
            clearCalc();
            resize(row1.getWidth(), 8 * d1.getHeight());
        }    public void start() {
            // Put your code here
        }    private double getDisplay() {
            return Double.parseDouble(result.getText());
        }    public void actionPerformed(ActionEvent e) {
            String s = (String) e.getActionCommand();
            if (digits.indexOf(s) != -1) handleDigit(s);
            else if (s.equals(".")) handleDecimal();
            else if (operators.indexOf(s) != -1) handleOperator(s);
            else if (s.equals("C")) handleClear();
        }    private void handleDigit(String s) {
            if (newOpd) {
                result.setText(s);
                newOpd = false;
            } else
                result.setText(result.getText() + s);
            sawAnOpd = true;
        }    private void handleDecimal() {
            if (!sawDecimal) {
                if (newOpd) {
                    result.setText("0.");
                    newOpd = false;
                } else
                    result.setText(result.getText() + ".");
                sawDecimal = true;
            }
        }    private void handleOperator(String s) {
            if (!sawAnOpd) return;
            if (lastOptr.equals("+")) opd1 += getDisplay();
            else if (lastOptr.equals("-")) opd1 -= getDisplay();
            else if (lastOptr.equals("*")) opd1 *= getDisplay();
            else if (lastOptr.equals("/")) opd1 /= getDisplay();
            else
                opd1 = getDisplay();
            result.setText(opd1 + "");
            lastOptr = s;
            sawDecimal = false;
            newOpd = true;
        }    private void handleClear() {
            clearCalc();
        }    private void clearCalc() {
            sawDecimal = false;
            newOpd = true;
            sawAnOpd = false;
            lastOptr = "";
            result.setText(initialString);
        }//    public void paint(Graphics g) {
    //        
    //        validate();
    //    }    public void stop() {
            // Put your code here
        }}布局设置的我觉得不是很合理
      

  2.   

    就是你的resize调用有问题,注视掉就可以了
    不要把validate()和resize()同时放在paint()中
      

  3.   

    resize()就是起到重新布局的作用,去掉resize就达不到效果了
      

  4.   

    是这样的 
    resize()不能放到pain()方法中
    因为resize会调用repain方法
    repain方法最后又会去调用pain方法
    所以会产生无限循环调用
    因此产生不断刷新的现象
      

  5.   

    你在其他地方(paint方法外)调用
    resize就可以了