编程语言
首页 > 编程语言> > Java SE 6:为什么Nimbus L&F在JTextField中剪切4个像素?

Java SE 6:为什么Nimbus L&F在JTextField中剪切4个像素?

作者:互联网

我的JFrame由一些JTextField组成.对于Windows操作系统,我只使用系统外观.对于Linux发行版,我使用Java SE 6附带的Nimbus L& F.

在Nimbus L& F中,JTextField的下边框截断了4个像素.因此,像“ g”这样的字母被切掉,看起来像“ a”.

有人知道如何消除文本字段内的白色边框吗?

这是问题的说明:

这是一个SSCCE(示例代码):

public class NimbusTextFieldIssue extends javax.swing.JFrame {

    public NimbusTextFieldIssue() {
        initComponents();
    }

    private void initComponents() {

        txtField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        txtField.setText("The letters \"g\" and \"p\" are not shown correctly.");
        txtField.setPreferredSize(new java.awt.Dimension(234, 22));

        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()
                .addComponent(txtField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }


    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {

        } catch (InstantiationException ex) {

        } catch (IllegalAccessException ex) {

        } catch (javax.swing.UnsupportedLookAndFeelException ex) {

        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NimbusTextFieldIssue().setVisible(true);
            }
        });
    }

    private javax.swing.JTextField txtField;

}

提前谢谢了!

解决方法:

我对Nimbus没有这种问题.您可能还有其他导致此问题的原因.查看以下示例.

更新

您永远不要在任何组件上调用setPreferredSize()(我的意思是真的).它总是在不同的外观和感觉上导致问题.如果要指示文本字段的宽度,请使用JTextField.setColumns(int).

import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

public class TestNimbusTextField {

    private void initUI() {
        JFrame frame = new JFrame(TestNimbusTextField.class.getSimpleName());
        frame.setLayout(new FlowLayout());
        JTextField textfield = new JTextField(20);
        textfield.setText("good morning. Look like I have no problems with 'g' and 'p'.");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(textfield);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        LookAndFeelInfo[] installedLookAndFeels = UIManager.getInstalledLookAndFeels();
        for (LookAndFeelInfo lafInfo : installedLookAndFeels) {
            if (lafInfo.getName().equals("Nimbus")) {
                UIManager.setLookAndFeel(lafInfo.getClassName());
                break;
            }
        }
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestNimbusTextField().initUI();
            }
        });
    }

}

标签:swing,jtextfield,nimbus,java
来源: https://codeday.me/bug/20191031/1975077.html