【问题标题】:Miglayout or an alternative layout managerMiglayout 或替代布局管理器
【发布时间】:2016-12-07 08:58:15
【问题描述】:

我目前正在学习 Java Swing,遇到了一个我认为很有趣但在网上找不到答案的话题。所以我的问题是我有一个使用 Miglayout 的 JFrame,但它没有按照我想要的方式工作。我将发布它当前的样子以及我希望它看起来像什么的图片,我还将发布我的代码。另外,我尝试将 JPanel 放在 JPanel 上,然后使用 Miglayout 移动它们,但这没有用。 Miglayout 会与此一起使用,还是其他布局管理器会更好?

编辑 我希望它看起来像有一个 JTextArea 并且在它旁边有 3 个 JRadioButtons 组合在一起,但堆栈底部的单选按钮会重新格式化自身,因此它最终位于 JTextArea 的底部

public SecondFrame() {
    formPanel.setLayout(new net.miginfocom.swing.MigLayout());

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(500, 525);
    f.setVisible(true);
    f.setLocationRelativeTo(null);

    //by adding the buttons to a group it will only allow you to select 1 button at a time
    buttonGroupLocation.add(sydney);
    buttonGroupLocation.add(melbourne);
    buttonGroupLocation.add(brisbane);

    buttonGroupSeverityLevels.add(lowSeverityBtn);
    buttonGroupSeverityLevels.add(mediumSeverityBtn);
    buttonGroupSeverityLevels.add(highSeverityBtn);
    buttonGroupSeverityLevels.add(criticalSeverityBtn);

    formPanel.add(sydney, "cell 0 0 1 1");
    formPanel.add(melbourne, "cell 0 1 1 1, wrap");
    formPanel.add(brisbane, "cell 0 2 1 1, wrap");

    formPanel.add(issue, "cell 1 0 1 1, wrap");
    formPanel.add(issueArea, "span ");

    formPanel.add(solution, "cell 0 8 1 1, wrap");
    formPanel.add(solutionArea, "cell 0 15 8 1");

    formPanel.add(severity, "cell 0 4 1 1, wrap");
    formPanel.add(lowSeverityBtn, "cell 0 5 1 1");
    formPanel.add(mediumSeverityBtn, "cell 1 5 1 1");
    formPanel.add(highSeverityBtn, "cell 2 5 1 1");
    formPanel.add(criticalSeverityBtn, "cell 3 5 1 1");

    formPanel.add(submit, "cell 0 16 1 1");
    formPanel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));

    f.add(formPanel);

    formPanel.add(submit, "cell 0 16 1 1");

【问题讨论】:

标签: java swing layout-manager miglayout


【解决方案1】:

如果MigLayout 做不到,其他布局管理器也很难做到。 对于MigLayout,这是小菜一碟。有几种方法如何 完成你的任务。我已经通过放置三个收音机来完成它 按钮到一个单元格中。这可以通过将单元格分成三个来完成 子细胞。

package com.zetcode;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;

public class MigLayoutAirportEx extends JFrame {

    public MigLayoutAirportEx() {

        initUI();
    }

    private void initUI() {

        setLayout(new MigLayout());

        ButtonGroup bg = new ButtonGroup();

        JRadioButton sydn = new JRadioButton("Sydney");
        JRadioButton melb = new JRadioButton("Melbourne");
        JRadioButton bris = new JRadioButton("Brisbane");

        bg.add(sydn);
        bg.add(melb);
        bg.add(bris);

        JLabel issue = new JLabel("Issue");
        JTextArea area = new JTextArea(15, 20);
        area.setBorder(BorderFactory.createEtchedBorder());

        add(sydn, "split 3, flowy, aligny top");        
        add(melb);
        add(bris);
        add(issue, "split 2, flowy");
        add(area, "push, grow, wrap");

        pack();

        setTitle("ARE - Airport Retail Enteprises");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }    


    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            MigLayoutAirportEx ex = new MigLayoutAirportEx();
            ex.setVisible(true);
        });
    }
}

该示例创建了您遇到问题的 UI 的上半部分。

另外,我尝试将 JPanel 放在 JPanel 上,然后移动它们 使用 Miglayout,但这不起作用。

这些都是从糟糕的布局管理器那里继承下来的坏习惯。使用MigLayout,您不需要任何带有其他布局管理器的附加面板。强大的布局管理器不需要这个。

这是截图:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多