随便写了个import java.awt.event.*;
import java.awt.*;
import java.applet.*;public class DrawTest extends Applet implements TextListener {
Color color[]={Color.blue,Color.black,Color.orange,Color.pink,Color.yellow,Color.white,Color.red,Color.green, Color.magenta,Color.cyan};
int n=1;
int c=0;
TextField tf1,tf2; public void init() {
tf1=new TextField(2);
add(tf1);
tf1.addTextListener(this);
tf2=new TextField(2);
add(tf2);
tf2.addTextListener(this);
     }
public void paint(Graphics g){
     g.setColor(color[c]);
     g.fillRect(30,60,n*30,n*30);
}
public void textValueChanged(TextEvent e){
if(e.getSource()==tf1)
n=Integer.parseInt(tf1.getText());
if(e.getSource()==tf2)
c=Integer.parseInt(tf2.getText())%10;
repaint();
}
}

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import com.bruceeckel.swing.*;
    class Rect extends JPanel {
        private Color color = Color.red;    private int size = 50;    public void setColor(Color color) {
            this.color = color;
            repaint();
        }    public void setSize(int size) {
            this.size = size;
            repaint(); 
        }
        
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(color);
            g.drawRect(5, 5, size, size);
        }
    }
    public class DrawRect extends JApplet {
        JPanel jp = new Rect();    JButton changeColor = new JButton("ChangeColor"),
            changeSize = new JButton("ChangeSize");    public void run() {
            Container cp = getContentPane();
            cp.setLayout(new FlowLayout());
            cp.add(jp);
            cp.add(changeColor);
            cp.add(changeSize);
            changeColor.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jp.setColor(Color.green);
                }
            });
            changeSize.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jp.setSize(100);
                }
            });
        }    public static void main(String[] args) {
            Console.run(new DrawRect(), 300, 300);
        }
    }
      

  2.   

    package com.bruceeckel.swing;
    import javax.swing.*;
    import java.awt.event.*;
    public class Console {
        // Create a title string from the class name :
        public static String title(Object o) {
    String t = o.getClass().toString(); // Remove the word "class" :
    if (t.indexOf("class") != -1) {
        t = t.substring(6);
    }
    return t;
        }    public static void setupClosing(JFrame frame) {
    // The JDK 1.2 Solution as an 
    // anonymous inner class :        frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
    System.exit(0);
        }
    }); // The improved solution in JDK 1.3 :
    // frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        
        public static void run(JFrame frame, int width, int height) {
    setupClosing(frame);
    frame.setSize(width, height);
    frame.setVisible(true);
        }    public static void run(JApplet applet, int width, int height) {
    JFrame frame = new JFrame(title(applet));
    setupClosing(frame);
    frame.getContentPane().add(applet);
    frame.setSize(width, height);
    applet.init();
    applet.start();
    frame.setVisible(true);
        }    public static void run(JPanel panel, int width, int height) {
    JFrame frame = new JFrame(title(panel));
    setupClosing(frame);
    frame.getContentPane().add(panel);
    frame.setSize(width, height);
    frame.setVisible(true);
        }
    }