gpt4 book ai didi

java - 在按钮的相对位置添加 JLabel

转载 作者:行者123 更新时间:2023-12-01 08:05:58 25 4
gpt4 key购买 nike

好的,这是我到目前为止的相关代码。

  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);
}

请帮忙

最佳答案

出于多种原因,我会避免绝对布局。当您在其他 PC 上运行它时,整个事情就开始崩溃,相反,您应该依赖 Swing 中提供的布局管理器 API。

以下示例使用复合布局方法。每一行都是它自己的容器 (JPanel),然后每一行都会添加到主面板。

Prymid

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ButtonPyramid {

public static void main(String[] args) {
new ButtonPyramid();
}

public ButtonPyramid() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private String[] lines;

public TestPane() {
lines = new String[]{
"AAFITQPNXBE",
"?AXOPKMSUR",
"TGKFREUDI",
"DFEAAEOY",
"ZDE?VIF",
"G@RMLC",
"YUJGO",
"NSCP",
"KO@",
"MI",
"Y",
"B",
};

setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.gridy = 1;
gbc.gridx = 0;
for (String line : lines) {
JPanel panel = new JPanel(new GridBagLayout());
for (char ch : line.toCharArray()) {
JButton btn = new JButton(Character.toString(ch));
btn.setMargin(new Insets(10, 10, 10, 10));
panel.add(btn);
}
add(panel, gbc);
gbc.gridy++;
}
JLabel label = new JLabel(":1234");
gbc.gridy -= 3;
gbc.gridx++;
gbc.anchor = GridBagConstraints.NORTH;
add(label, gbc);
}
}

}

如果您不希望文本占据另一列,您可以尝试一个小技巧,将标签布局约束更改为如下所示...

JLabel label = new JLabel(":1234");
gbc.gridy -= 3;
gbc.anchor = GridBagConstraints.NORTHEAST;
add(label, gbc);

查看Laying out Components within a Container了解想法和细节

           lines = new char[][]{...};

for (int outter = 0; outter < lines.length; outter++) {
JPanel panel = new JPanel(new GridBagLayout());
for (int inner = 0; inner < lines[outter].length) {
char ch = lines[outter][inner];
JButton btn = new JButton(Character.toString(ch));
btn.setMargin(new Insets(10, 10, 10, 10));
panel.add(btn);
}
add(panel, gbc);
gbc.gridy++;
}

关于java - 在按钮的相对位置添加 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21618866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com