3

我对 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 中,它将不起作用。但是如何以及为什么,我没有一个线索。 在此处输入图像描述

4

1 回答 1

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();
                }
            }
        });
    }
}
于 2013-05-03T14:23:41.020 回答