简体   繁体   English

为什么从 JFrame 调用此 JDialog 时无法正确绘制?

[英]Why does this JDialog not draw properly when called from a JFrame?

I'm having a problem with the display of a JDialog when called from a JFrame.从 JFrame 调用时,我在显示 JDialog 时遇到问题。 The JFrame only contains a button. JFrame 仅包含一个按钮。 When pressed, this launches a JDialog with a JLabel, JTextField and JButton.按下时,将启动一个带有 JLabel、JTextField 和 JButton 的 JDialog。

The JFrame displays fine. JFrame 显示正常。 The JDialog displays strangely, kinda like it's trying to draw it (or elements of it) offset multiple times, As you move the mouse around over different components, it keeps refreshing. JDialog 显示奇怪,有点像它试图多次绘制它(或它的元素)偏移量,当您将鼠标移动到不同的组件上时,它会不断刷新。 but still weirdly, As soon as you use the mouse to resize the JDialog.但仍然很奇怪,只要您使用鼠标调整 JDialog 的大小。 it behaves normally (doesn't matter whether you increase/decrease the size).它表现正常(无论您是否增加/减少大小)。

This is how it first appears:这是它第一次出现的方式:

显示奇怪绘制的 JButton 的对话框

After resizing it a little, it is drawn properly:稍微调整一下大小后,就可以正确绘制了:

调整大小后正确绘制的对话框

Where am I going wrong please?请问我哪里错了?

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


public class Demo extends JFrame implements Runnable {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Demo());
    }
    
    public void run() {
        setTitle("My JFrame");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton btn = new JButton("Launch JDialog");
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new Dlg();
            }
        });
        add(btn);
        pack();
        setVisible(true);
    }
}
    

class Dlg extends JDialog {
    public Dlg() {
        setTitle("My JDialog");
        
        JPanel pnl = new JPanel();
        pnl.setLayout(new BoxLayout(pnl, BoxLayout.X_AXIS));
        
        JLabel lbl1 = new JLabel("Label 1");
        JTextField txt1 = new JTextField(5);
        JButton btn1 = new JButton("Button 1");
        
        pnl.add(lbl1);
        pnl.add(txt1);
        pnl.add(btn1);
        
        add(pnl);
        
        pack();
        setVisible(true);
    }
}

Edit编辑

This is on Windows 10 with JDK 17.0.2这是在带有 JDK 17.0.2 的 Windows 10 上

I've found a similar issue descibed in an Apache NetBeans Bugzilla.我在 Apache NetBeans Bugzilla 中发现了类似的问题 Comment 7 , by "everflux", describes setting a JVM parameter: “everflux”的评论 7描述了设置 JVM 参数:

I assume this is due to Java 8 enbabling the xrender extension by default.我认为这是由于 Java 8 默认启用了 xrender 扩展。 I have serious graphics issues with Netbeans 8/Java 8 on Nvidia machine, flickering, windows only scrolling partly etc.我在 Nvidia 机器上的 Netbeans 8/Java 8 存在严重的图形问题,闪烁,windows 仅部分滚动等。

I added the following flag to the netbeans startup options: -J-Dsun.java2d.opengl=true我在 netbeans 启动选项中添加了以下标志:-J-Dsun.java2d.opengl=true

this seemed to mitigate the problems, at least during a 10 minute test period.至少在 10 分钟的测试期间,这似乎缓解了这些问题。 I encourage other users experiencing graphics problems to test it and report their results.我鼓励其他遇到图形问题的用户对其进行测试并报告他们的结果。

Adding -Dsun.java2d.opengl=true either on the command line or in Eclipse as a VM argument in the Run Configuration has fixed it for me.在命令行或 Eclipse 中添加-Dsun.java2d.opengl=true作为运行配置中的 VM 参数已为我修复了它。

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

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