JFrame repaint() 问题 - Java
我希望能够在 JFrame 上使用 Java 的 Paint() 进行绘制。当我单击 JFrame(现在任意位置)时,我希望使用单击的坐标重新绘制 JFrame - 类似于此 Java 小程序 http://www.realapplets.com/tutorial/MouseClickExample.html
当前工作:
- 所有内容均已初始绘制,并且 JFrame 正确显示
不工作:
- JFrame 不显示即使声明了 repaint() 也可以重新绘制和更新
这是我的代码 - 请尽可能严格地使用它 - 我想提高我的 Java 编程技术,所以(如果你有时间)指出我可以改进的每个方面。
任何帮助将非常感激。
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();
}
}
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
Currently Working:
- Everything is drawn initially and the JFrame is properly displayed
Not Working:
- JFrame does not repaint and update even when repaint() is declared
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.
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在创建 2 个 AreaForText 实例,这不是您想要做的。一份添加到 JFrame,一份添加到侦听器。因此,实际获取鼠标事件并调用重绘的对象与正在显示的对象不同。
你的一些代码组织并不是最好的。您有一个 JPanel 子类,它构建自己的 JFrame 并将其自身放入面板中。如果您确实需要 JFrame,则应该传入它。我在下面做了一些更改。
编辑。我修复了一些鼠标监听器的东西,你得到了错误的 X/Y 坐标,而且,你应该直接将监听器添加到面板,而不是 JFrame,这样你就不必翻译坐标。
编辑 我将paint方法更改为paintComponent,这是此处重写的首选方法。有关详细信息,请参阅 Swing Paint 教程。
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.
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.
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.
EDIT I changed the paint method to paintComponent, which is the preferred method to override here. Have a look at the Swing Paint Tutorial for more information.
您没有调用 JFrames
repaint()
您正在调用JPanel
repaint 方法(您所在的类)尝试:
Your not calling the JFrames
repaint()
you are calling theJPanel
repaint method ( the class you are in)Try: