GridLayout 抛出非法组件位置
GridLayout throws illegal component position
默认情况下,GridLayout(5,3) 将以这种方式添加组件:
A B C
D E F
G H I
J K L
M N O
将组件布置在以下位置:
A F K
B G L
C H M
D I N
E J O
我有这个代码:
//imports...
public class GridLayoutProblem {
private static final int NUM_ROWS = 5, NUM_COLMS=3;
private JPanel mainPanel = new JPanel();
private JPanel buttonPannel = new JPanel(new GridLayout(NUM_ROWS, NUM_COLMS));
private JButton btnA = new JButton("A");
private JButton btnB = new JButton("B");
//same with C, D...
private JButton btnO = new JButton("O");
private JComponent[] buttons = {
btnA, btnB, btnC, btnD, btnE,
btnF, btnG, btnH, btnI, btnJ,
btnK, btnL, btnM, btnN, btnO
};
public GridLayoutProblem(){
int i=0;
for (JComponent button : buttons){
int index = i%NUM_ROWS*NUM_COLMS+i/NUM_ROWS;
buttonPannel.add(button,index);
i++;
}
mainPanel.add(buttonPannel);
}
//...
但结果是:
线程异常 "AWT-EventQueue-0" java.lang.IllegalArgumentException: 非法组件位置。
将 buttonPannel.add(button,index);
更改为 buttonPannel.add(buttons[index]);
。 (你不需要 foreach 循环)GridLayout 总是像你在第一个例子中展示的那样添加组件,但是如果你可以对 index
进行正确的计算(参见其他答案),那么它会像 "A,F,K,B...",你可以达到你想要的。
运行 下面的代码可以查看如何添加按钮:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
//imports...
public class GridLayoutProblem {
private static final int NUM_ROWS = 5, NUM_COLMS=3;
private static JPanel mainPanel = new JPanel();
private JPanel buttonPannel = new JPanel(new GridLayout(NUM_ROWS, NUM_COLMS));
private JButton btnA = new JButton("A");
private JButton btnB = new JButton("B");
private JButton btnC = new JButton("C");
private JButton btnD = new JButton("D");
private JButton btnE = new JButton("E");
private JButton btnF = new JButton("F");
private JButton btnG = new JButton("G");
private JButton btnH = new JButton("H");
private JButton btnI = new JButton("I");
private JButton btnJ = new JButton("J");
private JButton btnK = new JButton("K");
private JButton btnL = new JButton("L");
private JButton btnM = new JButton("M");
private JButton btnN = new JButton("N");
private JButton btnO = new JButton("O");
private JComponent[] buttons = {
btnA, btnB, btnC, btnD, btnE,
btnF, btnG, btnH, btnI, btnJ,
btnK, btnL, btnM, btnN, btnO
};
public static void main(String[] args) {
new GridLayoutProblem();
}
public GridLayoutProblem(){
JFrame frame = new JFrame();
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < NUM_ROWS * NUM_COLMS; i++) {
int index = i%NUM_COLMS*NUM_ROWS+i/NUM_COLMS;
buttonPannel.add(buttons[index]);
frame.revalidate();
frame.repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
mainPanel.add(buttonPannel);
frame.getContentPane().add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
}
}
我做了一个快速测试,似乎你不能跳过索引并将元素添加到更高的索引。
所以你的选择是做这样的事情,
for (int i = 0; i < NUM_ROWS*NUM_COLMS; i++){
int index = i%NUM_COLMS*NUM_ROWS+i/NUM_COLMS; // Note the change in calculation. Just interchange rows and colms from your algo.
buttonPannel.add(button[index],i);
}
默认情况下,GridLayout(5,3) 将以这种方式添加组件:
A B C D E F G H I J K L M N O
将组件布置在以下位置:
A F K B G L C H M D I N E J O
我有这个代码:
//imports...
public class GridLayoutProblem {
private static final int NUM_ROWS = 5, NUM_COLMS=3;
private JPanel mainPanel = new JPanel();
private JPanel buttonPannel = new JPanel(new GridLayout(NUM_ROWS, NUM_COLMS));
private JButton btnA = new JButton("A");
private JButton btnB = new JButton("B");
//same with C, D...
private JButton btnO = new JButton("O");
private JComponent[] buttons = {
btnA, btnB, btnC, btnD, btnE,
btnF, btnG, btnH, btnI, btnJ,
btnK, btnL, btnM, btnN, btnO
};
public GridLayoutProblem(){
int i=0;
for (JComponent button : buttons){
int index = i%NUM_ROWS*NUM_COLMS+i/NUM_ROWS;
buttonPannel.add(button,index);
i++;
}
mainPanel.add(buttonPannel);
}
//...
但结果是: 线程异常 "AWT-EventQueue-0" java.lang.IllegalArgumentException: 非法组件位置。
将 buttonPannel.add(button,index);
更改为 buttonPannel.add(buttons[index]);
。 (你不需要 foreach 循环)GridLayout 总是像你在第一个例子中展示的那样添加组件,但是如果你可以对 index
进行正确的计算(参见其他答案),那么它会像 "A,F,K,B...",你可以达到你想要的。
运行 下面的代码可以查看如何添加按钮:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
//imports...
public class GridLayoutProblem {
private static final int NUM_ROWS = 5, NUM_COLMS=3;
private static JPanel mainPanel = new JPanel();
private JPanel buttonPannel = new JPanel(new GridLayout(NUM_ROWS, NUM_COLMS));
private JButton btnA = new JButton("A");
private JButton btnB = new JButton("B");
private JButton btnC = new JButton("C");
private JButton btnD = new JButton("D");
private JButton btnE = new JButton("E");
private JButton btnF = new JButton("F");
private JButton btnG = new JButton("G");
private JButton btnH = new JButton("H");
private JButton btnI = new JButton("I");
private JButton btnJ = new JButton("J");
private JButton btnK = new JButton("K");
private JButton btnL = new JButton("L");
private JButton btnM = new JButton("M");
private JButton btnN = new JButton("N");
private JButton btnO = new JButton("O");
private JComponent[] buttons = {
btnA, btnB, btnC, btnD, btnE,
btnF, btnG, btnH, btnI, btnJ,
btnK, btnL, btnM, btnN, btnO
};
public static void main(String[] args) {
new GridLayoutProblem();
}
public GridLayoutProblem(){
JFrame frame = new JFrame();
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < NUM_ROWS * NUM_COLMS; i++) {
int index = i%NUM_COLMS*NUM_ROWS+i/NUM_COLMS;
buttonPannel.add(buttons[index]);
frame.revalidate();
frame.repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
mainPanel.add(buttonPannel);
frame.getContentPane().add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
}
}
我做了一个快速测试,似乎你不能跳过索引并将元素添加到更高的索引。
所以你的选择是做这样的事情,
for (int i = 0; i < NUM_ROWS*NUM_COLMS; i++){
int index = i%NUM_COLMS*NUM_ROWS+i/NUM_COLMS; // Note the change in calculation. Just interchange rows and colms from your algo.
buttonPannel.add(button[index],i);
}