有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java组织JPanel和布局

因此,我有以下屏幕:

Screen

这是我的代码:

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) 个答案

  1. # 1 楼答案

    I'm using the BorderLayout. Do I need to have 2 JPanels to separate components (JLabel and JButtons) if I want to have the JLabel in the North and the JButtons in the Center? Or is there any way to use just one JPanel?

    是的,你可以使用一个JPanel和一个GridBagLayout,其中有一列和一些插图,以便将按钮与标签隔开

    然而,不管你如何调整JFrame的大小,嵌套的布局都会将按钮保持在中间

    I want to take out the setBounds used in my JButtons and use some Layout in order to have my JButtons like that in the middle of the screen. How could I do that?

    GridBagLayout将用插入的按钮隔开

    有关使用GridbagLayout的两个对话框示例,请参阅本文Sudoku Solver Swing GUI