我有以下创建基本窗口的 java 代码:
JPanel panelCampos, panelBoton;
JLabel labelIdCedula, labelContrasena;
JTextField textFieldIdCedula, textFieldContrasena;
JButton buttonLogin;
panelCampos = new JPanel();
labelIdCedula = new JLabel("ID / Cédula:");
textFieldIdCedula = new JTextField();
labelContrasena = new JLabel("Contraseña:");
textFieldContrasena = new JTextField();
panelBoton = new JPanel();
buttonLogin = new JButton("Iniciar sesión");
setIconImage(Config.ICONO);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(380, 214);
setLayout(new BorderLayout());
setLocationRelativeTo(null);
setResizable(false);
panelCampos.setLayout(new GridLayout(2, 2));
panelCampos.add(labelIdCedula);
panelCampos.add(textFieldIdCedula);
panelCampos.add(labelContrasena);
panelCampos.add(textFieldContrasena);
panelBoton.add(buttonLogin);
add(panelCampos, BorderLayout.CENTER);
add(panelBoton, BorderLayout.SOUTH);
setVisible(true);
结果是:
而且我希望矩阵的每个组件(GridLayout)保持居中,而不是在左侧显示并且大小不同,我该怎么做?
谢谢..