【发布时间】:2014-03-04 08:10:24
【问题描述】:
好的,这是我目前的相关代码。
for (int i = gameLines - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
JButton jButton = new JButton();
jButton.setSize(buttonWidth, buttonHeight);
jButton.setText("" + line[i].charAt(j));
int x = ((buttonWidth / 2 * gameLines + 1) - (buttonWidth / 2 * i) + (buttonWidth * j));
int y = (gameLines - (i + 1)) * buttonHeight;
jButton.setLocation(x, y);
panel.add(jButton);
button[i][j] = jButton;
button[i][j].setActionCommand(Integer.toString(i) + "." + Integer.toString(j));
button[i][j].addActionListener(this);
}
}
代码创建所有按钮并将其放置在我想要的位置。这花了我一段时间才弄清楚。我原本是 AppleSoft BASIC 程序员,很抱歉 i & j 变量名。
现在来玩玩。在底部 3 行按钮的右侧,我想放置一个 jLabel。 jLabel 的右边缘与顶行中最右侧按钮的右边缘对齐。每个中的文本都将右对齐,以 : 和最多 4 位数字结尾。
我想我可以像计算按钮位置一样简单地计算位置。文本,我将根据 switch/case 添加。
我在理解 JLabels 时遇到了麻烦,所以如果能得到任何帮助,我将不胜感激。
我目前的想法是:在j循环之后插入
if (i < 4)
{
JLabel jLable = new JLabel();
JLabel.setSize(???, buttonHeight);
Calculate value of X;
int y = (gameLines - (i +1)) * buttonHeight;
jLabel.setLocation(x,y);
switch (i)
{
case 3:
jLabel.setText("Buttons Clicked: " + buttonsClicked);
break;
case 2:
etc.
}
panel.add(jLabel);
}
请帮忙
【问题讨论】:
-
你有当前布局的屏幕截图吗?
-
i1192.photobucket.com/albums/aa329/Joes_Morgue/WordFunnel.jpg 我打算将提交按钮移到另一侧,以便为 jLabels 腾出空间
标签: java swing jbutton jlabel layout-manager