所以我有以下屏幕:
这是我的代码:
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并在中心使用JButton,是否需要2个JPanel来分离组件(JLabel和JButtons)?或者有没有办法只使用一个JPanel?
2 - 我想取出我的JButtons中使用的setBounds并使用一些Layout来让我的JButtons在屏幕中间。我怎么能这样做?
答案 0 :(得分:1)
我正在使用BorderLayout。如果我想在北方使用JLabel并在中心使用JButton,是否需要2个JPanel来分离组件(JLabel和JButtons)?或者有没有办法只使用一个JPanel?
是的,你可以使用一个JPanel和一个带有单个列的GridBagLayout和一些Insets来隔离标签上的按钮。
但是,无论您如何调整JFrame的大小,嵌套布局都会将按钮保持在中心位置。
我想取出我的JButtons中使用的setBounds并使用一些Layout来让我的JButtons在屏幕中间像这样。我怎么能这样做?
GridBagLayout将使用insets分隔按钮。
有关使用GridbagLayout的几个对话框示例,请参阅此文章Sudoku Solver Swing GUI。