如何创建带圆角的JButton扩展? [英] How to create a JButton extension with rounded corners?

查看:240
本文介绍了如何创建带圆角的JButton扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is a continuation of the question "Java rounded Swing JButton". I have searched for an extension of javax.swing.JButton which will inherit all runtime behavior and just override drawing of the corners.

使用 noah.w .jspa?threadID = 284897& tstart = 28696rel =nofollow noreferrer> sun论坛页面结果如下所示:

Using the code given by noah.w on sun forums page the result looks like this:

alt text http://i40.tinypic.com/107qfkp.jpg

我希望能够在后台使用相同的渐变,鼠标移位等等。有人知道怎么做吗?

I'd like to be able to have the same gradient in the background, on mouse over change etc. Does anybody know how to do that?

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

Code which creates the Java Swing window from the picture is:

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


推荐答案

我认为你有两个选择:

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

1.Implement drawing yourself in a paint method of your component

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

2.Create new ButtonUI for your look and feel. In this case i would suggest to use Synch LAF

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

In both cases drawing different states is your resposibility

这篇关于如何创建带圆角的JButton扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆