0

タイトルが示すように、JButton のメイン テキストの下にテキストを複製し、1 ピクセル下に移動しようとしています。ぼかしなしで、テキストのすぐ下に1ピクセルのドロップシャドウのような効果を付けようとしています。

ボタンではなく actionListener を使用して JPanel を設定する方が簡単でしょうか?

コード -

import java.awt.Color;
import java.awt.Component;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;

import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;

class ButtonUI extends JButton {

    private Color startColor = new Color(162, 110, 235);
    private Color endColor = new Color(110, 49, 237);
    private Color rollOverTopColor = new Color(171, 137, 219);
    private Color rollOverBottomColor = new Color(129, 89, 216);

    private Color pressedStartColor = new Color(73, 30, 113);
    private Color pressedMidColor = new Color(113, 57, 221);
    private Color pressedEndColor = new Color(93, 43, 198);

    private Color outerBorderColor = new Color(117, 14, 182);
    private Color innerBorderTopColor = new Color(178, 133, 237, 255);
    private Color innerBorderBottomColor = new Color(178, 133, 237, 150);

    /**
     * @param top
     *            , left, bottom, right
     */
    private Insets insetsForButtons = new Insets(0, 13, 4, 13);

    private int outerRoundRectSize = 4;
    private int innerRoundRectSize = 2;
    private GradientPaint GP;

    /**
     * 
     * @param text
     */
    public ButtonUI(String text) {
        super();
        setText(text);
        setAlignmentX(Component.CENTER_ALIGNMENT);
        setContentAreaFilled(false);
        setBorderPainted(false);
        setForeground(Color.WHITE);
        setFocusable(false);
        setMargin(insetsForButtons);

    }

    /**
     * 
     * @param startColor
     * @param endColor
     * @param rollOverColor
     * @param rollOverTopColor
     * @param rollOverBottomColor
     * @param pressedColor
     * @param pressedTopColor
     * @param pressedBottomColor
     */
    public ButtonUI(Color startColor, Color endColor, Color rollOverTopColor,
            Color rollOverBottomColor, Color pressedStartColor,
            Color pressedMidColor, Color pressedEndColor) {
        super();
        this.startColor = startColor;
        this.endColor = endColor;
        this.pressedStartColor = pressedStartColor;
        this.pressedMidColor = pressedMidColor;
        this.pressedEndColor = pressedEndColor;

        setForeground(Color.WHITE);
        setFocusable(false);
        setContentAreaFilled(false);
        setBorderPainted(false);
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        int h = getHeight();
        int w = getWidth();
        ButtonModel model = getModel();
        if (!model.isEnabled()) {
            setForeground(Color.BLACK);
            GP = new GradientPaint(0, 0, new Color(73, 30, 113), 0, h,
                    new Color(73, 30, 113), true);
        } else {
            setForeground(Color.WHITE);
            if (model.isRollover()) {
                GP = new GradientPaint(0, 0, rollOverTopColor, 0, h,
                        rollOverBottomColor, true);
            } else {
                GP = new GradientPaint(0, 0, startColor, 0, h, endColor, true);
            }
        }
        g2d.setPaint(GP);
        GradientPaint p1;
        GradientPaint p2;
        if (model.isPressed()) {
            GP = new GradientPaint(0, 0, pressedStartColor, 0, h,
                    pressedMidColor, true);
            g2d.setPaint(GP);
            p1 = new GradientPaint(0, 0, outerBorderColor, 0, h - 1,
                    outerBorderColor);

            // BE SURE TO EDIT TO MAKE LESS DRAMATIC
            p2 = new GradientPaint(0, 1, innerBorderTopColor, 0, h - 3,
                    innerBorderTopColor);
        } else {
            p1 = new GradientPaint(0, 0, outerBorderColor, 0, h - 1,
                    outerBorderColor);

            p2 = new GradientPaint(0, 1, innerBorderTopColor, 0, h - 3,
                    innerBorderBottomColor);

            GP = new GradientPaint(0, 0, startColor, 0, h, endColor, true);
        }
        RoundRectangle2D.Float r2d = new RoundRectangle2D.Float(0, 0, w - 1,
                h - 1, outerRoundRectSize, outerRoundRectSize);
        Shape clip = g2d.getClip();
        g2d.clip(r2d);
        g2d.fillRect(0, 0, w, h);
        g2d.setClip(clip);
        g2d.setPaint(p1);
        g2d.drawRoundRect(0, 0, w - 1, h - 1, outerRoundRectSize,
                outerRoundRectSize);
        g2d.setPaint(p2);
        g2d.drawRoundRect(1, 1, w - 3, h - 3, innerRoundRectSize,
                innerRoundRectSize);
        g2d.dispose();

        super.paintComponent(g);
    }
}

public class ButtonClass {

    public static void createGUI() {
        JFrame programFrame = new JFrame("Custom Button UI");
        ButtonUI testButton = new ButtonUI("Custom JButton UI!");

        programFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        programFrame.add(testButton);
        programFrame.pack();
        programFrame.setVisible(true);
        programFrame.setResizable(true);
        programFrame.setSize(250, 75);
        programFrame.setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createGUI();
            }
        });
    }
}

いつものように、これに反対票を投じる場合は、私がもっとうまくできることを説明してください。

EDIT:オーバーライドされた JButton クラスを投稿しました。これは、ボタンごとに作成するのではなく、すべてのボタンに使用するものです。

4

1 に答える 1