简体   繁体   English

Java中的repaint()方法

[英]repaint() Method in Java

I am playing around with the Java graphics class a little bit and I was just wondering--when is it necessary to call the method repaint() ? 我正在玩Java图形类,我只是想知道 - 什么时候需要调用方法repaint() I tried commenting it out and it didn't seem to affect my output GUI. 我试着将它评论出来,但它似乎并没有影响我的输出GUI。 I've seen it used a lot in Java GUI code that I've read, though. 我已经看到它在我阅读过的Java GUI代码中使用了很多。 Would anyone mind explaining when to use it and when not to use it? 有人会介意何时使用它以及何时不使用它?

It's never really necessary in most swing applications, because they handle it automatically (for common operations such as changing text values on buttons and adding data to a list box). 在大多数swing应用程序中它从来都不是必需的,因为它们会自动处理它(对于常见操作,例如更改按钮上的文本值和将数据添加到列表框)。

Generally, it's only if you've made some sort of change that swing won't automatically pick up - for example, you're not using a layout manager and are resizing components manually (because normally the layout manager repaints its components when necessary). 通常情况下,只有当你做出某种变化时才会自动获取摆动 - 例如,你没有使用布局管理器并手动调整组件大小(因为通常布局管理器会在必要时重新绘制其组件) 。

The repaint() refreshes the view (component), so whenever you make any change on the component, you must call it. repaint()刷新视图(组件),因此无论何时对组件进行任何更改,都必须调用它。 For instance, if you rotate the graphical component, you must make a call to repaint() in order to see the change on the containing component 例如,如果旋转图形组件,则必须调用repaint()以查看包含组件的更改

When you launch your application, you "paint" your GUI. 启动应用程序时,可以“绘制”GUI。

You need to call repaint() when you want to re-draw your GUI because you have changed something inside. 当你想要重新绘制GUI时需要调用repaint() ,因为你已经改变了内部的东西。

If you want to delete a button, you need to remove it (or make it invisible) then you need to call validate() or repaint() to re-calculate (re-draw) the GUI. 如果要删除按钮,则需要将其删除(或使其不可见),然后需要调用validate()repaint()来重新计算(重新绘制)GUI。

The only stuff I can think of: 我能想到的唯一的东西:

    new Thread() {
    @Override
    public void run() {
    while (ClassName.this.isVisible()) {
        ClassName.this.updateStatusLabel();
        ClassName.this.validate();
        ClassName.this.repaint(50L);
        try {
        Thread.sleep(1000);
        } catch (final InterruptedException e) {
        Log.log(e);
        }
    }
    }
}.start();

Suppose you have the above code in the constructor of a JDialog. 假设你在JDialog的构造函数中有上面的代码。 What updateStatusLabel does is checking a boolean variable, either public or settable through a method, and set the icon of a JLabel in base to such boolean. updateStatusLabel所做的是检查一个布尔变量,通过方法公开或可设置,并将基础中的JLabel的图标设置为这样的布尔值。 If you don't validate and repaint the GUI, the modification won't be shown until another event, most likely an user-triggered one, is thrown. 如果您不验证并重新绘制GUI,则在另一个事件(很可能是用户触发的事件)被抛出之前,不会显示修改。 And, if the user is waiting for the label to show a certain icon because of, let's say, it indicated if a device is reachable through the Internet, he/she'll never interact (or, at least, you're delaying interactions so much). 并且,如果用户正在等待标签显示某个图标,因为,它说,它表明设备是否可以通过互联网访问,他/她将永远不会互动(或者,至少,你正在推迟互动非常)。

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

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