【问题标题】:Array of JLabel in swing摇摆中的 JLabel 数组
【发布时间】:2013-05-11 19:58:30
【问题描述】:

我最近开始在 java 中使用 swing,我想将 JLabels 的二维数组添加到我的 JFrame 中,问题是,我想放置相同的 JFrame 和 JButtons 等东西。我的问题是,我不知道如何添加这个 JLabels 数组。

【问题讨论】:

  • 我正在寻找一些特定的布局,但没有成功。

标签: java swing jframe jlabel


【解决方案1】:

你可以看看GridLayout

使用此布局,您可以创建一个二维布局,您可以在其中添加JLabels

类似:

JPanel container = new JPanel();
GridLayout experimentLayout = new GridLayout(0,2);

container.setLayout(experimentLayout);

container.add(new JButton("Label 1,1"));
container.add(new JButton("Label 1,2"));
container.add(new JButton("Label 2,1"));
container.add(new JButton("Label 2,2"));

这将返回一个像这样的网格:

+-----------+-----------+
| Label 1,1 | Label 1,2 |
+-----------+-----------+
| Label 2,1 | Label 2,2 |
+-----------+-----------+

【讨论】:

  • 但我的问题是具体的,因为它是游戏中的一系列球。每 2 个标签(球)之间的距离必须是恒定的。 GridLayout会这样工作吗?
  • 是的,来自提供的GridLayout 链接:创建具有指定行数和列数的网格布局。布局中的所有组件都具有相同的大小。行和列中的一个(但不能同时)为零,这意味着可以将任意数量的对象放在一行或一列中。
猜你喜欢
  • 2013-10-04
  • 2018-04-07
  • 2012-06-30
  • 1970-01-01
  • 1970-01-01
  • 2011-06-26
  • 1970-01-01
  • 2019-11-13
  • 1970-01-01
相关资源
最近更新 更多