java组织JPanel和布局
因此,我有以下屏幕:
这是我的代码:
setLayout(new BorderLayout());
JLabel lblTitulo = new JLabel("Sistema Generador de Examenes");
lblTitulo.setFont(new Font("Tahoma", Font.BOLD, 18));
JPanel panel1 = new JPanel();
panel1.setBackground(Color.white);
panel1.add(lblTitulo);
add(panel1, BorderLayout.NORTH);
JButton btnCrear = new JButton("Crear Examen");
JButton btnRendir = new JButton("Rendir Examen");
JButton btnCorregir = new JButton("Corregir Examen");
JButton btnVerCorrecciones = new JButton("Ver Correcciones");
btnCrear.setBounds(15, 100, 450, 35);
btnRendir.setBounds(15, 150, 450, 35);
btnCorregir.setBounds(15, 200, 450, 35);
btnVerCorrecciones.setBounds(15, 250, 450, 35);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.white);
panel2.setLayout(null);
panel2.add(btnCrear);
panel2.add(btnRendir);
panel2.add(btnCorregir);
panel2.add(btnVerCorrecciones);
add(panel2, BorderLayout.CENTER);
1-我正在使用BorderLayout。如果我想让JLabel在北方,JButtons在中间,我需要2个JPanel来分离组件(JLabel和JButtons)吗?或者有没有办法只用一个JPanel
2——我想取出我的JButtons中使用的StEnLimes,并使用一些布局,以便在屏幕中间有我的JButtons。我怎么能这么做
# 1 楼答案
是的,你可以使用一个JPanel和一个GridBagLayout,其中有一列和一些插图,以便将按钮与标签隔开
然而,不管你如何调整JFrame的大小,嵌套的布局都会将按钮保持在中间
GridBagLayout将用插入的按钮隔开
有关使用GridbagLayout的两个对话框示例,请参阅本文Sudoku Solver Swing GUI