将按钮数组分配给Gridlayout时
因此,我从头开始启动了一个计算器,以练习Java知识。 我有一个小问题,当我使用jlabel
使用gridlayout
将按钮分配给 返回异常java.lang.illegalgumentException
。 我知道我正在做一些愚蠢的事情,我最好重新组织数组,并为循环做一个分配按钮,但是我太懒了,无法做到这一点,想了解我做错了什么。
这是代码:
buttonDisplay.setBounds(30, 30, 400, 320); //buttonDisplay is a JLabel
buttonDisplay.setLayout(new GridLayout(5,4,5,5));
operator = new JButton[20];
operator[0] = new JButton("0");
operator[1] = new JButton("1");
operator[2] = new JButton("2");
operator[3] = new JButton("3");
operator[4] = new JButton("4");
operator[5] = new JButton("5");
operator[6] = new JButton("6");
operator[7] = new JButton("7");
operator[8] = new JButton("8");
operator[9] = new JButton("9");
operator[10] = new JButton("+");
operator[11] = new JButton("-");
operator[12] = new JButton("*");
operator[13] = new JButton("/");
operator[14] = new JButton(".");
operator[15] = new JButton("=");
operator[16] = new JButton("AC");
operator[17] = new JButton("C");
operator[18] = new JButton("TAX+");
operator[19] = new JButton("TAX-");
buttonDisplay.add(operator[0],4,2); //exception starts on this line
buttonDisplay.add(operator[1],3,3);
buttonDisplay.add(operator[2],3,2);
buttonDisplay.add(operator[3],3,1);
buttonDisplay.add(operator[4],2,3);
buttonDisplay.add(operator[5],2,2);
buttonDisplay.add(operator[6],2,1);
buttonDisplay.add(operator[7],1,3);
buttonDisplay.add(operator[8],1,2);
buttonDisplay.add(operator[9],1,1);
buttonDisplay.add(operator[10],1,4);
buttonDisplay.add(operator[11],2,4);
buttonDisplay.add(operator[12],3,4);
buttonDisplay.add(operator[13],4,4);
buttonDisplay.add(operator[14],4,1);
buttonDisplay.add(operator[15],4,3);
buttonDisplay.add(operator[16],5,1);
buttonDisplay.add(operator[17],5,2);
buttonDisplay.add(operator[18],5,3);
buttonDisplay.add(operator[19],5,4);
So I have started a calculator from scratch in order to practice Java knowledge.
I have a slight problem, when I'm assigning buttons to a JLabel
using GridLayout
the console
returns an exception java.lang.IllegalArgumentException
.
I know I am doing something foolish and I better reorganize the array and do a for loop to assign buttons, but I am too lazy to do it and want to understand what I did wrong.
Here is the code:
buttonDisplay.setBounds(30, 30, 400, 320); //buttonDisplay is a JLabel
buttonDisplay.setLayout(new GridLayout(5,4,5,5));
operator = new JButton[20];
operator[0] = new JButton("0");
operator[1] = new JButton("1");
operator[2] = new JButton("2");
operator[3] = new JButton("3");
operator[4] = new JButton("4");
operator[5] = new JButton("5");
operator[6] = new JButton("6");
operator[7] = new JButton("7");
operator[8] = new JButton("8");
operator[9] = new JButton("9");
operator[10] = new JButton("+");
operator[11] = new JButton("-");
operator[12] = new JButton("*");
operator[13] = new JButton("/");
operator[14] = new JButton(".");
operator[15] = new JButton("=");
operator[16] = new JButton("AC");
operator[17] = new JButton("C");
operator[18] = new JButton("TAX+");
operator[19] = new JButton("TAX-");
buttonDisplay.add(operator[0],4,2); //exception starts on this line
buttonDisplay.add(operator[1],3,3);
buttonDisplay.add(operator[2],3,2);
buttonDisplay.add(operator[3],3,1);
buttonDisplay.add(operator[4],2,3);
buttonDisplay.add(operator[5],2,2);
buttonDisplay.add(operator[6],2,1);
buttonDisplay.add(operator[7],1,3);
buttonDisplay.add(operator[8],1,2);
buttonDisplay.add(operator[9],1,1);
buttonDisplay.add(operator[10],1,4);
buttonDisplay.add(operator[11],2,4);
buttonDisplay.add(operator[12],3,4);
buttonDisplay.add(operator[13],4,4);
buttonDisplay.add(operator[14],4,1);
buttonDisplay.add(operator[15],4,3);
buttonDisplay.add(operator[16],5,1);
buttonDisplay.add(operator[17],5,2);
buttonDisplay.add(operator[18],5,3);
buttonDisplay.add(operator[19],5,4);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我运行您的代码的一部分时,以下行抛出了一个例外:
这是我得到的堆栈跟踪的[一部分]:
您要调用的方法是:
- 但不是。
gridlayout
在添加组件时没有提供指示行和列的能力。我认为 gridbagaglayout 可能是更合适的布局经理您正在尝试做什么。另外,即使类
javax.swing.jlabel
从classjava.awt.container
继承,它也不打算包含jbutton
s。还有其他用于此目的的 swing 组件,例如 javax.swing.jpanel 。因此,我建议用
jpanel
和使用gridbaglayout
替换jlabel
。也许以下内容更符合您要实现的目标。
这是运行上述代码时显示的内容。
When I ran [part of] your code, the following line threw an exception:
Here is [part of] the stack trace I got:
The method you are calling is this one:
public void add(Component comp, Object constraints, int index)
I assume that you think that the second and third arguments are the row and column positions in the grid – but they are not.
GridLayout
does not provide the ability to indicate row and column when adding a component. I think that GridBagLayout is probably a more appropriate layout manager for what you are trying to do.Also, even though class
javax.swing.JLabel
inherits from classjava.awt.Container
, it is not intended to containJButton
s. There are other Swing components that are intended for this purpose, such as javax.swing.JPanel.So I suggest replacing
JLabel
withJPanel
and usingGridBagLayout
.Perhaps the following is more in line with what you are trying to achieve.
This is what is displayed when running the above code.
Jlabel不是容器,它用于放置纯文本或图像。但是我知道您需要一个可以容纳所有按钮的容器。因此,您应该使用jpanel。
这是对您有很大帮助的代码。
JLabel is not a container it is used to place a plain text or image. but i understand that you need something a container that can hold all the buttons. So, you should use JPanel.
here is the code that helps you a lot.