【发布时间】: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