现在我用java创建gui音乐播放器。在java.swing.JButton中,有方形按钮,但我想自定义该按钮,如音乐播放器的按钮。如何制作按钮,如播放按钮'在音乐播放器?而且我也想要停止和重置按钮。
播放按钮就像是这个圆形的形状
停止按钮就像||这个形状是圆形的。
重置按钮就像■这个形状的圆圈。
感谢您的评论
答案 0 :(得分:0)
您可以将播放状态设置为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);
您也可以为其他按钮添加相同的逻辑。
答案 1 :(得分:0)
您可以让按钮在i中有图像,或者使图像成为按钮本身。
答案 2 :(得分:0)
您可以通过将所需的图像设置为按钮来实现。这样你就可以有一个带有Play图标的按钮!
示例:
JButton button= new JButton();
ImageIcon img = new ImageIcon("imgfolder/name.png");
button.setIcon(img);
答案 3 :(得分:0)
您只需将JButton的文本设置为符号▶
即可JButton button = new JButton("▶");
你需要保存带有UTF-8字符集的.java文件,在eclipse中你可以很容易地获得弹出窗口。
它是最简单但最不易定制的解决方案。 另一种解决方法是使用您希望按钮显示的任何符号创建图像。然后将rectangle添加到图像的边界。要检查鼠标点击次数,只需使用MouseListener并执行与此类似的操作:
if(mouse.isClicked() && rect.contains(mouse.x, mouse.y) { //do stuff }
答案 4 :(得分:0)
如果您想制作自定义按钮,只有一种简单的方法。第一种方法是找到自定义按钮库或make your button from image。
否则,您可以尝试look on this another post。
答案 5 :(得分:0)
除了使用渲染图像(如PNG)和ImageIcon
之外,您还可以使用Java2D形状/区域。
E.g。要创建 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;
}
}
答案 6 :(得分:-1)
使用JButton的setBorder方法。
roundButton.setBorder(new RoundedBorder(10));