简体   繁体   中英

How add a JButton in a JPanel?

I want to add a JButton ( panButton ) in the lower area of this GUI, but I don't know to which element I have to add it. I tried to do panButton.add(element) with several elements but nothing works.

I can't understand what is the main element of the panel. This is how the GUI appears: 在此处输入图片说明 While this is the code:

    private void buildGUI() {
    setTitle("Avanzamento upload");
    setIconImage(Toolkit.getDefaultToolkit().getImage(ClientUpload.class.getResource("/clientupload/resources/logoRFI.gif")));

    addWindowListener(new java.awt.event.WindowAdapter() {
        @Override
        public void windowClosing(java.awt.event.WindowEvent evt) {
            formWindowClosing(evt);
        }
    });

    JLabel lblProgressAll = new JLabel("Totale: ");
    JLabel lblProgressCurrent = new JLabel("File attuale: ");
    JLabel lblTotalBytes = new JLabel("MB totali: ");
    JLabel lblCopiedBytes = new JLabel("MB copiati: ");
    lblTotalBytesValue = new JLabel("0 MB");
    lblCopiedBytesValue = new JLabel("0 MB");

    progressButton = new JButton("Interrompi"); //fc

    progressAll = new JProgressBar(0, 100);
    progressAll.setStringPainted(true);
    progressCurrent = new JProgressBar(0, 100);
    progressCurrent.setStringPainted(true);
    txtDetails = new JTextArea(5, 50);
    txtDetails.setEditable(false);
    DefaultCaret caret = (DefaultCaret) txtDetails.getCaret();
    caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
    JScrollPane scrollPane = new JScrollPane(txtDetails, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    JPanel contentPane = (JPanel) getContentPane();
    contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    JPanel panProgressLabels = new JPanel(new BorderLayout(0, 5));
    JPanel panProgressBars = new JPanel(new BorderLayout(0, 5));
    JPanel panCopyLabels = new JPanel(new BorderLayout(0, 5));
    JPanel panCopyFields = new JPanel(new BorderLayout(0, 5));

    panProgressLabels.add(lblProgressAll, BorderLayout.NORTH);
    panProgressLabels.add(lblProgressCurrent, BorderLayout.CENTER);
    panCopyLabels.add(lblTotalBytes, BorderLayout.NORTH);
    panCopyLabels.add(lblCopiedBytes, BorderLayout.CENTER);
    panCopyFields.add(lblTotalBytesValue, BorderLayout.NORTH);
    panCopyFields.add(lblCopiedBytesValue, BorderLayout.CENTER);
    panProgressBars.add(progressAll, BorderLayout.NORTH);
    panProgressBars.add(progressCurrent, BorderLayout.CENTER);

    JPanel panProgress = new JPanel(new BorderLayout(0, 5));
    panProgress.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Avanzamento"), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
    JPanel panCopy = new JPanel(new BorderLayout(0, 5));
    panCopy.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Stato"), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
    JPanel panDetails = new JPanel(new BorderLayout());
    panDetails.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Dettagli"), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
    //fc
    JPanel panButton = new JPanel(new BorderLayout());
    panButton.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(" "), BorderFactory.createEmptyBorder(5, 5, 5, 5)));

    panProgress.add(panProgressLabels, BorderLayout.LINE_START);
    panProgress.add(panProgressBars, BorderLayout.CENTER);
    panCopy.add(panCopyLabels, BorderLayout.LINE_START);
    panCopy.add(panCopyFields, BorderLayout.CENTER);
    panDetails.add(scrollPane, BorderLayout.CENTER);

    JPanel panUpper = new JPanel(new BorderLayout());
    panUpper.add(panProgress, BorderLayout.NORTH);
    panUpper.add(panCopy, BorderLayout.SOUTH);

    contentPane.add(panUpper, BorderLayout.NORTH);
    contentPane.add(panDetails, BorderLayout.CENTER);

    pack();
    setLocationRelativeTo(null);
}

I'd like it appears in the Stato area on the right, or under the Dettagli area.

Can anyone help me?

panButton is a JPanel which is never added to the GUI so if you add something to it, it -also- wont be added to the visible GUI.

You need to add the button to the button panel and add the button panel to the contentPane

JPanel panButton = new JPanel(new BorderLayout());
panButton.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(" "), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
panButton.add(element);  //Add button to panel
...
contentPane.add(panUpper, BorderLayout.NORTH);
contentPane.add(panDetails, BorderLayout.CENTER);
contentPane.add(panButton, BorderLayout.CENTER);  //Add panel to contentPane (which I assume is added to the frame at some point as I can see it's elements on the UI you posted)

or just add it the 'Stato' area with panCopy.add(element);

最后,我创建了一个新的JPanel,并在此添加了按钮。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM