请教一个关于JAVA GridLayout布局的问题。
我现在用eclipse写一个用户登录界面,我将布局方式改为3行2列的GridLayout布局,发现在网格中的组件都是充满了整个网格,使得界面不太美观。
相关代码如下:
private JLabel user_jlb =new JLabel("用户名");
private JLabel pwd_jlb =new JLabel("密 码");
private JTextField user_tf =new JTextField(15);
private JPasswordField pwd_pf =new JPasswordField(15);
private JButton ok =new JButton("确空");
private JButton exit =new JButton("退出");
//设置的布局方式为GridLayout。
jpane2.setLayout(new GridLayout(3,2));
jpane2.add(user_jlb);
jpane2.add(user_tf);
jpane2.add(pwd_jlb);
jpane2.add(pwd_pf);
jpane2.add(ok);
jpane2.add(exit);
jframe.add(jpane2);
运行之后才发现,我所填加的组件是填充满了整个网格,我试着改了一下组件的大小,却无效。高手们都回复一下,这个问题应该怎么去解决。
我现在用eclipse写一个用户登录界面,我将布局方式改为3行2列的GridLayout布局,发现在网格中的组件都是充满了整个网格,使得界面不太美观。
相关代码如下:
private JLabel user_jlb =new JLabel("用户名");
private JLabel pwd_jlb =new JLabel("密 码");
private JTextField user_tf =new JTextField(15);
private JPasswordField pwd_pf =new JPasswordField(15);
private JButton ok =new JButton("确空");
private JButton exit =new JButton("退出");
//设置的布局方式为GridLayout。
jpane2.setLayout(new GridLayout(3,2));
jpane2.add(user_jlb);
jpane2.add(user_tf);
jpane2.add(pwd_jlb);
jpane2.add(pwd_pf);
jpane2.add(ok);
jpane2.add(exit);
jframe.add(jpane2);
运行之后才发现,我所填加的组件是填充满了整个网格,我试着改了一下组件的大小,却无效。高手们都回复一下,这个问题应该怎么去解决。
后面两个参数分别为水平间隙和垂直间隙。
1。在放JButton的那个格子,先把JButton放到JPanel pnlBTN上,pnlBTN采用FlowLayout,再把pnlBTN放到jpane2上之前该JButton的位置。
2。采用GridBagLayout,网格包布局。
jpanel_ok.setLayout(new FlowLayout());
jpanel_exit.setLayout(new FlowLayout());
jpanel_ok.add(ok);
jpanel_exit.add(exit);
jpane2.add(jpanel_ok);
jpane2.add(jpanel_exit);
我试着将确定按钮右看齐,是这样写的,结果错错:
jpanel_ok.add(ok,FlowLayout.RIGHT);
jpanel_exit.add(exit,FLowLayout.LEFT);
new FlowLayout(FlowLayout.RIGHT)
哈哈,现在变的差不多了。我现在想把按钮的大小再加大一点,我用
ok.setSize();方法,结果发现,按钮的大小并没有改变。请问这个问题怎么解决。现在界面如下: