Java 重新画一个圆

Java 重新画一个圆,java,swing,paint,paintcomponent,Java,Swing,Paint,Paintcomponent,每当按下按钮时,我想重新画一个圆圈 目前,每当我按下一个按钮,它就会将我按下的按钮打印到控制台上。例如,如果我按下“绘制红色”按钮,我希望它用红色填充圆圈,其他颜色也一样。我正试着把我的头脑集中在整个油漆/油漆组件的差异上 这就是我目前所拥有的 public class testCircle extends JPanel { public void paint(Graphics g) { setSize(500,500); int R = (int) (Math.random

每当按下按钮时,我想重新画一个圆圈

目前,每当我按下一个按钮,它就会将我按下的按钮打印到控制台上。例如,如果我按下“绘制红色”按钮,我希望它用红色填充圆圈,其他颜色也一样。我正试着把我的头脑集中在整个油漆/油漆组件的差异上

这就是我目前所拥有的

public class testCircle extends JPanel {

public void paint(Graphics g)
{
    setSize(500,500);

    int R = (int) (Math.random( )*256);
    int G = (int)(Math.random( )*256);
    int B= (int)(Math.random( )*256);
    Color randomColor = new Color(R, G, B);
    g.setColor(randomColor);
    g.drawOval(75, 100, 200,200);
    g.fillOval(75, 100, 200, 200);
}

public static void main(String[] args)
{

     JFrame frame = new JFrame();
     frame.setSize(400, 400);
     testCircle circlePanel = new testCircle();
     frame.add(circlePanel);



     JButton redButton = new JButton("Paint Red");
     redButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent event)
         {
             System.out.println("Red Button Pressed!");  
         }
     });
     JButton blueButton = new JButton("Paint Blue");
     blueButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent event)
         {
             System.out.println("Blue Button Pressed!");
         }
     });

     JButton greenButton = new JButton("Paint Green");
     greenButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent event)
         {
             System.out.println("Green Button Pressed!");
         }
     });

     redButton.setPreferredSize(new Dimension(100,100));
     blueButton.setPreferredSize(new Dimension(100,100));
     greenButton.setPreferredSize(new Dimension(100,100));

     frame.setLayout(new FlowLayout());
     frame.add(redButton);
     frame.add(blueButton);
     frame.add(greenButton);
     frame.setVisible(true);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
}代码中的任何地方都不会触发“重新绘制”。这应该是可行的:

 redButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.out.println("Red Button Pressed!");
                frame.invalidate();
                frame.validate();
            }
        });
您不会在代码中的任何地方触发“重新绘制”。这应该是可行的:

 redButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.out.println("Red Button Pressed!");
                frame.invalidate();
                frame.validate();
            }
        });

考虑对代码的以下更改:

  • 如前所述,Swing程序应该重写
    paintComponent()
    ,而不是重写
    paint()

  • 为面板指定
    currentColor
    的属性

    private Color currentColor;
    
  • 让每个按钮的
    ActionListener
    设置
    currentColor
    并调用
    repaint()

  • 用于封装程序的功能

最后给出了一个完整的例子


考虑对代码进行以下更改:

  • 如前所述,Swing程序应该重写
    paintComponent()
    ,而不是重写
    paint()

  • 为面板指定
    currentColor
    的属性

    private Color currentColor;
    
  • 让每个按钮的
    ActionListener
    设置
    currentColor
    并调用
    repaint()

  • 用于封装程序的功能

最后给出了一个完整的例子


您不必验证整个
容器
;应该可以按照概述,仅对面板进行
重新绘制()
。您不必
验证()
整个
容器
;应该可以只对面板进行重新喷漆()
,如所述。