如何创建带圆角的JButton扩展?

时间:2009-06-17 13:42:17

标签: java swing rounded-corners jbutton

这是问题"Java rounded Swing JButton"的延续。我搜索了javax.swing.JButton的扩展,它将继承所有运行时行为并且只是覆盖角的绘制。

noah.w上使用sun forums page提供的代码,结果如下:

alt text

我希望能够在背景中使用相同的渐变,在鼠标上进行更改等等。有人知道该怎么做吗?

从图片中创建Java Swing窗口的代码是:

public class XrButton extends JButton implements MouseListener {

    private static final long serialVersionUID = 9032198251140247116L;

    String text;
    boolean mouseIn = false;

    public XrButton(String s) {
        super(s);
        text = s;
        setBorderPainted(false);
        addMouseListener(this);
        setContentAreaFilled(false);
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        if (getModel().isPressed()) {
            g.setColor(g.getColor());
            g2.fillRect(3, 3, getWidth() - 6, getHeight() - 6);
        }
        super.paintComponent(g);

        if (mouseIn)
            g2.setColor(Color.red);
        else
            g2.setColor(new Color(128, 0, 128));

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setStroke(new BasicStroke(1.2f));
        g2.draw(new RoundRectangle2D.Double(1, 1, (getWidth() - 3),
                (getHeight() - 3), 12, 8));
        g2.setStroke(new BasicStroke(1.5f));
        g2.drawLine(4, getHeight() - 3, getWidth() - 4, getHeight() - 3);

        g2.dispose();
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.getContentPane().setLayout(new FlowLayout());

        XrButton xrButton = new XrButton("XrButton");
        JButton jButton = new JButton("JButton");

        frame.getContentPane().add(xrButton);

        frame.getContentPane().add(jButton);

        frame.setSize(150, 150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
        mouseIn = true;
    }

    public void mouseExited(MouseEvent e) {
        mouseIn = false;
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

4 个答案:

答案 0 :(得分:1)

我认为你在这里有两个选择:

1.使用组件的绘制方法实现绘图

2.为您的外观创造新的ButtonUI。在这种情况下,我建议使用Synch LAF

在这两种情况下,绘制不同的状态是你的责任

答案 1 :(得分:1)

我在Swing中看到的圆形按钮的最佳实现是物质外观:

https://substance.dev.java.net/

并非所有主题都有圆角按钮,因此您可能需要更改演示中的默认值。该项目是开源的,因此可能值得在代码中探讨一些想法。

答案 2 :(得分:1)

我也需要这样做,这就是我最终的结果(仅限Metal LAF!)

@Override
    protected void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
        Shape firstClip = g.getClip();
        RoundRectangle2D rect = new RoundRectangle2D.Double();
        double arc = Math.ceil(getSize().getHeight()/3);
        rect.setRoundRect(0, 0, Math.ceil(getSize().getWidth()), Math.ceil(getSize().getHeight()), arc, arc);
        Area secondClip = new Area(getBounds());
        secondClip.subtract(new Area(rect));
        Area finalClip = new Area(firstClip);
        finalClip.subtract(secondClip);
        g2.setClip(finalClip);
        super.paintComponent(g2);
        Color[] gradients;
        if(getModel().isRollover())
        {
            gradients = new Color[] { new Color(184, 207, 229), new Color(122, 138, 153), new Color(184, 207, 229) };
        }
        else
        {
            gradients = new Color[] { new Color(122, 138, 153) };
        }
        for(int i = 0; i < gradients.length; i++)
        {
            arc -= 2;
            g2.setColor(gradients[i]);
            g2.drawRoundRect(i+1, i+1, (int)Math.ceil(getSize().getWidth()-2)-(i*2), (int)Math.ceil(getSize().getHeight()-2)-(i*2), (int)arc, (int)arc);
        }
    }

看起来像这样: http://i.stack.imgur.com/unZuc.png

答案 3 :(得分:0)

你必须自己照顾它:

  • 首先,您可以捕获按钮上用于更改状态的各种操作(例如在代码中,其中mouseIn用于更改颜色)
  • 然后你必须关心你需要的每一个油漆细节,例如对于渐变你应该指向用于绘制渐变的类GradientPaint

顺便说一下,你必须要注意各种外观,并且你的解决方案只适合其中一种......