我有一个使用SwingLayout的Swing程序。 Swing组件的结构如下所示。
JFrame
JPanel (Cardlayout)
JPanel (Miglayout) - Main panel
Jpanel (Flowlayout) - Checkbox Panel
JPanel (Flowlayout) - Option Panel
我现在的问题是,我不确定如何防止复选框面板增长。我不希望它“推”出它在右边的列。我希望Wraplayout(在选项面板上工作正常)在复选框面板变得太大时包装复选框面板的内容。
这是Eclipse内部Windowsbuilder GUI的视图。其中带有“sites”标签的面板是Checkbox面板。 “选项”面板直接位于“复选框”面板的右侧。包含它们的大面板是主面板。 http://i.imgur.com/S7Njo.png
当我添加太多复选框时会发生这种情况 http://i.imgur.com/f2SZz.png
我的主要问题是我不明白为什么在主面板中第一列的列约束上设置“grow 0”并不会阻止它在内部组件变得太大时增长,因为我添加了新的复选框站点面板(站点面板可以有任意数量的复选框)。
这是主面板的miglayout。
mainPanel.setLayout(new MigLayout("", "[][grow]", "[][][][][][][][grow]"));
以下是我添加复选框面板时的组件约束
siteCheckBoxPanel = new JPanel();
mainPanel.add(siteCheckBoxPanel, "cell 0 0,alignx left,gapx 0px,aligny center");
siteCheckBoxPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
我也尝试过没有流程布局,但这并没有解决任何问题。你能提供的任何见解都会很棒!如果有人提问,我也很乐意提供更多信息。仅供参考我还在Checkbox面板所在的单元格的列和行约束上尝试“增长0”。
这是一个SSCCE。你可以抓住下面的来源。您需要的唯一依赖是miglayout。您可以单击按钮添加复选框。
https://dl.dropbox.com/u/20614368/Test.java
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
public class Test extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
private Test frame;
public void run() {
try {
frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private JPanel contentPane;
private JPanel mainPanel;
private JPanel optionPanel;
private JButton btnUploadImages;
private JLabel thumbnailLabel;
private JTextArea textArea;
private JPanel checkboxPanel;
private final Action action = new SwingAction();
/**
* Create the frame.
*/
public Test() {
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1098, 846);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new CardLayout(0, 0));
mainPanel = new JPanel();
mainPanel.setName("");
contentPane.add(mainPanel, "name_329556913535654");
mainPanel.setLayout(new MigLayout("", "[][grow]", "[][][][][][][][grow]"));
JLabel lblPicture = new JLabel("Picture");
mainPanel.add(lblPicture, "cell 0 1");
optionPanel = new JPanel();
optionPanel.setBackground(Color.red);
mainPanel.add(optionPanel, "cell 1 0 1 5,grow");
btnUploadImages = new JButton("Upload Images");
btnUploadImages.setAction(action);
thumbnailLabel = new JLabel("");
mainPanel.add(thumbnailLabel, "cell 0 4");
textArea = new JTextArea();
mainPanel.add(btnUploadImages, "cell 0 6");
checkboxPanel = new JPanel();
checkboxPanel.setBackground(Color.green);
mainPanel.add(checkboxPanel, "flowx,cell 0 0");
JLabel lblSites = new JLabel("Sites");
checkboxPanel.add(lblSites);
}
private class SwingAction extends AbstractAction {
public SwingAction() {
putValue(NAME, "Add checkbox");
putValue(SHORT_DESCRIPTION, "Some short description");
}
public void actionPerformed(ActionEvent e) {
JCheckBox box = new JCheckBox();
box.setName("Foobar!");
checkboxPanel.add(box);
contentPane.validate();
contentPane.repaint();
}
}
}
答案 0 :(得分:4)
理论上,您的设置应该按预期工作 - 只要您告诉布局
示例列约束:
// first column - restrict max
"[grow, fill, n:pref:30%]" +
// second column - restrict min
"[grow, pref:pref:n]"
实际上,我无法通过任何约束使FlowLayout和MigLayout很好地协同工作:甚至初始布局都被破坏了(f.i.optionPanel推出框架宽度)。用Rob's WrapLayout替换FlowLayout看起来更好,但并不完全是最佳的,因为盒子没有完全对齐在彼此之下。
/*
* Created on 12.09.2012
*
*/
package layout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
public class MigLayoutNested extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
private MigLayoutNested frame;
public void run() {
try {
frame = new MigLayoutNested();
frame.pack();
// frame.setSize(frame.getWidth(), frame.getHeight()* 2);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private JPanel contentPane;
private JPanel mainPanel;
private JPanel optionPanel;
private JButton btnUploadImages;
private JLabel thumbnailLabel;
private JTextArea textArea;
private JPanel checkboxPanel;
private final Action action = new SwingAction();
/**
* Create the frame.
*/
public MigLayoutNested() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = (JPanel) getContentPane(); //new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new CardLayout(0, 0));
mainPanel = new JPanel();
contentPane.add(mainPanel, "name_329556913535654");
mainPanel.setLayout(new MigLayout("debug",
"[grow, fill][grow, pref:pref:n]"
, "[grow, fill][][][][][][][grow]"
));
// stand-in to simulate a row-spanning all-growing component
optionPanel = new JPanel();
((FlowLayout) optionPanel.getLayout()).setAlignment(JLabel.RIGHT);
JLabel option = new JLabel("just some label so we see the trailing corner, long enough");
optionPanel.add(option);
optionPanel.setBackground(Color.YELLOW);
mainPanel.add(optionPanel, "cell 1 0 1 5, grow");
// the panel to dynamically add components to
// expected behaviour is to wrap on revalidate if needed
// not working with FlowLayout, but looks half-way fine
// with Rob's WrapLayout
checkboxPanel = new JPanel(new WrapLayout());
checkboxPanel.setBackground(Color.green);
JLabel lblSites = new JLabel("Sites");
checkboxPanel.add(lblSites);
mainPanel.add(checkboxPanel, "cell 0 0");
JLabel lblPicture = new JLabel("Picture");
mainPanel.add(lblPicture, "cell 0 1");
thumbnailLabel = new JLabel("thumb");
mainPanel.add(thumbnailLabel, "cell 0 3");
textArea = new JTextArea(10, 10);
mainPanel.add(textArea, "cell 0 4");
btnUploadImages = new JButton("Upload Images");
btnUploadImages.setAction(action);
mainPanel.add(btnUploadImages, "cell 0 6");
}
private class SwingAction extends AbstractAction {
public SwingAction() {
putValue(NAME, "Add checkbox");
putValue(SHORT_DESCRIPTION, "Some short description");
}
@Override
public void actionPerformed(ActionEvent e) {
JCheckBox box = new JCheckBox();
box.setName("Foobar!");
checkboxPanel.add(box);
checkboxPanel.revalidate();
}
}
}