【发布时间】:2018-05-16 21:28:05
【问题描述】:
我们有一个JScrollpane 和一个带有按钮的JPanel。
一开始不会显示任何按钮。
那时我们的 GUI 总是会被销毁。我们怎样才能解决这个问题?
我们希望显示空的JScrollPane。
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridBagLayout());
buttonPanel.setPreferredSize(new Dimension(450, 500));
buttonPanel.setMinimumSize(new Dimension(450, 500));
buttonPanel.setBackground(Color.WHITE);
JScrollPane pane = new JScrollPane();
pane.setSize(new Dimension(450, 500));
pane.setMinimumSize(new Dimension(450, 500));
// GridBagConstraint for button
GridBagConstraints constraint = new GridBagConstraints();
constraint.anchor = GridBagConstraints.WEST;
constraint.fill = GridBagConstraints.HORIZONTAL;
constraint.gridx = 0;
constraint.gridy = GridBagConstraints.RELATIVE;
constraint.weightx = 1.0f;
constraint.weighty = 1.0f;
for (int i = 0; i < countVeranstaltungen; i++) {
JButton button = new JButton();
button.setText(events[i].getTitel() + " " + events[i].getTreffpunkt() + " " + events[i].getTermin());
button.setPreferredSize(new Dimension(40, 100));
buttonPanel.add(button, constraint);
Veranstaltungsdetails details = new Veranstaltungsdetails(events[i]);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
details.setVisible(true);
}
});
}
pane.setViewportView(buttonPanel);
this.jPanel3.add(pane);
pane.updateUI();
【问题讨论】:
-
如果为空,则不需要显示。请张贴您希望在没有创建按钮时显示的图片和正确的minimal reproducible example
-
ScrollPane 不是空的,buttonPanel 是。无法显示空的 JPanel。当没有按钮时,您应该有一个默认图像来显示。
-
谢谢!!但问题只是在 catch 块末尾缺少一个括号。
标签: java swing jscrollpane