简体   繁体   English

JFrame repaint()问题 - Java

[英]JFrame repaint() issues - Java

I want to be able to draw using Java's paint() on a JFrame. 我希望能够在JFrame上使用Java的paint()进行绘制。 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(现在任何地方)时,我希望用点击的坐标重新绘制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();

    }

 } 

You are creating 2 instances of AreaForText which is not what you want to do. 您正在创建2个AreaForText实例,这不是您想要做的。 One is added to the JFrame, and one is added to the listener. 一个添加到JFrame,一个添加到侦听器。 So the one that actually gets the mouse events and is calling repaint is not the same object that is being displayed. 因此实际获取鼠标事件并且正在调用重绘的那个与正在显示的对象不同。

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. 你有一个JPanel子类,它构建自己的JFrame并将自己放入面板中。 You should just pass in the JFrame if you really need it. 如果你真的需要它,你应该传入JFrame。 I've made a few changes below. 我在下面做了一些改动。

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,这样你就不必翻译坐标。

EDIT I changed the paint method to paintComponent, which is the preferred method to override here. 编辑我将paint方法更改为paintComponent,这是在此处覆盖的首选方法。 Have a look at the Swing Paint Tutorial for more information. 有关更多信息,请查看Swing Paint Tutorial

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);
            }
        });
    }

}

Your not calling the JFrames repaint() you are calling the JPanel repaint method ( the class you are in) 你没有调用JFrames repaint()你正在调用JPanel重绘方法(你所在的类)

Try: 尝试:

myJFrame.repaint();

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

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