【发布时间】:2014-08-20 05:18:38
【问题描述】:
我也使面板实现了可滚动,因此它可以轻松滚动它仍然无法正常工作。
这是一张图,中间是循环面板的jscrollpane:
所以当我尝试向下滚动时,它只显示组件滞后并且下面的组件不加载,我在单击按钮时使用 system.out.println() 对此进行了测试,最后一个按钮给出了相同的数字5 表示相同的数字。
这是我的滚动窗格类:
public BackupsStage(){
setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
getViewport().setLayout(new GridBagLayout());
setViewportBorder(new LineBorder(Color.RED));
panel = new BackupPanel();
getViewport().add(panel);
setBounds(5, 125, 985, 280);
setVisible(false);
current = this;
}
这是我的面板类:
private class BackupPanel extends JPanel implements Scrollable{
public BackupPanel() {
setLayout(new GridLayout(0, 1));
}
@Override
public Dimension getPreferredScrollableViewportSize() {
return super.getPreferredSize();
}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return 100;
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return 16;
}
@Override
public boolean getScrollableTracksViewportWidth() {
return true;
}
@Override
public boolean getScrollableTracksViewportHeight() {
return true;
}
}
这是我用来添加到面板的代码:
public static void loadBackupOntoStage(String username,int hour,int minute, int backupNum){
JButton revert = new JButton("Revert " + username + " to this backup");
revert.setFont(new Font("Tahoma", Font.BOLD, 9));
revert.setFocusPainted(false);
// revert.setVisible(false);
panel.add(revert);
revert_buttons.put(String.valueOf(backupNum - 1),revert);
JLabel label = new JLabel("Backup created " + hour + " hours and " + minute + " minutes ago");
label.setFont(new Font("Tahoma", Font.BOLD, 14));
label.setForeground(Color.DARK_GRAY);
panel.add(label);
// label.setVisible(false);
backups.put(String.valueOf(backupNum - 1),label);
JSeparator hsep = new JSeparator(JSeparator.HORIZONTAL);
// hsep.setVisible(false);
panel.add(hsep);
current.setVisible(true);
revert.addActionListener(e -> {
System.out.print(backupNum - 1);
String answer = JOptionPane.showInputDialog(
FullMode.current
, "Please type REVERT to confirm reverting " + username + " to this backup.\nThere is no going back after this."
, "Reverting"
, JOptionPane.WARNING_MESSAGE
);
if (answer.equalsIgnoreCase("revert")) {
// todo
}
});
}
那我做错了什么?
哦,也许您应该知道滚动窗格是由 setBounds 使用空布局设置在其位置的。
【问题讨论】:
标签: java swing layout jpanel jscrollpane