我希望能够在JFrame上使用
Java的paint()进行绘制.当我单击JFrame(现在任何地方)时,我希望用点击的坐标重新绘制JFrame – 类似于这个Java小程序
http://www.realapplets.com/tutorial/MouseClickExample.html
正在工作:
不工作:
>即使声明了repaint(),JFrame也不会重新绘制和更新
这是我的代码 – 请尽可能严格地使用它 – 我想改进我的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); g.drawString("("+xpos+","+ypos+")",xpos,ypos); } } class EnlargeText { public static void main(String args[]) { AreaForText test = new AreaForText(); test.setJFrame(); } }
解决方法
您正在创建2个AreaForText实例,这不是您想要做的.一个添加到JFrame,一个添加到侦听器.因此,实际获取鼠标事件并且正在调用重绘的那个与正在显示的对象不同.
你的一些代码组织并不是最好的.你有一个JPanel子类,它构建自己的JFrame并将自己放入面板中.如果你真的需要它,你应该传入JFrame.我在下面做了一些改动.
编辑.我修复了一些鼠标监听器的东西,你得到了错误的X / Y坐标,而且,你应该直接将监听器添加到面板,而不是JFrame,这样你就不必翻译坐标.
编辑我将paint方法更改为paintComponent,这是在此处覆盖的首选方法.有关更多信息,请查看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",80); g.fillRect(20,20); g.drawString("(" + xpos + "," + ypos + ")",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); } }); } }