【问题标题】:Java - GridBagLayout position JLabelJava - GridBagLayout 位置 JLabel
【发布时间】:2015-12-02 20:39:18
【问题描述】:

我使用GridBagLayout 并在其上放置JLabel,如图所示。我设法通过ipadxipadyJLabel 的大小设置为所需的大小,但我似乎无法使用它的位置。似乎它总是以中间为中心,而我想从红点开始并且在调整窗口大小时不要重新定位自己。我该怎么办?

谢谢。我使用的代码也是:

GridBagLayout gbl = new GridBagLayout();
setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();

JLabel jl = new JLabel("This is a jlabel!", SwingConstants.CENTER);
jl.setBorder(BorderFactory.createLineBorder(Color.black));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipadx = 87;
gbc.ipady = 220;
add(jl, gbc);

【问题讨论】:

  • 玩锚和填充

标签: java swing awt gridbaglayout


【解决方案1】:

我对其进行了改进...现在您可以使用 insets 将该标签定位在 JFrame 周围,但经过一些研究后,我建议选择不同的 layout manager

所以你的代码看起来像:

Dimension d = new Dimension(350, 400);
GridBagLayout gbl = new GridBagLayout();
JFrame frame = new JFrame("Heloo");
frame.setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();

JLabel jl = new JLabel("This is a jlabel!", SwingConstants.CENTER);
jl.setBorder(BorderFactory.createLineBorder(Color.black));
gbc.ipadx = 87;
gbc.ipady = 220;
gbc.insets = new Insets(0, 0, 360, 340);// here work with JFrame size!!

希望这段代码对你有帮助:)

【讨论】:

  • 它做到了!非常感谢!如果我调整窗口大小,它似乎也会移动。你有最后一个解决方案吗? ;)
  • 更改布局管理器...或设置 frame.setResizable(false)。
猜你喜欢
  • 2015-12-03
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多