在Linux上,JTextField super.paintComponent()忽略背景颜色

时间:2016-10-28 04:43:25

标签: java linux swing jtextfield paintcomponent

好吧,所以我一直在研究这个问题并且真的很茫然。在我的程序中,我通过创建扩展某些JComponent,覆盖其paintComponent方法以及将这些类用于对象的新类来接近自定义UI外观。然而,这是我失去的地方。我有一个简单的窗口,要求用户给它一个目录,然后检查目录,并将文本字段的颜色(名为DraconicTextField,将JTextField)更改为红色,如果它无法在该位置创建目录。我在两台计算机上开发这个,使用git进行交叉。此在Windows上运行,但在Linux上失败。这是代码:

class DraconicTextField extends JTextField {

    private static final long serialVersionUID = 1L;

    private static final int arcSize = 13;

    final Color textColor = new Color( 31, 31, 31 );
    final Color boxColor = new Color( 250, 250, 250 );
    final Color borderColor = new Color( 250, 250, 250, 0 );

    public DraconicTextField() {
        this.setOpaque( false ); //true gives the same result, but corners aren't rounded if set as such
        this.setForeground( textColor ); //Text color
        this.setBackground( boxColor );  //BG color
        this.putClientProperty( SwingUtilities2.AA_TEXT_PROPERTY_KEY, null );

        this.setFont( new Font( "Arial", Font.PLAIN, 18 ) );
        this.setFont( GUIUtils.getDefaultFont( this ).deriveFont( Font.PLAIN, 18f ) ); //GUIUtils is imported

        this.setBorder( new DraconicRoundBorder( arcSize, borderColor ) );

    }

    @Override
    public void paintComponent( Graphics graphics ) {
        Graphics2D graphics2d = (Graphics2D) graphics;

        graphics2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
        graphics2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON );

        graphics2d.setColor( this.getBackground() );
        graphics2d.fillRoundRect( 0, 0, this.getWidth(), this.getHeight(), arcSize, arcSize );

        super.paintComponent( graphics2d ); //I am almost certain this is the problem-causer

    }

}

为了节省一些空间,主框架类在找到要隐藏的目录时只调用gamedirBox.setBackground(/*some color*/)。 (我肯定会这样做!)

在我的测试中,我制作了一个简短(格式严格)的程序,看看实际上我是否可以改变颜色,但我可以,但这并不会覆盖paintComponent方法。 请注意,此代码不是上述代码的一部分!以下是代码:

class GuiBox extends JFrame {

    public JLabel thisIsTheLabel = this.label( "Hello again, world!" );
    public JTextField testBox = new JTextField();
    public JButton testButton = new JButton( "Change the color!" );

    private Random randy = new Random();

    public GuiBox( String title ) {
        super( title );

        this.setSize( 300, 400 );
        this.setLayout( new FlowLayout() );

        testBox.setMinimumSize( new Dimension( 200, 40 ) );
        testBox.setPreferredSize( new Dimension( 200, 40 ) );
        testBox.setText( "This is some really long string so that flow layout stops being a ****." );

        testBox.setBackground( new Color( 240, 240, 240 ) );

        testButton.addActionListener( new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                testBox.setBackground( new Color( randy.nextInt(255), randy.nextInt(255), randy.nextInt(255) ) );
                testBox.repaint(); //Notice, I do not override paintComponent()

            }


        });

        this.add( testBox );
        this.add( testButton );

        this.setVisible( true );

    }

    public static void createBox() {
        GuiBox window = new GuiBox( "test box" );

    }

}

谢谢你的帮助,伙计们!

编辑 - 屏幕截图:

视窗: Windows Functionality

Linux中: Linux Functionality

2 个答案:

答案 0 :(得分:1)

public JTextField testBox = new JTextField();

您发布的代码甚至没有使用自定义文本字段。

如果您使用自定义文本字段,即使在Windows上也无法使用。

this.setOpaque( false );

上述声明表示该组件不会绘制自己的背景。因此,您只能看到父组件的背景。

由于您的自定义边框,可能代码似乎有效,但文本组件本身不会绘制背景。

答案 1 :(得分:0)

Nimbus LAF不支持设置JTextField背景颜色:

https://bugs.openjdk.java.net/browse/JDK-8058704

相关问题