我要创建一个自定义的jdialog,但我希望它有点小。它不应该有空的空间。以下代码告诉我:
如果我使用setSize减小大小,会导致像这样的错误的GUI:
class Find extends JDialog
{
JPanel f1,f2,f3,rp;
JLabel fl,filler1,filler2,filler3;
JTextField ft,fillert;
JCheckBox mcase;
JButton fb1,fb2;
JRadioButton upr,dr;
ButtonGroup rg;
public Find()
{
setTitle("Find");
f3 = new JPanel();
f3.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
f3.setLayout(new GridLayout(3,1));
f1 = new JPanel();
f1.setLayout(new BoxLayout(f1,BoxLayout.X_AXIS));
f2 = new JPanel();
f2.setLayout(new BoxLayout(f2,BoxLayout.X_AXIS));
fl = new JLabel("Find what: ");
ft = new JTextField();
fb1 = new JButton(" Find ");
mcase = new JCheckBox("Match case",false);
fb2 = new JButton("Cancel");
rg = new ButtonGroup();
upr = new JRadioButton("Up");
dr = new JRadioButton("Down");
rg.add(upr);
rg.add(dr);
rp = new JPanel();
rp.add(upr);
rp.add(dr);
filler1 = new JLabel(" ");
filler2 = new JLabel(" ");
f1.add(fl);
f1.add(ft);
f1.add(filler1);
f1.add(fb1);
f2.add(mcase);
f2.add(rp);
//f2.add(filler2);
//f2.add(fb2);
f3.add(f1);
//f3.add(new JLabel());
f3.add(f2);
add(f3);
setSize(400,120);
setAlwaysOnTop(true);
setResizable(false);
setVisible(true);
}
}
答案 0 :(得分:1)
您的 f3 JPanel
有一个GridLayout
有3行,第三行是空的。
你只需要2,所以试试:
f3.setLayout(new GridLayout(2,1));
或者:
将 f3 的LayoutManager更改为垂直BoxLayout
f3.setLayout(new BoxLayout(f3, BoxLayout.Y_AXIS));
并降低JDialog
的高度:
setSize(400,100);
答案 1 :(得分:0)
f3.setLayout(new GridLayout(2,1));
ft.setMaximumSize(new Dimension(250,25));
上述变化就是诀窍!