我在Eclipse Neon上使用Java中的MigLayout构建GUI。我的代码应该提供两行,两列,间隙为15,当窗口扩展时,它会增长以填充框架。我不知道为什么,但是有一个第三行被自动生成,我似乎无法摆脱。我已经将布局硬编码为两行和两列,并尝试使用swing编辑器删除第三行。虽然删除按钮适用于行0和1,删除它们及其内容,但它对最后一行没有任何作用。
下面是我的代码与第三行讨厌的代码运行的图片
这是我的代码
public Master() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 440, 163);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new MigLayout("", "[grow]15[grow]", "[grow]15[grow]"));
btnDailyLog = new JButton("Daily Log");
btnDailyLog.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
contentPane.add(btnDailyLog, "spany 2,grow");
btnEmpReports = new JButton("Employee Reports");
btnJobReports = new JButton("Job Reports");
contentPane.add(btnEmpReports, "grow,wrap");
contentPane.add(btnJobReports, "grow,wrap");
}
答案 0 :(得分:3)
额外的行是由于wrap
中的contentPane.add(btnJobReports, "grow,wrap");
指示移除wrap
来解决问题:
contentPane.add(btnJobReports, "grow");
例如:
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class Tester extends JPanel{
public Tester(){
super(new MigLayout("", "[grow]15[grow]", "[grow]15[grow]"));
JButton btnDailyLog = new JButton("Daily Log");
JButton btnEmpReports = new JButton("Employee Reports");
JButton btnJobReports = new JButton("Job Reports");
add(btnDailyLog,"spany 2,grow");
add(btnEmpReports, "grow,wrap");
add(btnJobReports, "grow");
}
@Override
public Dimension getPreferredSize() {
return new Dimension(440, 163);
}
private static void createAndShowGUI(){
JFrame frame = new JFrame("Tester");
Tester tester = new Tester();
frame.add(tester);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}