【问题标题】:How to add gradient color to Jbuttons placed in jtoolbar如何将渐变颜色添加到放置在 jtoolbar 中的 Jbuttons
【发布时间】:2014-04-29 07:23:44
【问题描述】:

我正在使用 netbeans 平台模块来开发这个桌面应用程序。我在开发中使用 netbeans 中的拖放功能。

我需要创建一个按钮很少的工具栏。我需要为这些按钮创建渐变颜色。

我拖放JToolBar,在它上面拖放JButton 对象。 在按钮的属性中,我选择了一种我想要阴影颜色的颜色。在我修改的自定义代码中。

jbutton = new javax.swing.Jbutton();

如下

jbutton = new javax.swing.JButton(){
    @Override
    protected void paintComponent(Graphics grphcs) {
        Graphics2D g2d = (Graphics2D) grphcs;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
           RenderingHints.VALUE_ANTIALIAS_ON);
        GradientPaint gp = new GradientPaint(0, 0,
            getBackground().brighter().brighter().brighter(), 0, getHeight(),
            getBackground().darker());
         g2d.setPaint(gp);
        g2d.fillRect(0, 0, getWidth(), getHeight());
        super.paintComponent(grphcs);
    }};

当我在我的项目中将上述代码用于JPanel 时,它可以工作,但是当我将它用于按钮时它没有显示任何效果。

如何获得放置在工具栏中的按钮的渐变颜色?

【问题讨论】:

    标签: java swing netbeans jbutton jtoolbar


    【解决方案1】:

    按钮有一个contentAreaFilled 属性,它决定外观是否应该绘制按钮的内容区域。当您致电super.paintComponent 时,外观委托将覆盖您所做的工作。

    您可以将此属性设置为false,然后它应该可以工作。

    例如...

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridBagLayout;
    import java.awt.RenderingHints;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class GradientButtons {
    
        public static void main(String[] args) {
            new GradientButtons();
        }
    
        public GradientButtons() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new GridBagLayout());
                    frame.add(new RoundButton("Click me"));
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class RoundButton extends JButton {
    
            public RoundButton(String text) {
                super(text);
                setBorderPainted(false);
                setContentAreaFilled(false);
                setFocusPainted(false);
                setOpaque(false);
            }
    
            @Override
            public Dimension getPreferredSize() {
                Dimension size = super.getPreferredSize();
                int radius = Math.max(size.width, size.height);
                size.width = radius;
                size.height = radius;
                return size;
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                GradientPaint gp = new GradientPaint(0, 0,
                        Color.RED, 0, getHeight(),
                        Color.YELLOW);
                g2d.setPaint(gp);
                g2d.fillOval(0, 0, getWidth(), getHeight());
                super.paintComponent(g);
            }
    
        }
    
    }
    

    您可能需要检查ButtonModelarmed 和/或pressed 状态,以便您还可以更改单击时按钮的绘制方式,作为建议...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多