我有一个使用 SwingLayout 的 Swing 程序。Swing 组件的结构如下所示。
JFrame
JPanel (Cardlayout)
JPanel (Miglayout) - Main panel
Jpanel (Flowlayout) - Checkbox Panel
JPanel (Flowlayout) - Option Panel
我现在的问题是我不确定如何防止复选框面板增长。我不希望它“推出”它在右侧的列。我希望 Wraplayout(在选项面板上工作正常)在复选框面板变得太大时包装它的内容。
这是 Eclipse 内的 Windowsbuilder GUI 的视图。带有“站点”标签的面板是复选框面板。复选框面板的右侧是选项面板。包含它们的大面板是主面板。 http://i.imgur.com/S7Njo.png
当我添加太多复选框时会发生这种情况 http://i.imgur.com/f2SZz.png
我的主要问题是我不明白为什么在主面板中的第一列的列约束上设置“增长 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));
我也尝试过没有流布局,但这并不能解决任何问题。您能提供的任何见解都会很棒!如果人们有问题,我也很乐意提供更多信息。仅供参考,我还尝试在复选框面板所在的单元格的列和行约束上“增长 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();
}
}
}