如何在运行时更改边框颜色?

3

我正在尝试更改JScrollPane的边框颜色:

JScrollPane scroll = new JScrollPane (textPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
Color color = new Color(150, 255, 243);
scroll.setBorder(new CompoundBorder(new EmptyBorder(3, 3, 4, 4), new LineBorder(color, 7)));

//some other code

//if smth happens then:

color = Color.red;

但是我的“滚动条”始终保持不变。我该如何看到边框颜色的变化?
4个回答

6

在Java42的示例中,有一种更简单的方法可以覆盖paintBorder()方法,因为lineColor字段是受保护的。所以只需执行以下操作:

@Override
public void paintBorder(final Component c, final Graphics g, final int x, final int y, final int width, final int height) {
    super.lineColor = color;
    super.paintBorder(c, g, x, y, width, height);
}

5

要动态改变LineBorder(),您需要重写它的paintBorder()方法。

以下是关键代码片段:

    lineBorder = new LineBorder(color, 7) {
        @Override
        public void paintBorder(final Component c, final Graphics g, final int x, final int y, final int width, final int height) {
            // super.paintBorder(c, g, x, y, width, height);
            final boolean doSimple = true;
            if (doSimple) {
                g.setColor(color);
                g.fillRect(x, y, width, height);
            }
       }
  };

以下是一个教学例子:
public class LineBorder_ColorChange {

static JTextPane  textPane    = new JTextPane();
static JScrollPane scrollPane;
static Color      color       = new Color(150, 255, 243);
static Border     emptyBorder = new EmptyBorder(3, 3, 4, 4);
static Border     lineBorder;
static Border     border;
static JButton    jButton     = new JButton("Change Color");
static Random     random      = new Random();

public static void main(final String[] args) {

    final JFrame jFrame = new JFrame();

    lineBorder = new LineBorder(color, 7) {
        @Override
        public void paintBorder(final Component c, final Graphics g, final int x, final int y, final int width, final int height) {
            // super.paintBorder(c, g, x, y, width, height);
            final boolean doSimple = false;
            if (doSimple) {
                g.setColor(color);
                g.fillRect(x, y, width, height);
            }
            else {
                if ((this.thickness > 0) && (g instanceof Graphics2D)) {
                    final Graphics2D g2d = (Graphics2D) g;
                    final Color oldColor = g2d.getColor();
                    g2d.setColor(color);
                    Shape outer;
                    Shape inner;
                    final int offs = this.thickness;
                    final int size = offs + offs;
                    if (this.roundedCorners) {
                        final int arc = offs + size;
                        outer = new RoundRectangle2D.Float(x, y, width, height, arc, arc);
                        inner = new RoundRectangle2D.Float(x + offs, y + offs, width - size, height - size, arc, arc);
                    }
                    else {
                        outer = new Rectangle2D.Float(x, y, width, height);
                        inner = new Rectangle2D.Float(x + offs, y + offs, width - size, height - size);
                    }
                    final Path2D path = new Path2D.Float(Path2D.WIND_EVEN_ODD);
                    path.append(outer, false);
                    path.append(inner, false);
                    g2d.fill(path);
                    g2d.setColor(oldColor);
                }
            }
        }
    };

    border = new CompoundBorder(emptyBorder, lineBorder);

    scrollPane = new JScrollPane(textPane);

    jButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            color = new Color(random.nextInt(142), random.nextInt(142), random.nextInt(142));
            Toolkit.getDefaultToolkit().beep();
            jFrame.repaint();
        }
    });

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            scrollPane.setBorder(border);
            jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            jFrame.getContentPane().setLayout(new BorderLayout());
            jFrame.getContentPane().add(jButton, BorderLayout.SOUTH);
            jFrame.getContentPane().add(scrollPane, BorderLayout.CENTER);
            jFrame.setSize(200, 200);
            jFrame.setLocationRelativeTo(null);
            jFrame.setVisible(true);
        }
    });

}
}

4
改变颜色变量并不会实际改变边框的颜色。您需要重新设置边框,而不仅仅是更改颜色变量。
想象一个更简单的例子,一个包含一些文本的JLabel。
String text = "example";
JLabel label = new JLabel(text);
text = "changed";
标签仍然会显示“example”,即使变量已更改。

您仍然需要再次设置的文本:

String text = "example";
JLabel label = new JLabel(text);
text = "changed";
label.setText(text);

同样地,您需要再次设置边框。

没错,他的问题是为什么当他在代码后面更改颜色变量时,边框不会自动更改。 - Kevin Workman
我曾认为有一个技巧可以保持我创建的复合边框,而不是每次想要更改其颜色时都创建一个新的边框,但这个方法有效!谢谢。 - Dario
1
@Dario,你可以保留对LineBorder(带有颜色的边框)的引用,但它没有setColor()方法。你可以尝试通过子类化LineBorder并自己跟踪颜色来进行一些黑客攻击,但重新设置边框可能是更好的选择。 - Kevin Workman
1
@Dario:如果Kevin的回答解决了你的问题(应该会),请接受它,然后稍后点赞。 - Hovercraft Full Of Eels

0

我刚开始学习Java,遇到了更新边框颜色的问题。如果你只使用几种颜色,有一种替代方法可以覆盖paint方法,那就是为每种颜色创建一个单独的border对象。

在我的情况下,我想要一个与背景颜色匹配的边框(看起来透明),以及当组件被选中时为黑色边框,每次选择标签时切换边框颜色。下面的代码不完整,但我希望它提供足够的信息。

public class YourClass { 
    final private static java.awt.Color TRANSPARENT_BORDER = new java.awt.Color(190,190,225); 
    final private static java.awt.Color VISIBLE_BORDER     = new java.awt.Color(  0,  0,  0); 
    final private LineBorder transparentBorder = new LineBorder( TRANSPARENT_BORDER, 1); 
    final private LineBorder visibleBorder     = new LineBorder( VISIBLE_BORDER,     1); 

    public YourClass() 
    { 
        yourLabel = new JLabel(); 
        yourLabel.setBorder( transparentBorder ); 

        yourLabel.addMouseListener( new MouseAdapter()  
        { 
            @Override
            public void mouseClicked( MouseEvent e )  
            { 
                if ( borderColor == TRANSPARENT_BORDER ) { 
                    yourLabel.setBorder(  visibleBorder );
                    borderColor = VISIBLE_BORDER; 
                } else {
                    yourLabel.setBorder( transparentBorder ); 
                    borderColor   = TRANSPARENT_BORDER; 
                } 
            } 
        }); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接