【问题标题】:JTextField super.paintComponent() ignores background color when on LinuxJTextField super.paintComponent() 在 Linux 上忽略背景颜色
【发布时间】:2016-10-28 04:43:24
【问题描述】:

好的,所以我已经为此工作了一段时间,真的很茫然。在我的程序中,我通过创建扩展某些JComponents 的新类、覆盖它们的paintComponent 方法并将这些类用于对象来接近自定义UI 外观。然而,这就是我迷路的地方。我有一个简单的窗口,它要求用户给它一个目录,然后它检查目录,如果它无法创建一个该位置的目录。我正在两台计算机上开发这个,使用 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" );

    }

}

谢谢大家的帮助,伙计们!

编辑 -- 截图:

窗户:

Linux:

【问题讨论】:

  • super.paintComponent() 将清除背景。因此,您在该声明之前完成的所有自定义绘画都将丢失。
  • @camickr 这是特定于在 Linux 上摆动的吗?就像我提到的,它适用于 Windows,所以这会让我想象 super.paintComponent() 在这种情况下使用 getBackground() 作为颜色,但这里有别的东西。此外,盒子仍然是一个圆角矩形,所以我不会想象它会完全清除它。另外,我忘了提一下,如果这也有什么不同的话,这就是 SE 8。不过,谢谢!
  • 忽略我的第一条评论是错误的。
  • 我在 Linux 上也看到了这个问题。任何解决方案@Drayux?

标签: java linux swing jtextfield paintcomponent


【解决方案1】:
public JTextField testBox = new JTextField();

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

如果您确实使用自定义文本字段,它仍然无法工作,即使在 Windows 上也是如此。

this.setOpaque( false );

上面的声明说组件不绘制自己的背景。因此,您只会看到父组件的背景。

也许由于您的自定义边框,代码似乎可以工作,但文本组件本身不会绘制背景。

【讨论】:

  • 不幸的是,这仍然不起作用。正如我在帖子中提到的(抱歉,如果不够清楚),第二个代码块是一个 确实 工作的 单独 程序,所以我可以断定问题出在在我的自定义文本字段中的某处。我很快就会在这里添加一些屏幕截图,但在 Windows 上仍然存在 setOpaque(false) 着色。我可能是错的,但我知道将它设置为不在超类绘制组件中绘制它自己的背景正是我想要的,所以我可以像我一样绘制自己的。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2021-03-01
    • 1970-01-01
    • 2015-02-20
    • 2023-03-26
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    相关资源
    最近更新 更多