我剛開始製作一個簡單的MP3播放器,我正在創建播放,向前,後退等...按鈕,但出於某種原因只有第一個按鈕出現,並且使第二個按鈕出現,我必須去滾動它。如果你能幫我解決這個問題,那會很棒。我使用兩個圖像,一個名爲play.jpg,另一個名爲next.png。JButton不可見,直到我滾動它
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Graphic extends JPanel{
JFrame f = new JFrame();
JPanel p = new JPanel(new GridBagLayout());
public Graphic(){
gui();
}
public void gui(){
f.setVisible(true);
f.setSize(1600,900);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
ppr(75,26,25,25,"pics/play.jpg");
//above is the play button
ppr(40,26,25,25,"pics/next.png");
// above is the button that wont appear until it is scrolled over (it is just to the left of the button above
}
public void ppr(int x, int y, int width, int height, String file){
p.setLayout(null);
Toolkit tool = Toolkit.getDefaultToolkit();
Image player = tool.getImage(file);
ImageIcon playbutton = new ImageIcon(player);
JButton play = new JButton(playbutton);
play.setBounds(x, y, width, height);
p.add(play);
// ********************** above is the the method that makes a button
}
public static void main(String args[]) {
new Graphic();
}
}
要強調:*不要*做任何手動調整/定位組件 - 永遠 - 使用合適的LayoutManager – kleopatra 2013-03-23 11:20:10