【问题标题】:repaint swing button with different gradient用不同的渐变重绘摇摆按钮
【发布时间】:2011-05-26 10:20:16
【问题描述】:

如何在单击时使用不同的渐变重新绘制 JButton。我已经重写了paintComponent(Graphics) 方法来进行初始绘制。 Onclick 我想重新绘制它,但我不希望用户在 actionperformed 事件中这样做,因为我希望它是一个独立的组件。

任何想法如何实现这一点。

谢谢

【问题讨论】:

    标签: java swing jbutton


    【解决方案1】:

    最简单的方法是使用setPressedIcon(),但您也可以在ButtonUI 委托中覆盖paint(),如example 所示。

    【讨论】:

      【解决方案2】:

      还有一个有趣的例子:

      import java.util.List;
      import javax.swing.*;
      import javax.swing.plaf.ColorUIResource;
      
      public class GradieltButton {
      
          public static void main(String[] args) {
              Object grad = UIManager.get("Button.gradient");
              List gradient;
              if (grad instanceof List) {
                  gradient = (List) grad;
                  System.out.println(gradient.get(0));
                  System.out.println(gradient.get(1));
                  System.out.println(gradient.get(2));
                  System.out.println(gradient.get(3));
                  System.out.println(gradient.get(4));
                  //gradient.set(2, new ColorUIResource(Color.blue));
                  //gradient.set(3, new ColorUIResource(Color.YELLOW));
                  //gradient.set(4, new ColorUIResource(Color.GREEN));
                  //gradient.set(2, new ColorUIResource(221, 232, 243));//origal Color
                  //gradient.set(2, new ColorUIResource(255, 255, 255));//origal Color
                  //gradient.set(2, new ColorUIResource(184, 207, 229));//origal Color
                  gradient.set(2, new ColorUIResource(190, 230, 240));
                  gradient.set(3, new ColorUIResource(240, 240, 240));
                  gradient.set(4, new ColorUIResource(180, 200, 220));
                  //UIManager.put("Button.background", Color.pink);
              }
              SwingUtilities.invokeLater(new Runnable() {
      
                  @Override
                  public void run() {
                      new GradieltButton().makeUI();
                  }
              });
          }
      
          public void makeUI() {
              JButton button = new JButton("Click");
              JFrame frame = new JFrame();
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.add(button);
              frame.pack();
              frame.setLocationRelativeTo(null);
              frame.setVisible(true);
          }
      }
      

      @ShaggyInjun 出于某种原因写了我的 UIManager.get("Button.gradient") 返回一个空值。你知道为什么吗?我知道我正在使用 MetalTheme。

      这个Key in UIManager 返回ColorUIResource (more in UIManagerDefaults by @camickr)

      [0.3, 0.0, javax.swing.plaf.ColorUIResource[r=221,g=232,b=243], javax.swing.plaf.ColorUIResource[r=255,g=255,b=255], javax.swing.plaf.ColorUIResource[r=184,g=207,b=229]]

      需要使用ColorUIResource而不是GradientButton.gradient 返回arrays of Colors and Insets == ColorUIResource

      【讨论】:

      • 出于某种原因,我的UIManager.get("Button.gradient") 返回一个空值。你知道为什么吗?我知道我正在使用 MetalTheme。
      • 实际调用的是UIManager.getDefaults().get("Button.gradient")
      猜你喜欢
      • 1970-01-01
      • 2012-11-15
      • 2010-12-16
      • 1970-01-01
      • 2016-12-10
      • 1970-01-01
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      相关资源
      最近更新 更多