简体   繁体   English

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

[英]Java: JButton with custom Shape: Fill with Metal Look and Feel Gradient

I have a new class derived from JButton which gives me the shape of a Enter-Button.我有一个从JButton派生的新 class,它给了我一个 Enter-Button 的形状。

Now I want to have it filled with the same gradient as the default JButton .现在我想让它填充与默认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!谢谢!

See java.awt.GradientPaint .请参阅java.awt.GradientPaint

Also search for the source code of the update(Graphics, JComponent) method of class javax.swing.plaf.metal.MetalButtonUI .同时搜索 class javax.swing.plaf.metal.MetalButtonUIupdate(Graphics, JComponent)方法的源代码。 That's somewhere under your JDK.那是在你的 JDK 下的某个地方。

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但是为什么要为自定义外观和感觉中多次制作的东西而烦恼,其中一些允许您根据需要更改颜色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM