从另一个类JFrame调用重绘

我试图从另一个class级打电话给他。 但它不起作用。 我必须画一个框架。

public class Tester extends JFrame{ public static dtest d ; public static void main(String[] args) { Tester t = new Tester(); d = new dtest(); test tnew = new test(); } public static class dtest extends JFrame implements MouseMotionListener{ public static int x,y; dtest() { super("title"); setSize(500,500); setVisible(true); addMouseMotionListener(this); } @Override public void mouseDragged(MouseEvent e) { x = e.getX(); y = e.getY(); repaint(); } @Override public void mouseMoved(MouseEvent e) { // TODO Auto-generated method stub } public void paint(Graphics g) { System.out.println("I am called"); } } public static class test { public test() { for(int i = 0 ; i < 5 ; i++) { System.out.println("I am called from run"); d.repaint(); } } } } 

这打印

 I am called from run I am called from run I am called from run I am called from run I am called from run 

所以它不执行paint()部分。 d.repaint()不起作用。 为什么?

看一下这个页面 ,看一下第一个答案。 对你来说这是一个类似的问题。

JFrame的paint()方法已被弃用。 编译器或IDE应该抱怨一下,特别是如果你将@Override标记放在方法的正上方(使用它来测试这个方法是否可以重写…也就是你要做的事情)。

这意味着不鼓励使用它,并且可能已删除某些function。 使用javax.swing ,您将完全了解有关JPanelsJComponents的系统。 要在屏幕上绘制内容,您需要添加一个使用add(Component c)方法扩展JPanel的自定义类。 然后,覆盖该类中的paintComponent(Graphics g)方法。 确保该方法的第一行是super.paintComponent(g); 这样窗口就可以刷新自己。

为了完整性:

 public class MyWindow extends JFrame { MyPanel thePanel; public MyWindow(int x, int y) { setSize(x, y); thePanel = new MyPanel(x, y); this.add(thePanel); } } public class MyPanel extends JPanel { public MyPanel(int x, int y) setSize(x, y); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(ImageManager.getImage("Cute Puppy"), 40, 40, null); // Or whatever } } 

因此,当在MyWindow上调用repaint()revalidate()方法时,Panel将接收paintComponent调用。

如果您需要任何其他帮助,请在评论中告诉我。

编辑:

既然你需要使用MouseMotionListener,我仍然不太了解“我需要从另一个class级调用重绘”的背景和麻烦……我会尽我所能。

首先,在Oracle页面上查看本教程 。 另外,请查看GUI上的其他内容。 您将学到很多关于组织和展示的知识,这将使您了解他们的系统如何与您的系统协同工作。

现在,针对您的问题:

i have to use MouseMotionListener.

不完全……这是一个很好的设置方法,但你可以运行一个Thread(不断运行方法的东西)来检查鼠标坐标。 当你进入游戏和其他杂项应用程序时,你会想要开始这样做。

 new Thread() { public void run() { Point mouse; int mousex; int mousey; while (true) { mouse = MouseInfo.getPointerInfo().getLocation(); mousex = mouse.x - theWindow.getLocationOnScreen().x - 3; // You'll need to get the // x coordinate, subtract the window's x coordinate, and subtract 3 because of // the blue border around a standard pc window. mousey = mouse.y - theWindow.getLocationOnScreen().y - 29; // 29 is top bar height SomeOtherClass.processMove(mousex, mousey); } } }.start(); 

下一篇: I tried that with JPanel but i could not do that. 如果您阅读编辑顶部的教程,您会看到他们轻松实现了MouseMotionListener。

下一篇: I prefer to do it with JFrame. 如果您希望在JFrame中处理鼠标,请执行以下操作:将JFrame作为侦听器,但JPanel是鼠标数据的来源。 如下:

 public class MyWindow extends JFrame implements MouseMotionListener { public MyPanel thePanel; public int x; public int y; public MyWindow() { thePanel = new MyPanel(); thePanel.addMouseMotionListener(this); // Make this JFrame get called when the mouse // moves across the panel. } @Override public void mouseDragged(MouseEvent e) { x = e.getX(); y = e.getY(); thePanel.repaint(); } @Override public void mouseMoved(MouseEvent e) { // TODO Auto-generated method stub } } public class MyPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); // Other painting stuff } } 

下一步: Now i have to update the frame from another class. I could not find a way to update the GUI(the frame) from another class. Now i have to update the frame from another class. I could not find a way to update the GUI(the frame) from another class.

简单。 由于JPanel需要更新,因此将以下方法添加到MyWindow类:

 public void repaintWindow() { thePanel.repaint(); } 

并在需要更新时将其添加到:

 MyWindow theWindow = new MyWindow(); theWindow.repaintWindow(); 

下一步: all the answers here extended JPanel. So i could not find my answer. all the answers here extended JPanel. So i could not find my answer.

我道歉,但你需要一个小组。 可以使用JFrames,但是如果你想开始做原始和低级的事情,你需要通过学习阅读oracle教程和oracle文档来了解这些事情是如何工作的。 现在,以我向您展示的任何方式使用JPanels。

接下来: from another class I have to draw something on JFrame.Is that possible?

确实是的! 每当你想要绘制一些东西:

 MyWindow theWindow = new MyWindow(); Graphics g = theWindow.thePanel.getGraphics(); BufferedImage someRandomImage = SomeRandomClass.getRandomImage(); g.drawImage(someRandomImage, 200, 481, null); theWindow.repaintWindow(); 

我真的希望我能帮助你,但是要用java编程你需要使用他们给你的工具,特别是当涉及像Swing这样的高级东西时。 这个东西到处都有教程。 请在将来要求特定帮助之前阅读它们。