Java:具有自定义形状的 JButton:填充金属外观和感觉渐变

发布于 2024-11-14 12:53:36 字数 2265 浏览 2 评论 0原文

我有一个从 JButton 派生的新类,它为我提供了 Enter 按钮的形状。

现在我想让它填充与默认 JButton 相同的渐变。 但我不知道。我怎样才能做到这一点?

目前它充满了纯黑色。

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class EnterButton extends JButton {

    private Polygon shape;

    public EnterButton() {
        this.shape = new Polygon();
        // initialisiere Form
        this.initialize();
    }

    protected void initialize() {
        Point p1, p2, p3, p4, p5, p6;

        this.setSize(90, 120);

        p1 = new Point(0, 0);
        p2 = new Point(0, 60);
        p3 = new Point(30, 60);
        p4 = new Point(30, 120);
        p5 = new Point(90, 120);
        p6 = new Point(90, 0);

        this.shape.addPoint((int) Math.round(p1.getX()),
                (int) Math.round(p1.getY()));
        this.shape.addPoint((int) Math.round(p2.getX()),
                (int) Math.round(p2.getY()));
        this.shape.addPoint((int) Math.round(p3.getX()),
                (int) Math.round(p3.getY()));
        this.shape.addPoint((int) Math.round(p4.getX()),
                (int) Math.round(p4.getY()));
        this.shape.addPoint((int) Math.round(p5.getX()),
                (int) Math.round(p5.getY()));
        this.shape.addPoint((int) Math.round(p6.getX()),
                (int) Math.round(p6.getY()));
        this.setMinimumSize(this.getSize());
        this.setMaximumSize(this.getSize());
        this.setPreferredSize(this.getSize());
    }

    // Hit detection
    public boolean contains(int x, int y) {
        return this.shape.contains(x, y);
    }

    // Zeichne den Button
    protected void paintComponent(Graphics g) {
        Graphics2D gCopy = (Graphics2D) g.create();
        gCopy.fillPolygon(this.shape);

    }

    // zeichne die Border
    protected void paintBorder(Graphics g) {

    }

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        EnterButton button = new EnterButton();

        panel.add(button);
        frame.add(panel);

        frame.pack();
        frame.setVisible(true);

    }

}

谢谢你!

I have a new class derived from JButton which gives me the shape of a Enter-Button.

Now I want to have it filled with the same gradient as the default JButton.
But I do not know. How I can do this?

At the moment it is filled with plain black color.

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class EnterButton extends JButton {

    private Polygon shape;

    public EnterButton() {
        this.shape = new Polygon();
        // initialisiere Form
        this.initialize();
    }

    protected void initialize() {
        Point p1, p2, p3, p4, p5, p6;

        this.setSize(90, 120);

        p1 = new Point(0, 0);
        p2 = new Point(0, 60);
        p3 = new Point(30, 60);
        p4 = new Point(30, 120);
        p5 = new Point(90, 120);
        p6 = new Point(90, 0);

        this.shape.addPoint((int) Math.round(p1.getX()),
                (int) Math.round(p1.getY()));
        this.shape.addPoint((int) Math.round(p2.getX()),
                (int) Math.round(p2.getY()));
        this.shape.addPoint((int) Math.round(p3.getX()),
                (int) Math.round(p3.getY()));
        this.shape.addPoint((int) Math.round(p4.getX()),
                (int) Math.round(p4.getY()));
        this.shape.addPoint((int) Math.round(p5.getX()),
                (int) Math.round(p5.getY()));
        this.shape.addPoint((int) Math.round(p6.getX()),
                (int) Math.round(p6.getY()));
        this.setMinimumSize(this.getSize());
        this.setMaximumSize(this.getSize());
        this.setPreferredSize(this.getSize());
    }

    // Hit detection
    public boolean contains(int x, int y) {
        return this.shape.contains(x, y);
    }

    // Zeichne den Button
    protected void paintComponent(Graphics g) {
        Graphics2D gCopy = (Graphics2D) g.create();
        gCopy.fillPolygon(this.shape);

    }

    // zeichne die Border
    protected void paintBorder(Graphics g) {

    }

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        EnterButton button = new EnterButton();

        panel.add(button);
        frame.add(panel);

        frame.pack();
        frame.setVisible(true);

    }

}

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

快乐很简单 2024-11-21 12:53:36

请参阅java.awt.GradientPaint

另请搜索 javax.swing.plaf.metal.MetalButtonUI 类的 update(Graphics, JComponent) 方法的源代码。那是在你的 JDK 下的某个地方。

See java.awt.GradientPaint.

Also search for the source code of the update(Graphics, JComponent) method of class javax.swing.plaf.metal.MetalButtonUI. That's somewhere under your JDK.

烟凡古楼 2024-11-21 12:53:36

请查看此处这里

但何苦呢一些在 自定义外观和样式中多次制作的东西感觉,有些可以让你随心所欲地改变颜色

look here or here

but why bother with something that is lots of times made in Custom Look & Feels, some of them allows you change the Color as you wants

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文