本文介绍了Java Swing 错误呈现 GUI 元素(Windows 8.1)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[已解决]:我有联想 Z500 笔记本,它使用自己的 Intel HD 4000 视频驱动程序.我使用这个驱动程序是因为它与我的笔记本电脑键盘上的热键交互(F11、F12 用于屏幕亮度).我在我的第二台戴尔笔记本电脑上测试了编译的 JAR 文件,一切正常!如果您在使用联想预装驱动程序的联想笔记本电脑上渲染 Swing 元素时遇到任何问题,则必须安装本机英特尔驱动程序.

[SOLVED]: I have Lenovo Z500 notebook, which use it's own video driver for Intel HD 4000. I use this driver because of it interacts with hotkeys on my laptop keyboard (F11, F12 for screen brightness). I tested compiled JAR file on my second Dell laptop and everything works fine! If you have any problems with rendering Swing elements on your Lenovo laptop with Lenovo pre-installed drivers, you have to install native Intel driver.

===

我使用 Windows L&F 制作了一个 Swing 应用程序,但是在运行(编译)之后,我看到了错误的渲染元素.但!当我使用预览时,一切看起来都很好.这是一些错误屏幕(我认为):

I made a Swing application using Windows L&F, but after Run (compile) i see wrong rendering elements. BUT! when i use preview, everything looks OK. Here's some screens of bugs (i think):

左侧:预览右侧:运行模式

如右图所示,除了按钮处于运行模式之外,每个元素(包括 jTextField、jComboBox、jSpinner 等)的左侧都有一条粗线(类似重复).有人知道有什么问题吗?这里有人说是显卡驱动的问题,请问具体怎么解决这个问题?

As you can see on the right picture, we have a thick line (like duplicate) on a left side of each element (including jTextField, jComboBox, jSpinner and others) except Buttons in Run Mode. Does anybody know what's a problem? Somebody said in here that it's problems with video card driver, but what exactly should i do to fix this problem?

  • 系统:Windows 8.1
  • NetBeans:8.0
  • Java SDK:1.8.0_11
  • System: Winodows 8.1
  • NetBeans: 8.0
  • Java SDK: 1.8.0_11

感谢您提供任何答案!

完整源代码如下:

public static void main(String args[]) {

    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Windows".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(mainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(mainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(mainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(mainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new mainWindow().setVisible(true);
        }
    });
}

由 NetBeans 生成的 GUI 元素:

GUI elements generated by NetBeans:

private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jTextField1 = new javax.swing.JTextField();
        jSpinner1 = new javax.swing.JSpinner();
        jProgressBar1 = new javax.swing.JProgressBar();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Button");

        jButton1.setText("jButton1");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jProgressBar1.setMinimum(1);
        jProgressBar1.setValue(25);
        jProgressBar1.setStringPainted(true);

        jLabel2.setText("ComboBox");

        jLabel3.setText("TextField");

        jLabel4.setText("Spinner");

        jLabel5.setText("ProgressBar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jTextField1)
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1)
                            .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jProgressBar1, jSpinner1});

        pack();
    }

推荐答案

使用 Windows 8.1 Pro 和 JRE 1.8.0_25,我在渲染的 gui 中没有看到问题,这是我的屏幕截图:

Using Windows 8.1 Pro, with JRE 1.8.0_25, i see no problem in the rendered gui, here is my screenshot:

这篇关于Java Swing 错误呈现 GUI 元素(Windows 8.1)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 16:18