java循环语句 创建按钮

来源:百度知道 编辑:UC知道 时间:2024/06/28 17:42:52
java用循环语句 创建按钮
按扭的名字是什么?

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Button {
private JFrame frame = null;
public Button(){
frame = new JFrame("按钮窗口");
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
createButton(16);
frame.setSize(400,400);
frame.setLocation(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private void createButton(int count) {
for(int i=0;i<count;i++){
JButton button = new JButton("按钮"+i);
frame.add(button);
}
}
public static void main(String[] args) {
new Button();
}
}
你运行一下就可以看到按钮的名字了啊JButton("按钮"+i);