JFrame repaint() 问题 - Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6415060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:48:01  来源:igfitidea点击:

JFrame repaint() issues - Java

javaawtjframevalidationrepaint

提问by Mr Tickle

I want to be able to draw using Java's paint() on a JFrame. When I click the JFrame (anywhere for now) I want the JFrame to be repainted with the co-ordinates of the click - similar to this Java applet http://www.realapplets.com/tutorial/MouseClickExample.html

我希望能够在 JFrame 上使用 Java 的 paint() 进行绘制。当我点击 JFrame(现在在任何地方)时,我希望 JFrame 用点击的坐标重新绘制 - 类似于这个 Java 小程序http://www.realapplets.com/tutorial/MouseClickExample.html

Currently Working:

正在工作:

  • Everything is drawn initially and the JFrame is properly displayed
  • 最初绘制所有内容并正确显示 JFrame

Not Working:

不工作:

  • JFrame does not repaint and update even when repaint() is declared
  • 即使声明了 repaint(),JFrame 也不会重新绘制和更新

Here is my code - Please be as stringent as possible with it - I would like to improve my Java programming technique so (if you have time that is) point out every aspect I could improve on.

这是我的代码 - 请尽可能严格 - 我想改进我的 Java 编程技术,以便(如果你有时间)指出我可以改进的每个方面。

Any help would be very much appreciated.

任何帮助将不胜感激。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class AreaForText extends JPanel implements MouseListener {

int xpos; 
int ypos;

JFrame myJFrame = new JFrame();

public void setJFrame() {

    myJFrame.setSize(300, 150);
    myJFrame.setTitle("Bigger Text!");
    myJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myJFrame.setVisible(true);
    myJFrame.getContentPane().add(new AreaForText());
    myJFrame.addMouseListener(new AreaForText());

}

public void mouseClicked(MouseEvent me) {
    //Save the coordinates of the click lke this. 
    xpos = MouseInfo.getPointerInfo().getLocation().x; 
    ypos = MouseInfo.getPointerInfo().getLocation().y;
    System.out.print("Click" + "  x: " + xpos + "  y: " + ypos);
    myJFrame.invalidate();
    repaint();
    revalidate();
}


public void mouseEntered(MouseEvent e){
}

public void mouseReleased(MouseEvent e) { 
}

public void mousePressed(MouseEvent e) {
}

public void mouseExited(MouseEvent e) { 
}

public void paint(Graphics g) {

    System.out.print("hello");
    g.drawString("Hello World", 30, 80);
    g.fillRect(20,20,20,20);        
    g.drawString("("+xpos+","+ypos+")",xpos,ypos);

    }
}

class EnlargeText {

    public static void main(String args[]) {

       AreaForText test = new AreaForText();

       test.setJFrame();

    }

 } 

回答by wolfcastle

You are creating 2 instances of AreaForText which is not what you want to do. One is added to the JFrame, and one is added to the listener. So the one that actually gets the mouse events and is calling repaint is not the same object that is being displayed.

您正在创建 AreaForText 的 2 个实例,这不是您想要做的。一个添加到 JFrame,一个添加到侦听器。因此,实际获取鼠标事件并调用重绘的对象与正在显示的对象不同。

Some of your code organization is not the best. You have a JPanel subclass that builds its own JFrame and puts itself into the panel. You should just pass in the JFrame if you really need it. I've made a few changes below.

你的一些代码组织不是最好的。您有一个 JPanel 子类,它构建自己的 JFrame 并将自己放入面板中。如果您真的需要它,您应该只传入 JFrame。我在下面做了一些更改。

EDIT. I fixed up some of the mouse listener stuff, you were getting the wrong X/Y co-ordinates, and also, you should just add the listener to the panel directly, not the JFrame, that way you don't have to translate the co-ordinates.

编辑。我修复了一些鼠标监听器的东西,你得到了错误的 X/Y 坐标,而且,你应该直接将监听器添加到面板,而不是 JFrame,这样你就不必翻译坐标。

EDITI changed the paint method to paintComponent, which is the preferred method to override here. Have a look at the Swing Paint Tutorialfor more information.

编辑我将paint方法更改为paintComponent,这是此处覆盖的首选方法。查看Swing Paint 教程以获取更多信息。

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

class AreaForText extends JPanel implements MouseListener {

    private int xpos;
    private int ypos;


    public AreaForText() {
        super();
        this.addMouseListener(this);
    }

    public void mouseClicked(MouseEvent me) {
        // Save the coordinates of the click lke this.
        xpos = me.getX();
        ypos = me.getY();
        System.out.print("Click" + "  x: " + xpos + "  y: " + ypos);
        repaint();
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        System.out.print("hello");
        g.drawString("Hello World", 30, 80);
        g.fillRect(20, 20, 20, 20);
        g.drawString("(" + xpos + "," + ypos + ")", xpos, ypos);

    }
}

class EnlargeText {

    public static void main(String args[]) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame myJFrame = new JFrame("Bigger Text!");
                myJFrame.setSize(300, 150);
                myJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                myJFrame.getContentPane().add(new AreaForText());
                myJFrame.setVisible(true);
            }
        });
    }

}

回答by RMT

Your not calling the JFrames repaint()you are calling the JPanelrepaint method ( the class you are in)

你没有调用 JFramesrepaint()你正在调用JPanelrepaint 方法(你所在的类)

Try:

尝试:

myJFrame.repaint();