我遇到了Mig布局的问题。我已经开始重新创建JFrame的主要JPanel,我过去使用过绝对布局。最初一切顺利(cfr第二张图片)。控制台面板(带有标签面板的Box的一部分)具有良好的对齐方式,但仍然是绝对布局。当我开始将各个JPanels的布局转换为Mig布局时,它看起来像第一个图像(没有左对齐)。同样的结果也适用于我已经将绝对布局更改为Mig布局的其他JPanel。
http://i.stack.imgur.com/97Yop.png [BAD] http://i.stack.imgur.com/KTLGK.png [好]
http://i.stack.imgur.com/p3qWZ.png [debug 1000 alignments]
这是我的帧类的简化版本。结构看起来很奇怪,因为我试图尽可能地减少它。我也删除了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中,它就无法工作。但是如何以及为什么,我不是一个线索。
答案 0 :(得分: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();
}
}
});
}
}