java Java动态添加按钮作为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13946415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java add buttons dynamically as an array
提问by William Kinaan
Possible Duplicate:
java - How would I dynamically add swing component to gui on click?
I want to add array of buttons dynamically. I tried like this:
我想动态添加按钮数组。我试过这样:
this.pack();
Panel panel = new Panel();
panel.setLayout(new FlowLayout());
this.add(panel);
panel.setVisible(true);
for (int i = 0; i < Setting.length; i++) {
for (int j = 0; j < Setting.width; j++) {
JButton b = new JButton(i+"");
b.setSize(30, 30);
b.setLocation(i * 30, j * 30);
panel.add(b);
b.setVisible(true);
}
}
but didn't get anything , what mistake did I make?
但没有得到任何东西,我犯了什么错误?
Edit
编辑
I have jFrame class "choices" on it i have a button , when I press the button, this is supposed to happen:
我有 jFrame 类“选择”我有一个按钮,当我按下按钮时,这应该发生:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
structure.Setting s = new Setting(8, 8, 3, 1, 1);
Game g = new Game();
g.setSetting(s);
this.dispose();
g.show();
}
then i go to the Game class (also jFrame class) to the function setSetting and it is like this:
然后我转到 Game 类(也是 jFrame 类)到函数 setSetting ,它是这样的:
void setSetting(Setting s) {
this.setting = s;
structure.Game game = new structure.Game(setting);
JPanel panel = new JPanel(new GridLayout(5, 5, 4, 4));
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
JButton b = new JButton(String.valueOf(i));
panel.add(b);
}
}
add(panel);
pack();
setVisible(true);
}
structure.Setting setting;
}
采纳答案by adatapost
You may use GridLayoutto add equal height/width buttons:
您可以使用GridLayout添加等高/等宽的按钮:
public class Game extends JFrame {
private JPanel panel;
public Game(int rows,int cols,int hgap,int vgap){
panel=new JPanel(new GridLayout(rows, cols, hgap, vgap));
for(int i=1;i<=rows;i++)
{
for(int j=1;j<=cols;j++)
{
JButton btn=new JButton(String.valueOf(i));
panel.add(btn);
}
}
add(panel);
pack();
setVisible(true);
}
}
and code in button's handler should be:
按钮处理程序中的代码应该是:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Game g = new Game(5,5,3,3);
}
Note that you can also pass Setting
object reference via Game
constructor (when you may add widgets dynamically) instead of calling setSetting
method.
请注意,您还可以Setting
通过Game
构造函数传递对象引用(当您可以动态添加小部件时)而不是调用setSetting
方法。
回答by MadProgrammer
The JPanel
is already under the control of a layout manager, setting the size and position of the buttons is irrelevant, as they will changed once the panel is validated.
所述JPanel
已经是一个布局管理器的控制下,设置按钮的大小和位置是无关紧要的,因为一旦面板被验证它们将改变。
Try, instead, adding the panel AFTER you've populated it with buttons..
相反,尝试在使用按钮填充面板后添加面板..
UPDATED with Example
更新示例
Without further evidence, we are only guessing...You now have two people who have no issues.
没有进一步的证据,我们只是猜测……你现在有两个没有问题的人。
Panel panel = new Panel();
for (int i = 0; i < Setting.length; i++) {
for (int j = 0; j < Setting.width; j++) {
jButton b = new jButton(i + "");
panel.add(b);
}
}
this.add(panel);
public class BadBoy {
public static void main(String[] args) {
new BadBoy();
}
public BadBoy() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
int buttonCount = (int) Math.round(Math.random() * 20);
int columnCount = (int) Math.round(Math.random() * 20);
JPanel buttonPane = new JPanel(new GridLayout(0, columnCount));
for (int i = 0; i < buttonCount; i++) {
JButton b = new JButton(i + "");
buttonPane.add(b);
}
frame.add(buttonPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ButtonPane extends JPanel {
public ButtonPane() {
}
}
}
回答by Mohammod Hossain
回答by Martin Larsson
I am guessing that this
extens or is some kind of frame?
我猜这是this
扩展还是某种框架?
First step is to pack the frame by doing this.pack();
sets the frame size ti just fit all objects.
第一步是通过this.pack();
设置框架大小来打包框架,使其适合所有对象。
I assume you have set it to visible?
我假设您已将其设置为可见?
Now you should be able to see the buttons.
If you want a different layout use panel.setLayout(new SomeLayout);
现在您应该能够看到按钮了。如果您想要不同的布局,请使用panel.setLayout(new SomeLayout);