import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
 *
 * @author Administrator
 */
public class hua extends JFrame implements ActionListener{
    static hua frame=new hua();
    static Button button1=new Button("circle");
    static Button button2=new Button("ellipse");
   static JPanel pane=new JPanel();
    int circle=0;
    public static void main(String[] args) {
        frame.setTitle("简单的画图程序");
        frame.setSize(300, 250);
        frame.setLayout(new BorderLayout());
        frame.add(pane, BorderLayout.SOUTH);
        //frame.setBackground(Color.WHITE);
        pane.add(button1);
        pane.add(button2);
        button1.addActionListener(frame);
        button2.addActionListener(frame);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent e){
        Button button=(Button)e.getSource();
        if(button==button1){
            circle=1;
        }
        else
            circle=2;
        Graphics g=getGraphics();
        paint(g);
    }
   
    @Override
    public void paint(Graphics g){
        g.setFont(new Font("黑体",Font.ITALIC,20));
        g.setColor(Color.red);
        g.drawString("画圆或画椭圆", 120, 50);
        if(circle==1){
            /*g.setColor(Color.WHITE);
            g.drawOval(80, 60, 70, 120);
            g.setColor(Color.red);*/
            g.drawOval(100, 90, 70, 70);
            repaint();//此处repaint()怎么不起作用呢
        }
        else if(circle==2) {
            /*g.setColor(Color.WHITE);
            g.drawOval(100, 90, 70, 70);
            g.setColor(Color.red);*/
            g.drawOval(80, 60, 70, 120);
            repaint();//此处repaint()怎么不起作用呢
        }    }
}
想用repaint()实现,点画圆就画圆,然后点椭圆,然后画椭圆,圆消失,但是好像实现不了,不知为什么,还请各位帮忙解答一下   1.请告诉我一下为什么不能实现。2.怎么实现?  谢谢

解决方案 »

  1.   


    import java.awt.BorderLayout;
    import java.awt.Button;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    /**
     *
     * @author Administrator
     */
    public class hua extends JFrame implements ActionListener{
        static hua frame=new hua();
        static Button button1=new Button("circle");
        static Button button2=new Button("ellipse");
       static JPanel pane=new JPanel();
        int circle=0;
        public static void main(String[] args) {
            frame.setTitle("简单的画图程序");
            frame.setSize(300, 250);
            frame.setLayout(new BorderLayout());
            frame.add(pane, BorderLayout.SOUTH);
            //frame.setBackground(Color.WHITE);
            pane.add(button1);
            pane.add(button2);
            button1.addActionListener(frame);
            button2.addActionListener(frame);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        public void actionPerformed(ActionEvent e){
            Button button=(Button)e.getSource();
            if(button==button1){
                circle=1;
            }
            else
                circle=2;
            repaint();
        }
       
        @Override
        public void paint(Graphics g){
            super.paint(g);
            g.setFont(new Font("黑体",Font.ITALIC,20));
            g.setColor(Color.red);
            g.drawString("画圆或画椭圆", 120, 50);
            if(circle==1){
                /*g.setColor(Color.WHITE);
                g.drawOval(80, 60, 70, 120);
                g.setColor(Color.red);*/
                g.drawOval(100, 90, 70, 70);
            }
            else if(circle==2) {
                /*g.setColor(Color.WHITE);
                g.drawOval(100, 90, 70, 70);
                g.setColor(Color.red);*/
                g.drawOval(80, 60, 70, 120);
            }    }
    }
      

  2.   

    public void actionPerformed(ActionEvent e){
            Button button=(Button)e.getSource();
            if(button==button1){
                circle=1;
            }
            else
                circle=2;
            /*Graphics g=getGraphics();
            paint(g);*/

            repaint();
        }public void paint(Graphics g){
            g.setFont(new Font("黑体",Font.ITALIC,20));
            g.setColor(Color.red);
            g.drawString("画圆或画椭圆", 120, 50);
            if(circle==1){
                /*g.setColor(Color.WHITE);
                g.drawOval(80, 60, 70, 120);
                g.setColor(Color.red);*/
                g.drawOval(100, 90, 70, 70);
                //repaint();//repaint call paint autoly
            }
            else if(circle==2) {
                /*g.setColor(Color.WHITE);
                g.drawOval(100, 90, 70, 70);
                g.setColor(Color.red);*/
                g.drawOval(80, 60, 70, 120);
                //repaint();//repaint call paint autoly
            }    }
      

  3.   

    repaint是外部需要重新画这个对象时用的,
    repaint引起paint
      

  4.   

    版主点击了按钮,可是系统只有在第一次调用一次paint方法,后面你在点击就必须使用rapaint方法来重新让系统调用paint方法,达到你的目的。所有你的不行,你楼下的可以。