简体   繁体   English

ButtonClick上的Java Graphics repaint()

[英]Java Graphics repaint() on ButtonClick

I have this code and want to repaint my graphic when the Button gets pressed: 我有这个代码,并希望在按下Button时重新绘制我的图形:

public class JDraw extends JFrame {

/**
 * Draws a figur in a JFrame.
 * The color of it is random and can get changed by a button.
 */

public static JButton okButton = new JButton("change color");

public JDraw(String newTitel) {
    super.setTitle(newTitel);
}

//Main method
public static void main(String str[]) {
    JDraw window = new JDraw("Graphic");
    window.setSize(300, 450);
    window.setVisible(true);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.add(okButton, BorderLayout.SOUTH);

    okButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            //JDraw.repaint(); <-- problem
        }
    });
}



@Override
public void paint(final Graphics g) {

    super.paint(g);

    Random rand = new Random();
    int r1 = rand.nextInt(255);
    int b1 = rand.nextInt(255);
    int g1 = rand.nextInt(255);
    g.setColor(new Color(r1, g1, b1));

    //head
    g.drawOval(100, 50, 100, 100);
    g.fillOval(100, 50, 100, 100); // filled
    //more drawing stuff.....

}
}

However I have no idea how to do it, because I cant do the repaint in my ActionPerfomed. 但是我不知道该怎么做,因为我不能在ActionPerfomed中进行重绘。 Error: non-static method repaint() cannot be referenced from a static context 错误:无法从静态上下文引用非静态方法repaint()

I hope somebody can help. 我希望有人可以提供帮助。 :) :)

You need to make the following call in your actionPerformed : 您需要在actionPerformed进行以下调用:

window.repaint();

To be able to reference window from within you actionPerformed , you need to make your window variable final : 为了能够从actionPerformed引用window ,您需要使窗口变量final

final JDraw window = ...;

However, if I can suggest a few improvements: 但是,如果我可以提出一些改进建议:

  1. Don't extend JFrame 不要扩展JFrame
  2. Don't override paint(Graphics) of JFrame 不要覆盖JFrame paint(Graphics)
  3. Instead create a class that extends JComponent or JPanel and set it as the content pane of the JFrame 而是创建一个扩展JComponentJPanel的类,并将其设置为JFrame的内容窗格
  4. Instead of overrding paint(Graphics) , rather override paintComponent(Graphics) 而不是覆盖paint(Graphics) ,而是覆盖paintComponent(Graphics)
  5. Your okButton should not be static . 你的okButton不应该是static Instead move all your code in a non-static method like initUI() and have a code like new JDraw().initUI(); 而是将所有代码移动到非静态方法(如initUI() ,并使用类似new JDraw().initUI();的代码new JDraw().initUI();
  6. Wrap the code starting your UI in a SwingUtilities.invokeLater(Runnable) so that your UI is properly initiated from the Event Dispatching Thread. SwingUtilities.invokeLater(Runnable)中包含启动UI的代码,以便从Event Dispatching Thread正确启动UI。

You can't refer to the class JDraw. 你不能引用JDraw类。 You should use an object instead. 您应该使用对象。 The object in your case is window . 您的案例中的对象是window So, use: 所以,使用:

window.repaint();

It is like saying: Human, walk to the door. 就像说: 人类,走到门口。 Human is the class. 人是阶级。 You can't tell Human to do something, you need an instance of Human, like Obama or Santa Claus. 你不能告诉人类做某事,你需要人类的实例,比如奥巴马或圣诞老人。 In your case: you can't tell JDraw to repaint, but the object of type JDraw, ie: window . 在你的情况下:你不能告诉JDraw重绘,而是JDraw类型的对象,即: window

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM