Java自定义按钮paintComponent不重绘默认按钮背景

时间:2013-12-28 20:13:13

标签: java swing jbutton paintcomponent

我正在使用Swing in Java制作一个自定义按钮。 这就是我的按钮的样子:

enter image description here

我想要一个带圆角的灯光按钮,我想稍后添加一个小图片。

问题是,当我覆盖paintComponent()时,将绘制灰色圆角,但是在它之后会看到旧的蓝色Swing背景。 按钮的角落必须是透明的。

这是我的代码     包游戏;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JButton;

public class ItemButton extends JButton {

    public ItemButton(String text) {

        super(text);
        this.setBorderPainted(false);
        this.setBorder(null);

    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;

        g2.setColor(new Color(220, 220, 220));
        g2.fillRoundRect(0, 0, this.getWidth() - 10, this.getHeight() - 10, 10, 10);
    }

}

2 个答案:

答案 0 :(得分:1)

有两种方式

  1. 在班级getPrefferedSize

  2. 中覆盖ItemButton
  3. 使用正确的LayoutManager,我会从GridLayout开始

答案 1 :(得分:1)

我不确定您为什么要这样做,因为JButton允许您提供Icon图片,但无论如何......

有一种方法可以使用JButton执行此操作,但这不是正确的事情,相反,您应该首先使用AbstractButton,因为它没有UI委托绘画开始例如......

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractButton;
import javax.swing.DefaultButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class CustomButton {

    public static void main(String[] args) {
        new CustomButton();
    }

    public CustomButton() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends AbstractButton {

        public TestPane() {
            setModel(new DefaultButtonModel());
            MouseHandler mh = new MouseHandler();
            addMouseListener(mh);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(40, 20);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics g2d = (Graphics2D) g.create();
            g2d.setColor(new Color(220, 220, 220));
            g2d.fillRoundRect(0, 0, this.getWidth() - 1, this.getHeight() - 1, 10, 10);
            if (getModel().isRollover()) {
                g2d.setColor(new Color(128, 128, 128));
                g2d.drawRoundRect(0, 0, this.getWidth() - 1, this.getHeight() - 1, 10, 10);
            }
            g2d.dispose();
        }

        public class MouseHandler extends MouseAdapter {

            @Override
            public void mouseEntered(MouseEvent e) {
                getModel().setRollover(true);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                getModel().setRollover(false);
            }

            @Override
            public void mousePressed(MouseEvent e) {
                getModel().setArmed(true);
                getModel().setPressed(true);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                getModel().setPressed(false);
                getModel().setArmed(false);
            }

        }

    }

}