首页 > 解决方案 > 如何将 2 列复选框添加到 Jpanel

问题描述

我想在字符串和文本框下方以及按钮“Aceptar”和“Cancelar”上方的 2 列中添加几个(大约 8 个)复选框

在此处输入图像描述

我试图在另一个 Jpanel 中做一个 Jpanel,但我不工作,因为子面板没有对齐我想要的位置(或者我不知道如何对齐它)

    JLabel texto_titulo_qliksense = new JLabel();
    texto_titulo_qliksense.setText("Nombre del cuadro de mando :");    
    texto_titulo_qliksense.setBounds(20, 50, 700, 25);

    JCheckBox checkBox1 = new JCheckBox("Stream 1");  
    checkBox1.setBounds(75,100, 50,50);  
    JCheckBox checkBox2 = new JCheckBox("Stream 2");  
    checkBox2.setBounds(10,150, 50,50);  

    textField = new JTextField(80);       
    textField.setBounds(200,53,220,20);

    JLabel texto3 = new JLabel(); 
    texto3.setText("Usuario: " + usuario);  

    JButton aceptar = new JButton();
    aceptar.setText("Aceptar");
    aceptar.setActionCommand(comandoAceptar);
    aceptar.setBounds(90, 250, 100, 30); 
    aceptar.addActionListener(this);

    JButton cancelar = new JButton();
    cancelar.setText("Cancelar"); 
    cancelar.setActionCommand(comandoCancelar);
    cancelar.setBounds(250, 250, 100, 30); 
    cancelar.addActionListener(this);

    panel = new JPanel();

    panel.setBorder(null);
    subpanel = new JPanel();
    subpanel.setBorder(BorderFactory.createTitledBorder("Subpanel 1"));
    subpanel.setBounds(250, 450, 250, 250);
    subpanel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
    subpanel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
    subpanel.setVisible(true);
    panel.setBounds(250, 100, 1000, 1000);

标签: javaswinglayout-manager

解决方案


它是一个非常小的程序,使用绝对定位会不会更容易?

比这容易吗?

在此处输入图像描述

JPanel ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(40,4,40,4));

JPanel pageStartPanel = new JPanel(new FlowLayout());
ui.add(pageStartPanel, BorderLayout.PAGE_START);
pageStartPanel.add(new JLabel("Nombre del cuadro de mando:"));
pageStartPanel.add(new JTextField(12));

JPanel checkPanel = new JPanel(new GridLayout(0,2));
ui.add(checkPanel, BorderLayout.CENTER);
for (int ii=1; ii<9; ii++) {
    checkPanel.add(new JCheckBox("Checkbox " + ii));
}
JOptionPane.showConfirmDialog(
        null, 
        ui, 
        "Exportar CdM de Qliksense", 
        JOptionPane.OK_CANCEL_OPTION);

推荐阅读