【问题标题】:How to change border colour run-time?如何更改边框颜色运行时?
【发布时间】:2014-08-13 18:27:02
【问题描述】:

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

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

【问题讨论】:

    标签: java swing colors border jscrollpane


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      要动态改变 LineBorder(),你必须重写它的 paintBorder() 方法。

      这是重要的sn-p:

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

      【讨论】:

        【解决方案3】:

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

        想一个更简单的例子,一个包含一些文本的 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);
        

        同样,你必须重新设置边框。

        【讨论】:

        • 对,他的问题是为什么当他稍后在代码中更改颜色变量时边框不会自动更改。
        • 我认为有一个技巧可以保持我创建的复合边框,而不是每次我想改变它的颜色时都创建一个新的边框,但是这个工作! ty
        • @Dario 您可以维护对 LineBorder 的引用(这是颜色的边框),但它没有 setColor() 方法。您可以通过继承 LineBorder 并自己跟踪颜色来将某些东西组合在一起,但是再次设置边框可能是可行的方法。
        • @Dario:如果 Kevin 的回答解决了您的问题(它应该),那么请接受它,然后再投票。
        【解决方案4】:

        我刚开始学习 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; 
                        } 
                    } 
                }); 
        

        【讨论】:

          猜你喜欢
          • 2012-11-15
          • 1970-01-01
          • 1970-01-01
          • 2020-01-12
          • 2013-02-18
          • 1970-01-01
          • 1970-01-01
          • 2010-09-09
          • 2021-09-27
          相关资源
          最近更新 更多