日期:2014-05-20 浏览次数:20711 次
import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; public class ButtonArray extends JFrame { public ButtonArray() { this.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public JButton[] addButtons(String[] names) { JButton[] btns = new JButton[names.length]; for (int i = 0; i < names.length; i++) { btns[i] = new JButton(names[i]); this.add(btns[i]); } return btns; } public static void main(String[] args) { ButtonArray ba = new ButtonArray(); ba.setSize(300, 200); ba.setVisible(true); String[] names = { "AAAA", "BBBB", "CCCC", "Hello", "World", "1111", "2222", "3333", "4444" }; ba.addButtons(names); } }