Java Swing 渲染在 JDK 1.8 中出现损坏,在 JDK 1.7 中更正

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22737535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:35:14  来源:igfitidea点击:

Swing rendering appears broken in JDK 1.8, correct in JDK 1.7

javaswingrenderingnvidiajava-8

提问by duffy356

I have installed IntelliJ IDEA (13.1.1 #IC-135.480) and JDK 1.8.0 (x64) and I generated some GUI with the GUI Form designer.

我已经安装了 IntelliJ IDEA (13.1.1 #IC-135.480) 和 JDK 1.8.0 (x64) 并且我用 GUI 表单设计器生成了一些 GUI。

Then I ran the code and realized that something is not alright.

然后我运行了代码并意识到有些不对劲。

Here is a Screenshot of my GUI: Buggy Rendering JDK 1.8.0

这是我的 GUI 的屏幕截图: 错误渲染 JDK 1.8.0

The rendering of the Font seems to be not OK. Additionally the Button looses its Text, when I move my mouse over it.

字体的渲染似乎不太好。此外,当我将鼠标移到按钮上时,按钮会丢失其文本。

So I installed JDK 1.7.0_40 (x64), recompiled the Project and ran it again.

所以我安装了 JDK 1.7.0_40 (x64),重新编译项目并再次运行它。

The following Form appears, when i use JDK 1.7: Rendering OK JDB 1.7.0_40

当我使用 JDK 1.7 时,会出现以下表单: 渲染 OK JDB 1.7.0_40

The Rendering seems to be OK and the Button is also OK.

渲染似乎没问题,按钮也正常。

So I installed the newest Graphics and Chipset drivers and much more, but the problem still exists. Have you ever had such problems with Java Swing UIs? Could you help me to fix my problems? Any help would be greatly appreciated.

所以我安装了最新的显卡和芯片组驱动程序等等,但问题仍然存在。您在 Java Swing UI 方面遇到过这样的问题吗?你能帮我解决我的问题吗?任何帮助将不胜感激。



UPDATE:

更新:

I created a small Project with only 1 JTextArea and 1 JButton and i have the same rendering problems.

我创建了一个只有 1 个 JTextArea 和 1 个 JButton 的小项目,并且我遇到了相同的渲染问题。

According to Andrew Thompson i changed the setSize() and I start from EDT. The Example below:

根据 Andrew Thompson 的说法,我更改了 setSize() 并从 EDT 开始。下面的例子:

package at.maeh.java.client.simpleTextClient;

import javax.swing.*;
import java.awt.*;

public class SimpleClient extends JFrame {
    private JPanel panel1;
    private JTextArea textArea1 = new JTextArea();
    private JButton button1 = new JButton();

    public SimpleClient() {
        super("SimpleClient");

        // Panel
        panel1 = new JPanel();
        panel1.setLayout(new FlowLayout());

        // BUtton
        button1.setText("TestButton");

        // TextArea
        textArea1.setColumns(40);
        textArea1.setRows(15);

        // Add Components
        panel1.add(textArea1);
        panel1.add(button1);

        // Add to Frame
        this.getContentPane().add(panel1);

        // pack and set Visible
        pack();
        setVisible(true);

        System.out.println("Constructor EDT: " + SwingUtilities.isEventDispatchThread());
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(SimpleClient::new);

        System.out.println("Main EDT: " + SwingUtilities.isEventDispatchThread());
    }
}

The result is a simple JFrame with the Components in it.

结果是一个包含组件的简单 JFrame。

SimpleClient ->Screenshot immediately after Start

SimpleClient ->启动后立即截图

When I write some Text, or moove my mousePointer over the Button the components get rendered like this (Text: testsentence.123; Button-Label: TestButton)

当我写一些文本,或者将鼠标指针移到按钮上时,组件会像这样呈现(文本:testsentence.123;按钮标签:TestButton)

after writing - or mouse over

写入后 - 或鼠标悬停

采纳答案by duffy356

Finally I found the solution for my problem. I had to change the energy control settings of my NVIDIA GeForce 620M to maximum performance.

最后我找到了我的问题的解决方案。我不得不将 NVIDIA GeForce 620M 的能量控制设置更改为最高性能。

回答by Andrew Thompson

textArea1.setSize(500, 300);

There is a problem. Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement or sizingof components. To organize the components for a robust GUI, instead use layout managers, or combinations of them1, along with layout padding & borders for white space2.

有一个问题。Java GUI 可能必须在多个平台上工作,使用不同的屏幕分辨率并使用不同的 PLAF。因此,它们不利于组件的精确放置或大小调整。要为强大的 GUI 组织组件,请改用布局管理器或它们的组合1,以及用于空白区域的布局填充和边框2

回答by nbred

I have exactly the same problem. What I found after duffys quality setting adjustion:

我有完全一样的问题。我在 duffys 质量设置调整后发现:

My Notebook runs both an Intel HD 4000 and a NVS 5200M. Running Swing applications with Java 7 is fine on both cards. With Java 8 Swing GUIs rendered by the Intel card (regardless of quality settings) look crumbled (as in duffys Screenshot), using the NVS 5200M they look as they used to.

我的笔记本同时运行 Intel HD 4000 和 NVS 5200M。在两张卡上都可以使用 Java 7 运行 Swing 应用程序。使用英特尔卡渲染的 Java 8 Swing GUI(无论质量设置如何)看起来很崩溃(如在 duffys 屏幕截图中),使用 NVS 5200M 时,它们看起来像以前一样。

Could not find anything on the java bug tracker, on the otherhand it is hard to describe this bug.

在 java 错误跟踪器上找不到任何东西,另一方面,很难描述这个错误。

回答by Halfabee2

Similar fix to duffy356:

对 duffy356 的类似修复:

Lenovo T430s running Windows 7 64 bit with NVIDIA NVS 5200M alongside HD 4000. Update to Java 8 update 5 created graphics and button corruption for two Swing apps, jEdit and Freemind.

Lenovo T430s 运行 Windows 7 64 位和 NVIDIA NVS 5200M 以及 HD 4000。更新到 Java 8 update 5 为两个 Swing 应用程序 jEdit 和 Freemind 创建了图形和按钮损坏。

Using the NVIDIA control panel applet to change the global settings for Power Management Mode to "Prefer maximum performance" looks like it fixed this issue.

使用 NVIDIA 控制面板小程序将电源管理模式的全局设置更改为“首选最大性能”似乎解决了这个问题。

Not sure if there is a lurking issue with how the apps set sizes.

不确定应用程序如何设置大小是否存在潜在问题。

回答by 25mhz

For those whose problem has not been solved; try this solution:

对于那些问题没有解决的人;试试这个解决方案:

Set the global environment variable "J2D_D3D" to "false" inside the OS. According to Sun, this setting is used to turn off the Java 2D system's use of Direct3D in Java 1.4.1_02 and later.

在操作系统内部将全局环境变量“J2D_D3D”设置为“false”。据 Sun 称,此设置用于在 Java 1.4.1_02 及更高版本中关闭 Java 2D 系统对 Direct3D 的使用。

ie: simply create a environmental variable with name "J2D_D3D" and value "false" without the quotes.

即:只需创建一个名称为“J2D_D3D”且值为“false”且不带引号的环境变量。

this has solved my rendering problems, hope it does yours also.

这已经解决了我的渲染问题,希望它也能解决你的问题。

回答by Lord ST

You can follow this steps :

您可以按照以下步骤操作:

  1. Right-click on desktop and click NVIDIA Control Panel
  2. Select 'Manage 3D Settings' in the left pane
  3. Select the "Program Settings" tab in the main window
  4. Click 'Add'
  5. Navigate to your java folder (C:\Program Files\Java\jre1.8.0_20\bin) and select javaw.exe and press 'open'(this step will probably depend on your operating system but should be pretty easy to find)
  6. Where it says 'Select the preferred graphics processor for this program' click the drop-down and choose 'High Performance NVIDIA processor'
  7. Click 'Apply' in the bottom right and you're good to go.
  1. 右键单击桌面,然后单击 NVIDIA 控制面板
  2. 在左侧窗格中选择“管理 3D 设置”
  3. 在主窗口中选择“程序设置”选项卡
  4. 点击“添加”
  5. 导航到您的 java 文件夹 (C:\Program Files\Java\jre1.8.0_20\bin) 并选择 javaw.exe 并按“打开”(此步骤可能取决于您的操作系统,但应该很容易找到)
  6. 在显示“为此程序选择首选图形处理器”的地方单击下拉菜单并选择“高性能 NVIDIA 处理器”
  7. 点击右下角的“应用”,你就可以开始了。

回答by Vishal Patil

steps:

脚步:

  1. control panel
  2. system and security
  3. system
  4. Remote settings
  5. Advanced tab
  6. click Environment variables
  7. create a J2D_D3D variable
  8. and set the value to false.
  1. 控制面板
  2. 系统和安全
  3. 系统
  4. 远程设置
  5. 高级选项卡
  6. 点击环境变量
  7. 创建 J2D_D3D 变量
  8. 并将值设置为 false。

worked for me,....

为我工作,....

回答by Rafael Gimenes

Dell Vostro 5480 with ubuntu mate 16.04 I had a similar issue. It's fixed changing the Nvidia Driver, using an additional driver.

带有 ubuntu mate 16.04 的 Dell Vostro 5480 我遇到了类似的问题。它修复了使用附加驱动程序更改 Nvidia 驱动程序的问题。

enter image description here

在此处输入图片说明

And select the NVidia Profile. enter image description here

并选择 NVidia 配置文件。 在此处输入图片说明