2008年10月30日 星期四

[Java]動態生成JButton 問題究竟出在哪裡?(已解決)

問題究竟出在哪裡?

import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JFrame {

JFrame myFrame;
JPanel myPanel;
JButton btn[];
Toolkit toolkit = Toolkit.getDefaultToolkit();

Main() {
myPanel = new JPanel();


for (int i = 0; i < 5; i++) {
btn[i] = new JButton();
myPanel.add(btn[i]);
}
add(myPanel);
}

public static void main(String argv[]) {
Main app = new Main();
app.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {
System.out.println("Frame closing...");
System.exit(0);
}
});
app.setVisible(true);
app.repaint();
}
}


原來是這裡忘了初始= =

JButton btn[];

改成

JButton btn[] = new JButton[5];

沒有留言:

張貼留言