繁体   English   中英

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

[英]How to change border colour run-time?

我正在尝试更改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;

但我的“滚动”总是一样的..我怎么能看到边框颜色的变化?

有一种更简单的方法可以通过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);
}

要动态更改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);
        }
    });

}
}

更改颜色变量实际上不会更改边框。 您必须再次设置边框,而不仅仅是更改颜色变量。

想想一个更简单的例子,一个包含一些文本的JLabel。

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

即使变量已经改变,JLabel仍会显示“示例”。

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

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

同样,您必须再次设置边框。

我刚开始学习Java并遇到了更新边框颜色的同样问题。 如果您只想使用几种颜色,则可以使用覆盖paint方法的替代方法,即为每种颜色创建单独的边框对象。

在我的例子中,我想要一个与背景颜色相匹配的边框(显示透明),以及选择组件时的黑色边框,每次选择标签时都会切换边框颜色。 下面的代码不完整,但我希望它能提供足够的信息。

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; 
                } 
            } 
        }); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM