gpt4 book ai didi

java - 如何在 JPanel 中添加 JButton?

转载 作者:行者123 更新时间:2023-12-01 10:55:59 30 4
gpt4 key购买 nike

我想在此 GUI 的下部区域添加一个 JButton (panButton),但我不知道必须将其添加到哪个元素。我尝试对多个元素执行 panButton.add(element) 但没有任何效果。

我无法理解面板的主要元素是什么。GUI 的显示方式如下: enter image description here这是代码:

    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);
}

我希望它出现在右侧的 Stato 区域或 Dettagli 区域下方。

有人可以帮助我吗?

最佳答案

panButton 是一个 JPanel,它永远不会添加到 GUI 中,因此如果您向其中添加某些内容,它也不会添加到可见的 GUI 中。

您需要将按钮添加到按钮面板,并将按钮面板添加到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)

或者只是使用 panCopy.add(element); 将其添加到“Stato”区域

关于java - 如何在 JPanel 中添加 JButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33606196/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com