【问题标题】:Mig layout: bad alignmentMig 布局:对齐错误
【发布时间】:2012-09-30 02:08:22
【问题描述】:

我的 Mig 布局有问题。我已经开始重新创建 JFrame 的主 JPanel,过去我在其中使用了绝对布局。最初一切都很顺利(cfr 第二张图片)。控制台面板(带有选项卡面板的 Box 的一部分)具有良好的对齐方式,但仍然是绝对布局。当我开始将单个 JPanel 的布局转换为 Mig 布局时,它看起来像第一个图像(没有左对齐)。同样的结果也适用于我将绝对布局更改为 Mig 布局的其他 JPanel。

http://i.stack.imgur.com/97Yop.png [不好] http://i.stack.imgur.com/KTLGK.png[好]

http://i.stack.imgur.com/p3qWZ.png [调试 1000 个对齐]

这是我的框架类的简化版本。这个结构看起来很奇怪,因为我试图尽可能地减少它。我还删除了 ControlConsolePanel 因为我的问题 甚至与带有 MigLayout 的默认 JPanel 一起出现。

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import net.miginfocom.swing.MigLayout;

public class MainControll extends JFrame{

    private static final long serialVersionUID = 14L;
private JPanel configurationPane;
private JPanel feedbackPane;
private JTextArea feedback;
private JTabbedPane plotTabPane;
private JPanel consolePane;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run(){
            try {
                MainControll frame = new MainControll();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public MainControll(){
    setTitle("test");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 860, 660);
    initiateComponents();
}

private Box rightPanel;

private void initiateComponents() {
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new MigLayout("insets 0, debug 1000", "", ""));

            this.configurationPane = new JPanel();
            this.configurationPane.setBorder(getTitleBorder("Configuration"));
            this.configurationPane.setLayout(new MigLayout());

        this.plotTabPane = new JTabbedPane();
            this.plotTabPane.add("Tab1", new JPanel());
            this.consolePane = new JPanel(new MigLayout("","",""));
            // --> The MigLayout ruins the frame.
            // --> change it to this and look at the difference:
            //                  this.consolePane = new JPanel();
            this.consolePane.setBorder(getTitleBorder("Console"));

            this.feedback = new JTextArea();
        this.feedbackPane = new JPanel();
        this.feedbackPane.setBorder(getTitleBorder("Status"));
        this.feedbackPane.setLayout(new MigLayout());
        JScrollPane sbrText = new JScrollPane(this.feedback);
        this.feedbackPane.add(sbrText, "push, grow");

        this.rightPanel = new Box(BoxLayout.Y_AXIS);
        this.rightPanel.add(this.plotTabPane);
            this.rightPanel.add(this.consolePane);

        mainPanel.add(this.configurationPane, "shrinky, top, w 450!");
        mainPanel.add(this.rightPanel, "spany 5, wrap, grow, pushx, wmin 400");
        mainPanel.add(this.feedbackPane, "pushy, growy, w 450!");

        JScrollPane contentScrollPane = new JScrollPane(mainPanel);
        contentScrollPane.setBorder(BorderFactory.createEmptyBorder());
        setContentPane(contentScrollPane);
}

private Border getTitleBorder(String title){
    return BorderFactory.createTitledBorder(null, title, TitledBorder.LEFT,       TitledBorder.TOP, new Font("null", Font.BOLD, 12), Color.BLUE);
}
 }

目标: 关键是我希望控制台面板和绘图面板适合右侧面板而没有间隙(完美的边框对齐),根据右侧面板的增长和收缩行为进行扩展和收缩。

编辑:我最近发现了一个。如果我将 Mig 布局面板放在 JTabbedPane 中,它会起作用。如果我将 Mig 布局面板放在单独的 JPanel 中,它将不起作用。但是如何以及为什么,我没有一个线索。

【问题讨论】:

  • 不知道(没有足够的信息)只是几个通用的 cmets:a)不要使用子面板 b)不要使用标题边框 c)不要硬编码像素跨度>
  • 我认为没有足够的信息来得出结论。可以帮助您的一件简单的事情是将“调试 1000”添加到布局约束中,这将向您显示网格和组件边界。
  • *我在主面板和控制台面板中添加了“debug 1000”。右面板 (=Box) 具有良好的对齐方式,如预期的那样,其左边框与选项卡面板边框重合。然而,控制台面板的左边框与右面板的左边框不一致。

标签: java swing layout miglayout


【解决方案1】:

我认为问题在于您仍在使用的BoxLayout。我又添加了一些 MigLayout,现在看起来就像你想要的那样。


import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import net.miginfocom.swing.MigLayout;

public class MainControl extends JFrame {

    private static final long serialVersionUID = 14L;
    private JPanel configurationPane;
    private JPanel feedbackPane;
    private JTextArea feedback;
    private JTabbedPane plotTabPane;
    private JPanel consolePane;
    private JPanel rightPanel;

    public MainControl() {
        setTitle("test");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 860, 660);
        initiateComponents();
    }

    private void initiateComponents() {
        JPanel mainPanel = new JPanel();

        configurationPane = new JPanel();
        configurationPane.setBorder(getTitleBorder("Configuration"));
        configurationPane.setLayout(new MigLayout());

        plotTabPane = new JTabbedPane();
        plotTabPane.add("Tab1", new JPanel());

        consolePane = new JPanel(new MigLayout("", "", ""));
        consolePane.setBorder(getTitleBorder("Console"));

        feedback = new JTextArea();
        feedbackPane = new JPanel();
        feedbackPane.setBorder(getTitleBorder("Status"));
        feedbackPane.setLayout(new MigLayout());
        JScrollPane sbrText = new JScrollPane(feedback);
        feedbackPane.add(sbrText, "push, grow");

        rightPanel = new JPanel(new MigLayout("fill"));
        rightPanel.add(plotTabPane, "grow, wrap");
        rightPanel.add(consolePane, "grow");

        mainPanel.setLayout(new MigLayout("insets 0, debug 1000", "", ""));
        mainPanel.add(configurationPane, "shrinky, top, w 450!");
        mainPanel.add(rightPanel, "spany 5, wrap, grow, pushx, wmin 380");
        mainPanel.add(feedbackPane, "pushy, growy, w 450!");

        JScrollPane contentScrollPane = new JScrollPane(mainPanel);
        contentScrollPane.setBorder(BorderFactory.createEmptyBorder());
        setContentPane(contentScrollPane);
    }

   private static Border getTitleBorder(String title) {
        return BorderFactory.createTitledBorder(null, title, TitledBorder.LEFT, TitledBorder.TOP, new Font("null", Font.BOLD, 12), Color.BLUE);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    MainControl frame = new MainControl();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 2013-07-07
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 2011-05-17
    • 2014-03-05
    相关资源
    最近更新 更多