0

这是我的程序:

子类:

import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.util.Scanner;

public class abc extends JPanel {

    public void paintComponent(Graphics g) {
        Scanner input = new Scanner(System.in);
        super.paintComponent(g);

        this.setBackground(Color.WHITE);
        int a, b;
        System.out.print("input a: ");
        a = input.nextInt();
        a = b;
        g.setColor(Color.BLUE);
        g.fillOval(150, 40, a, b);
    }
}

主类:

import java.awt.Color;
import javax.swing.JFrame;

public class abcd {

    public static void main(String args[]) {
        JFrame frame = new JFrame("Draw");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        abc panel = new abc();
        panel.setBackground(Color.WHITE);
        frame.add(panel);
        frame.setSize(400, 200);
        frame.setVisible(true);
    }
}

输出总是重复;我必须输入3-4次。我用子类和主类做这个。请帮助解决这个问题,并解释为什么它会重复?

4

1 回答 1

3

我注意到您的代码中有几件事可能会导致问题:

public class abc extends JPanel{
public void paintComponent(Graphics g){
Scanner input=new Scanner(System.in);
super.paintComponent(g);

this.setBackground(Color.WHITE);
int a,b;
System.out.print("input a: ");
a=input.nextInt();

a=b;shouldn't this be b=a , maybe you did it by mistake?

g.setColor(Color.BLUE);
g.fillOval(150,40,a,b);

}

其次,你为什么要画两次面板,第一次在abcd课堂上,然后在abc课堂上?

于 2013-06-25T08:27:10.927 回答