我試着讓我的應用程序的外觀看起來是這樣的:我JPanels沒有顯示,因爲我希望他們
但是當我嘗試做這個,我得到這樣的:
下面是我用它來創建我的兩個JPanels的代碼,我怎麼添加按鈕和洙..
//This is the panel that shows the image
appletRunningPanel = new ImagePanel();
appletRunningPanel.setSize(600, 300);
appletRunningPanel.validate();
//This is the panels that shows the 3 buttons
appletRunningPanel2 = new Panel(new GridLayout(1, 3));
appletRunningPanel2.setSize(600, 300);
appletRunningPanel2.add(test1);
appletRunningPanel2.add(test2);
appletRunningPanel2.add(test3);
appletRunningPanel2.validate();
//Then i add them to the applet with this:
add(appletRunningPanel);
add(appletRunningPanel2);
這裏是ImagePanel
public class ImagePanel extends JPanel{
private BufferedImage image;
public ImagePanel() {
setSize(600, 300);
try {
image = ImageIO.read(getClass().getResourceAsStream("/res/TCHLogo.png"));
} catch (IOException ex) {
// handle exception...
System.out.println(ex);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
}
1)JButtons被添加到JPanel包含Image ?, 2)也許你想要resiziable你的形象?+1發佈想法 – mKorbel