栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从另一个类JFrame调用重画

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从另一个类JFrame调用重画

看一下这个页面,看看第一个答案。这是一个与您的问题类似的,甚至不是确切的问题。

Jframe的

paint()
方法已被弃用。编译器或您的IDE应该有点抱怨,特别是如果您将
@Override
标记直接放置在方法上方(使用它来测试是否可以重写此方法,也就是您想做的事情)。

这意味着不鼓励使用它,并且某些功能可能已删除。使用时

javax.swing
,您将需要全面了解
JPanels
和了解系统
JComponents
。要在屏幕上绘制内容,您需要添加一个
JPanel
随该
add(Componentc)
方法扩展的自定义类。然后,重写
paintComponent(Graphicsg)
该类中的方法。确保该方法的第一行是
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    }}

因此,当在上调用

repaint()
or
revalidate()
方法时
MyWindow
,面板将收到一个
paintComponent
调用。

如果您需要任何其他帮助,请在评论中让我知道。

编辑:

由于您需要使用MouseMotionListener,而且我仍然不太了解“我需要从另一个类调用重绘”的上下文和麻烦。我将尽力而为。

首先,在Oracle页面上查看本教程。另外,在GUI上查看其他。您将学到很多有关组织和显示的知识,这将使您意识到他们的系统如何与您的系统一起工作。

现在,对于您的问题:

i have to use MouseMotionListener.

不完全是…这是一种很好的设置方法,但是您可以运行一个Thread(不断不断地运行方法的东西)来检查Mouse坐标。当您进入游戏和其他各种应用程序时,您将要开始执行此操作。

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 dothat.
如果您在我的编辑顶部阅读了该教程,那么您会看到它们轻松实现了MouseMotionListener。

下一步:

I prefer to do it withJframe.
如果希望在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 away 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.

抱歉,您需要面板。可以使用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
。到处都有关于这些东西的教程。在将来寻求具体帮助之前,请先阅读它们。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/570977.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号