Java Mig布局:对齐不良
我的米格布局有问题。我已经开始重新创建JFrame的主JPanel,我在过去使用了绝对布局。起初一切顺利(第二幅图)。控制台面板(带有选项卡面板的框的一部分)对齐良好,但仍然是绝对布局。当我开始将单个JPanel的布局转换为Mig布局时,它看起来像第一张图像(没有左对齐)。同样的结果也适用于我将绝对布局更改为Mig布局的其他JPanel [坏的] [好] [调试1000次校准] 这是我的frame类的简化版本。这个结构看起来很奇怪,因为我试着尽量减少它。我还删除了控制台面板,因为我的问题 甚至出现带有MigLayout的默认JPanelJava Mig布局:对齐不良,java,swing,layout,miglayout,Java,Swing,Layout,Miglayout,我的米格布局有问题。我已经开始重新创建JFrame的主JPanel,我在过去使用了绝对布局。起初一切顺利(第二幅图)。控制台面板(带有选项卡面板的框的一部分)对齐良好,但仍然是绝对布局。当我开始将单个JPanel的布局转换为Mig布局时,它看起来像第一张图像(没有左对齐)。同样的结果也适用于我将绝对布局更改为Mig布局的其他JPanel [坏的] [好] [调试1000次校准] 这是我的frame类的简化版本。这个结构看起来很奇怪,因为我试着尽量减少它。我还删除了控制台面板,因为我的问题 甚至出
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中,它将不起作用。但我一点也不知道是怎么回事,为什么。
我认为问题在于您仍然使用的
BoxLayout
。我添加了更多的布局,现在看起来就像你想要的
不知道(没有足够的信息)只是一些一般性的评论:a)不要使用子面板b)不要使用有标题的边框c)不要硬编码像素我认为没有足够的信息得出结论。可以帮助您的一件简单的事情是将“debug 1000”添加到布局约束中,这将显示网格和组件边界。*我将“debug 1000”添加到主面板和控制台面板中。右面板(=框)具有良好的对齐效果,与预期的一样,其左边框与选项卡面板边框重合。但是,控制台面板的左边框与右面板的左边框不重合。
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();
}
}
});
}
}