繁体   English   中英

如何使用java.swing使按钮像音乐播放器一样

[英]How to make button like music player using java.swing

现在,我用Java创建gui音乐播放器。 在java.swing.JButton中,有方形按钮,但是我想像音乐播放器的按钮一样自定义该按钮。 如何在音乐播放器中制作“播放按钮”之类的按钮? 我也想要停止和重置按钮。

播放按钮就像▶圆形的形状。
停止按钮就像|| 此形状为圆形。
重置按钮就像■圆形的形状。

谢谢你的评论

您只需将JButton的文本设置为符号▶

JButton button = new JButton("▶");

但是,您需要使用UTF-8字符集保存.java文件,在eclipse中,当您弹出窗口时,这真的很容易。

这是最简单但可定制性最强的解决方案。 另一个解决方法是使用您希望按钮显示的任何符号创建图像。 然后将矩形添加到图像的边界。 要检查鼠标单击,只需使用MouseListener并执行类似的操作:

if(mouse.isClicked() && rect.contains(mouse.x, mouse.y) { //do stuff }

您可以将播放图像设置为JButton。

将名为“ playname”的图像保存到“ path / to / image /”,然后按以下代码所示进行调用:

// JButton play = new JButton();
// assuming play is the place where you've added your JButton
ImageIcon playimg = new ImageIcon("path/to/image/playname");
play.setIcon(playimg);

您也可以类似地为其他按钮添加相同的逻辑。

您可以使按钮在i中具有图像,也可以使图像本身成为按钮。

您可以通过将所需的图像尺寸设置为按钮来实现。 这样,您可以使用“播放图标”按钮!

例:

JButton button= new JButton();
ImageIcon img = new ImageIcon("imgfolder/name.png");
button.setIcon(img);

如果要创建自定义按钮,只有一种简单的方法可以实现。 第一种方法是找到自定义按钮库或从图像制作按钮

否则,您可以尝试看看另一篇文章

除了使用渲染图像(如PNG)和ImageIcon ,还可以使用Java2D Shapes / Areas。

例如,创建一个Play Area ,执行以下操作:

final GeneralPath play = new GeneralPath();
play.moveTo(1.0/5.0, 1.0/5.0);
play.lineTo(1.0/5.0, 1.0*4.0/5.0);
play.lineTo(1.0*4.0/5.0, 1.0/2.0);
play.closePath();
final Area playArea = new Area(play);

要将形状绘制为Icon ,请使用以下自定义类:

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class ShapeIcon implements Icon {

    private final Shape shape;
    private final Paint paint;
    private final Color color;
    private final int size;
    private final boolean fill;
    private final Stroke stroke;

    public ShapeIcon(final Shape shape, final Color color, final int size) {
        this(shape, color, size, true, new BasicStroke(0.5f));
    }

    public ShapeIcon(final Shape shape, final Color color, final int size, final boolean fill, final Stroke stroke) {
        this.stroke = stroke;
        this.fill = fill;
        this.color = color;
        // allow for customization of fill color/gradient
        // a plain color works just as well—this is a little fancier
        this.paint = new GradientPaint(0, 12, color.brighter(), 0, 20, color);
        this.size = size;
        // you could also define different constructors for different Shapes
        if (shape instanceof Path2D) {
            this.shape = ((Path2D)shape).createTransformedShape(AffineTransform.getScaleInstance(size, size));
        } else if (shape instanceof Area) {
            this.shape = ((Area) shape).createTransformedArea(AffineTransform.getScaleInstance(size, size));
        } else {
            this.shape = new Area(shape).createTransformedArea(AffineTransform.getScaleInstance(size, size));
        }
    }

    @Override
    public void paintIcon(final Component c, final Graphics g, final int x, final int y) {
        final Graphics2D g2d = (Graphics2D)g.create();
        g2d.translate(x, y);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        if (fill) {
            g2d.setPaint(paint);
            g2d.fill(shape);
        }
        g2d.setPaint(color);
        g2d.setStroke(stroke);
        g2d.draw(shape);
        g2d.dispose();
    }

    @Override
    public int getIconWidth() {
        return size;
    }

    @Override
    public int getIconHeight() {
        return size;
    }
}

使用JButton的setBorder方法。

roundButton.setBorder(new RoundedBorder(10));

暂无
暂无

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

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