public class my_gui extends JFrame {
public my_gui() {
setTitle("Broscute 1.0 :p");
setSize(954, 320);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setIconImage(Toolkit.getDefaultToolkit().getImage("src/img/test.png"));
setVisible(true);
initUI();
}
public final void initUI() { //ui here
setLayout(null);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0, 0, 954, 320);
getContentPane().add(panel);
JButton button = new JButton("Start!");
button.setBounds(0, 0, 954, 40);
final ImagePanel[] label = new ImagePanel[4];
int i, j;
for(i=40, j=0;i<=220;i+=60, j++){
label[j] = new ImagePanel(0, i);
label[j].setBounds(0, 0, 954, 320);
panel.add(label[j]);
}
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
label[3].X += 100;
}
});
panel.add(button);
}
}
class ImagePanel extends JComponent{
public int X, Y;
float v = 10;
private BufferedImage image;
ImagePanel(){}
public ImagePanel(int x, int y) {
X = x; Y = y;
try {
image = ImageIO.read(new File("src/img/broasca.png"));
} catch (IOException ex) {
// handle exception...
}
}
@Override
public void paintComponent(Graphics g) {
super.paintChildren(g); //a friend told me I should put it here
g.drawImage(image, X, Y, this); // see javadoc for more info on the parameters
repaint(); //I think this should go here
}
}
如果我從netbeans運行它,它有時會按照預期繪製圖像..但有時候我只能看到主窗口和移動鼠標後的「開始」按鈕。
在IDE之外運行它,它無法找到圖像。
我在這裏做錯了什麼?任何意見是極大的讚賞。 謝謝你的時間。
在Code Review上問你可能會更好:http://codereview.stackexchange.com/ – DJClayworth 2011-04-18 20:17:10
不錯,我不知道Code Review。從現在開始我會記住,謝謝。 – sdadffdfd 2011-04-18 20:22:23