Java Swing GUI : Moving around components specifically with layouts

标签 java swing user-interface flowlayout

我正在为我正在制作的东西制作一个小的测试 GUI。 但是,面板的定位会出现问题。

public winInit() {
    super("Chatterbox - Login");

    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch(ClassNotFoundException e) {
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
    } catch (UnsupportedLookAndFeelException e) {
    }

    setSize(300,135);

    pn1 = new JPanel();
    pn2 = new JPanel();
    pn3 = new JPanel();

    l1 = new JLabel("Username");
    l2 = new JLabel("Password");
    l3 = new JLabel("Random text here");
    l4 = new JLabel("Server Address");
    l5 = new JLabel("No address set.");

    i1 = new JTextField(10);

    p1 = new JPasswordField(10);

    b1 = new JButton("Connect");
    b2 = new JButton("Register");
    b3 = new JButton("Set IP");

    l4.setBounds(10, 12, getDim(l4).width, getDim(l4).height);
    l1.setBounds(10, 35, getDim(l1).width, getDim(l1).height);
    l2.setBounds(10, 60, getDim(l2).width, getDim(l2).height);
    l3.setBounds(10, 85, getDim(l3).width, getDim(l3).height);
    l5.setBounds(l4.getBounds().width + 14, 12, l5.getPreferredSize().width, l5.getPreferredSize().height);

    l5.setForeground(Color.gray);

    i1.setBounds(getDim(l1).width + 15, 35, getDim(i1).width, getDim(i1).height);
    p1.setBounds(getDim(l1).width + 15, 60, getDim(p1).width, getDim(p1).height);

    b1.setBounds(getDim(l1).width + getDim(i1).width + 23, 34, getDim(b2).width, getDim(b1).height - 5);
    b2.setBounds(getDim(l1).width + getDim(i1).width + 23, 60, getDim(b2).width, getDim(b2).height - 5);
    b3.setBounds(getDim(l1).width + getDim(i1).width + 23, 10, etDim(b2).width, getDim(b3).height - 5);

    b1.addActionListener(clickButton);
    b2.addActionListener(clickButton);
    b3.addActionListener(clickButton);

    pn1.setLayout(new FlowLayout(FlowLayout.RIGHT));
    pn2.setLayout(new FlowLayout(FlowLayout.RIGHT));

    pn1.add(l1);
    pn1.add(i1);
    pn1.add(b1);

    pn2.add(l2);
    pn2.add(p1);
    pn2.add(b2);

    add(pn1);
    add(pn2);

}

我正在尝试使用 FlowLayout 以所需的方式定位面板。我会在添加时使用 BorderLayout,但是当我只使用彼此最接近的方向时,垂直间距太远了。

此代码的输出是创建一个窗口 300,150,将两个面板中的任何内容都放在完全相同的空间中。

是的,我知道 setBounds() 中有一些无用的代码,但那只是我在玩绝对定位,这对我来说也没有用。

编辑:

问题已解决...我想。 我现在很高兴使用 MiGLayout,但我也看到了 GridBagLayout 必须提供的功能。从你们的一些例子来看,这似乎与我想要达到的目标非常相似。

感谢您的回答。

最佳答案

如果你必须做绝对定位(除了动画我不推荐这样做),那么布局需要设置为null,而不是FlowLayout

对于数据采集面板,我建议使用布局管理器的智能组合,包括考虑一些非标准布局,例如 MiGLayout。

编辑澄清:MiGLayout 可能比 GridBagLayout 更容易使用,但如果您熟悉 GridBagLayout,它就不会那么难用,而且不需要下载,因为它是核心 Java 库的一部分。我自己,我可能会使用嵌套的 JPanel,它们使用布局的组合,可能是整体 GUI 的 BorderLayout 和数据采集面板的 GridBagLayout,其中包含 JLabel/JTextField 组合的行。

关于Java Swing GUI : Moving around components specifically with layouts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12658314/

相关文章:

Java:减去日期的最简单方法

java - JDBC无法连接到openshift上的mysql数据库

java - 为我的应用程序中的每个 java swing 组件创建自定义类

java - Swing JTable 的重绘表问题

user-interface - 对 Log4Net 配置 Gui 有什么建议吗?

javascript - 在 Angular 应用程序的弹出窗口中显示网页

java - 使用 for 循环的方法不会打乱我的数组元素或重写所有元素

java - 如何在 Java 中的 JOptionPane.showInputDialog() 上显示图标?

java - JScrollPane 不起作用,视口(viewport)正在堆叠面板

java - 使用不带 @GeneratedValue 注释的 JPA 实体 ID