Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:JButton和自定义形状:填充金属外观和感觉渐变_Java_Swing_Jbutton_Look And Feel - Fatal编程技术网

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

Java:JButton和自定义形状:填充金属外观和感觉渐变,java,swing,jbutton,look-and-feel,Java,Swing,Jbutton,Look And Feel,我有一个从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

我有一个从
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);

    }

}
谢谢大家!

还可以搜索类
javax.swing.plaf.metal.MetalButtonUI
update(Graphics,JComponent)
方法的源代码。那就在你的JDK下。

看看或者


但是,为什么要费心去做很多次的东西呢?有些东西可以让你随心所欲地改变颜色

无法创建代码格式。相反,选择代码示例并单击消息发布表单上方的
{}
按钮。