我刚开始制作一个简单的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