2016-11-16 114 views
0

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

播放按钮就像▶这个圆形的形状。
停止按钮与||一样。这个形状在圈子里。
重置按钮就像■这个圆形的形状。

感谢您的评论

回答

0

您可以设置类似图像将JButton的发挥。

保存图像名为“playname”到“路径/到/图片/”,并呼吁为以下代码所示:

// 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); 

您同样可以添加相同的逻辑,其他按钮了。

0

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

-1

使用JButton的setBorder方法。

roundButton.setBorder(new RoundedBorder(10)); 
0

你可以通过设置你想要的按钮的图像。这样您就可以拥有一个带有Play图标的按钮!

例子:

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

你可以简单地设置一个JButton的文本作为符号▶

JButton button = new JButton("▶"); 

您需要保存使用UTF-8字符的java文件虽然设置,在日食它很容易,你会得到一个弹出窗口。

这是最简单但最不可定制的解决方案。 另一个解决方法是用任何希望显示按钮的符号创建图像。然后添加一个rectangle到图像的边界。要检查的鼠标点击,只需使用MouseListener,做一些与此类似:

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

除了使用渲染图像(如PNG)和ImageIcon,您可以使用Java2D形状/面积。

E.g.创建播放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; 
    } 
}