同意楼上做法,不过如果改写UI的话可能更好一些(功能更强大,但较复杂)

解决方案 »

  1.   

    谢谢楼上的兄弟
    可不可以说的具体些
    我比较菜
      

  2.   

    下面这段代码就是一个椭圆按钮
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import javax.swing.plaf.basic.*;
    import javax.swing.plaf.*;
    import javax.swing.border.*;
    public class EllipseButton extends JButton {
        public EllipseButton(String text) {
            super(text);
            this.setUI(EllipseButtonUI.createUI(this));
            this.setBorder(null);
            this.setContentAreaFilled(false);
            this.setMargin(new Insets(8, 14, 8, 14));
        }
        public static void main(String [] args) {
            UIManager.put("Button.font", new Font("Arial", 0, 20));
            JFrame frame = new JFrame();
            frame.setLocation(100, 100);
            frame.setSize(200, 80);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("Elliptical Button");
            frame.getContentPane().setLayout(new GridBagLayout());
            EllipseButton button = new EllipseButton("Elliptical");
            frame.getContentPane().add(button);
            frame.getContentPane().setBackground(new Color(230, 230, 150));
            frame.setVisible(true);
        }
    }
    class EllipseButtonUI extends BasicButtonUI {
        protected static EllipseButtonUI singleton = new EllipseButtonUI();
        protected static Stroke thickStroke = new BasicStroke(2.0f);
        public static ComponentUI createUI(JComponent c) {
            return singleton;
        }
        public void paint(Graphics g, JComponent c) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            AbstractButton b = (AbstractButton) c;
            Rectangle viewRect = new Rectangle();
            viewRect.x = 0;
            viewRect.y = 0;
            viewRect.width = b.getWidth() - 1;
            viewRect.height = b.getHeight() - 1;
            viewRect.grow(-2, -2);
            Ellipse2D ellipse = new Ellipse2D.Float();
            ellipse.setFrame(viewRect.getX(), viewRect.getY(), viewRect.getWidth(), viewRect.getHeight());
            ButtonModel model = b.getModel();
            boolean pressed = (model.isArmed() && model.isPressed()) || model.isSelected();
            pressed = !pressed;
            if (!pressed) {
                Color background = UIManager.getColor("Button.select");
                g2.setPaint(background == null ? Color.gray : background);
            }
            else
                g2.setPaint(UIManager.getColor("control"));
            g2.fill(ellipse);
            Arc2D arc = new Arc2D.Float();
            arc.setFrame(viewRect.getX(), viewRect.getY(), viewRect.getWidth(), viewRect.getHeight());
            arc.setArcType(Arc2D.OPEN);
            arc.setAngles(viewRect.getWidth(), 0, 0, viewRect.getHeight());
            g2.setStroke(thickStroke);
            g2.setPaint(!pressed ?
                        UIManager.getColor("controlDkShadow") :
                        UIManager.getColor("controlHighlight"));
            g2.draw(arc);
            arc.setAngles(0, viewRect.getHeight(), viewRect.getWidth(), 0);
            g2.setPaint(!pressed ?
                        UIManager.getColor("controlHighlight") :
                        UIManager.getColor("controlShadow"));
            g2.draw(arc);
            super.paint(g, c);
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
        }
        public Dimension getPreferredSize(JComponent c) {
            AbstractButton b = (AbstractButton) c;
            Dimension dim = super.getPreferredSize(c);
            dim.height +=  (b.getMargin().top + b.getMargin().bottom);
            dim.width += (b.getMargin().left + b.getMargin().right);
            retur