日期:2014-05-20  浏览次数:20566 次

绘图程序有点疑问 求指导
Java code
/**
 * 
 */
package Paint.App14_1;

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.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class App14_1 extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    static JButton jButton1 = new JButton("画圆");
    static JButton jButton2 = new JButton("画椭圆");
    static Mypanel mypanel = new Mypanel();
    static App14_1 jframe = new App14_1();
    static int circle = 0;

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        jframe.setVisible(true);
        jframe.setTitle("简单的绘图应用程序");
        jframe.setBounds(500, 250, 300, 250);
        jframe.setLayout(null);
        jframe.add(mypanel);
        mypanel.setBounds(0, 0, 300, 250);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    static class Mypanel extends JPanel implements ActionListener {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        /**
         * 
         */
        public Mypanel() {
            // TODO Auto-generated constructor stub
            add(jButton1);
            add(jButton2);
            jButton1.addActionListener(this);
            jButton2.addActionListener(this);
            // jButton1.setBounds(50, 180, 80, 25);
            // jButton2.setBounds(150, 180, 80, 25);
        }

        /*
         * (non-Javadoc)
         * 
         * @see
         * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent
         * )
         */
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            JButton button = (JButton) e.getSource();
            if (button == jButton1)
                circle = 1;
            else
                circle = 2;
            Graphics graphics = this.getGraphics();//把这句注释掉怎么也可以正确运行?
            repaint();
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            jButton1.setBounds(50, 180, 80, 25);
            jButton2.setBounds(150, 180, 80, 25);
            g.setFont(new Font("楷体", Font.ITALIC, 20));
            g.setColor(Color.red);
            g.drawString("画圆或椭圆", 120, 30);
            if (circle == 1)
                g.drawOval(100, 70, 70, 70);
            else if (circle == 2)
                g.drawOval(80, 40, 70, 120);
        }

    }
}


问题就在注释的那一行

------解决方案--------------------
Graphics graphics = this.getGraphics();//把这句注释掉怎么也可以正确运行?

这一行没有任何地方在用,是无意义的一行代码。