1
我在JFrame內的JPanel內的JScrollPane中的JEditorPane中顯示一個html文件。如何添加偵聽器來檢測「Page Down」和「Page Up」事件?JScrollBar中的頁面向上/向下事件
這裏是我的代碼:
public class Test_Url extends JPanel {
JEditorPane editor;
JScrollPane scroller;
String source = "test.html";
Test_Url(){
super();
setLayout(new BorderLayout());
editor = new JEditorPane();
editor.setEditable(false);
File file = new File(source);
String page = "file:///" + file.getAbsolutePath();
try {
editor.setPage(page);
} catch (IOException ex) {
Logger.getLogger(Test_Url.class.getName()).log(Level.SEVERE, null, ex);
}
scroller = new JScrollPane(editor, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroller.setPreferredSize(new Dimension(500, 600));
add(scroller, BorderLayout.CENTER);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Test_Url());
frame.pack();
frame.setVisible(true);
}
}
謝謝!
AdjustmentEvent參數有一個int類型字段,其中一個是BLOCK_INCREMENT,所以它看起來很可能會告訴你。 1+ –
@HovercraftFullOfEels,好點。我應該看看API更接近:) – camickr
謝謝你們倆。不幸的是,無論用戶的操作如何,「AdjustmentEvent」類型總是顯示爲「TRACK」。無論如何,我可以通過垂直滾動條位置的變化來檢測動作。 Page Up/Down只生成一個事件。 – Serg