Java GridLayout抛出非法组件位置
默认情况下,GridLayout(5,3)将以以下方式添加组件: A B C D E F G H I J K L M N O 但结果是:Java GridLayout抛出非法组件位置,java,swing,layout-manager,grid-layout,Java,Swing,Layout Manager,Grid Layout,默认情况下,GridLayout(5,3)将以以下方式添加组件: A B C D E F G H I J K L M N O 但结果是: 线程“AWT-EventQueue-0”java.lang.IllegalArgumentException中出现异常:组件位置非法。更改按钮计划添加(按钮,索引)到按钮计划添加(按钮[索引])。(您不需要foreach循环)GridLayout总是像第一个示例中所示那样添加组件,但是如果您能够正确地计算索引(参见其他答案),那么它会像“a,F,K,B…”一样
线程“AWT-EventQueue-0”java.lang.IllegalArgumentException中出现异常:组件位置非法。更改
按钮计划添加(按钮,索引)代码>到按钮计划添加(按钮[索引])代码>。(您不需要foreach循环)GridLayout总是像第一个示例中所示那样添加组件,但是如果您能够正确地计算索引(参见其他答案),那么它会像“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);
}
}
导入java.awt.BorderLayout;
导入java.awt.GridLayout;
导入javax.swing.JButton;
导入javax.swing.JComponent;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
//进口。。。
公共类网格布局问题{
私有静态final int NUM_ROWS=5,NUM_COLMS=3;
private static JPanel mainPanel=new JPanel();
private JPanel buttonpanel=新的JPanel(新的网格布局(NUM_ROWS,NUM_COLMS));
私有JButton btnA=新JButton(“A”);
私有JButton btnB=新JButton(“B”);
私有JButton btnC=新JButton(“C”);
私有JButton btnD=新JButton(“D”);
私有JButton btnE=新JButton(“E”);
私有JButton btnF=新JButton(“F”);
私有JButton btnG=新JButton(“G”);
私有JButton btnH=新JButton(“H”);
私有JButton btnI=新JButton(“I”);
私有JButton btnJ=新JButton(“J”);
私有JButton btnK=新JButton(“K”);
私有JButton btnL=新JButton(“L”);
私有JButton btnM=新JButton(“M”);
私有JButton btnN=新JButton(“N”);
私有JButton btnO=新JButton(“O”);
专用JComponent[]按钮={
btnA,btnB,btnC,btnD,btnE,
btnF、btnG、btnH、btnI、btnJ、,
btnK、btnL、btnM、btnN、btnO
};
公共静态void main(字符串[]args){
新的GridLayoutProblem();
}
公共网格布局问题(){
JFrame=新JFrame();
新线程(newrunnable()){
公开募捐{
对于(int i=0;i
我做了一个快速测试,您似乎无法跳过索引并将元素添加到更高的索引中
所以你的选择是这样做
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);
}
for(int i=0;i
在按钮panel.Add()周围添加一个try catch。当抛出异常时,打印出您计算的索引,并确定计算出错的位置。在i=1 index=3时失败。好的,我将每个循环的整体更改为:buttonpanel.add(btnB,3)代码>并再次引发异常。所以结论是GridLayout不允许这样做。@Iridrio你读过这两个答案了吗?Codebender已经说过“你不能跳过索引,不能向更高的索引中添加元素。”@LuxxMiner我拒绝了它(我的错),因为我遵循的参考手册似乎是错误的。它表明您实际上可以添加任意索引,但不能。
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);
}