作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在此 GUI 的下部区域添加一个 JButton
(panButton
),但我不知道必须将其添加到哪个元素。我尝试对多个元素执行 panButton.add(element)
但没有任何效果。
我无法理解面板的主要元素是什么。GUI 的显示方式如下: 这是代码:
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/
我是一名优秀的程序员,十分优秀!