2016-11-16 8 views
0

今私はjavaでGUIプレーヤーを作成します。 java.swing.JButtonには四角形のボタンがありますが、そのボタンを音楽プレーヤーのボタンのようにカスタマイズしたいと思います。音楽プレーヤーで '再生ボタン'のようなボタンを作る方法は?また、停止ボタンとリセットボタンも必要です。java.swingを使用してボタンのような音楽プレーヤーを作る方法

再生ボタンは円のような形です。
停止ボタンは次のようなものです。この円の形。
リセットボタンはこのような形をしています。あなたはJButtonのに画像のようなプレーを設定することができますあなたのコメント

答えて

0

ため

感謝。

は、「パス/に/画像/」に「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

ボタンに割り当てるimmageを設定すると、これを行うことができます。この方法であなたはプレイアイコンでボタンを持つことができます!

例:

あなたは単にで、あなたががセットUTF-8文字で.javaファイルを保存する必要があり、シンボル▶

JButton button = new JButton("▶"); 

としてのJButtonのテキストを設定することができ

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

あなたがポップアップを取得すると、それは本当に簡単です食べる。

これは最も簡単ではありますが、最もカスタマイズしにくいソリューションです。 もう1つの回避策は、ボタンに表示したいシンボルを含むイメージを作成することです。次に、画像の境界にrectangleを追加します。

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

カスタムボタンを作成したい場合は、それを行うための唯一の簡単な方法があります:単にMouseListenerを使用し、これに似た何かを、マウスクリックを確認するには。最初の方法は、カスタムボタンライブラリまたはmake your button from imageを見つけることです。

これ以外の場合は、look on this another postを試してみてください。

0

レンダリングされたイメージ(PNGなど)とImageIconを使用する代わりに、Java2D図形/領域を使用できます。

など。 プレイAreaを作成し、このような何かしますIconような形状を描画するには

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

を、このカスタムクラスを使用します。

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; 
    } 
} 
関連する問題