A JPanel
有一個JScrollPane
,其中包含另一個JPanel
或兩個。我的生活取決於使用鍵盤的方向箭頭來提高滾動速度。經過仔細考慮後,決定的權力:sc.getVerticalScrollBar().setUnitIncrement(240);
應該只適用於鼠標,以巧妙的方式引發Java開發人員之間的輕微麻煩。有什麼可以做的,以提高滾動速度?我的生活處於平衡狀態。Java - 如何設置JScrollPane的鍵盤滾動速度
2
A
回答
4
必須使用InputMap.put和ActionMap.put的組合來捕獲JScrollPane
中包含的組件的鍵盤事件,並在JScrollPane
有重點時處理鍵盤事件。由於滾動默認增量值是1,你應該添加或。減去所需的增量值的滾動條爲JScrollPane
的電流值,你可以用JScrollPane.getVerticalScrollBar().getValue()
得到與JScrollPane.getVerticalScrollBar().setValue(int).
捕捉事件所包含的元素的例子設置withing JScrollPane中可以使用此代碼來完成,我和按鈕完成,但你明白了吧(很抱歉的代碼的壞組織):
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test
{
public static void main(String[] args)
{
final JFrame f = new JFrame("");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2000,1));
for(int i = 0; i != 2000; i++)
{
JButton btn = new JButton("Button 2");
panel.add(btn);
}
final JScrollPane sPane = new JScrollPane(panel);
final int increment = 5000;
sPane.getVerticalScrollBar().setUnitIncrement(increment);
KeyStroke kUp = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
KeyStroke kDown = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
sPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(kUp,"actionWhenKeyUp");
sPane.getActionMap().put("actionWhenKeyUp",
new AbstractAction("keyUpAction")
{
public void actionPerformed(ActionEvent e)
{
final JScrollBar bar = sPane.getVerticalScrollBar();
int currentValue = bar.getValue();
bar.setValue(currentValue - increment);
}
}
);
sPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(kDown,"actionWhenKeyDown");
sPane.getActionMap().put("actionWhenKeyDown",
new AbstractAction("keyDownAction")
{
public void actionPerformed(ActionEvent e)
{
final JScrollBar bar = sPane.getVerticalScrollBar();
int currentValue = bar.getValue();
bar.setValue(currentValue + increment);
}
}
);
f.add(sPane);
f.pack();
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
f.setVisible(true);
}
}
);
}
}
我們註冊偵聽和處理該事件有:
sPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(kUp,"actionWhenKeyUp");
sPane.getActionMap().put("actionWhenKeyUp",
new AbstractAction("keyUpAction")
{
public void actionPerformed(ActionEvent e)
{
final JScrollBar bar = sPane.getVerticalScrollBar();
int currentValue = bar.getValue();
bar.setValue(currentValue - increment);
}
}
);
執行JScrollBar
遞增值的鍵代碼是AbstractAction的值(在這種情況下,當用戶按下向上鍵時)。
public void actionPerformed(ActionEvent e)
{
final JScrollBar bar = sPane.getVerticalScrollBar();
int currentValue = bar.getValue();
bar.setValue(currentValue - increment);
}
你應該做的是當你的JScrollPane具有焦點時完成事件,但這應該是微不足道的。
希望它有助於挽救你的生命:P或至少爲你服務的起點。
2
可能不是你正在尋找的東西,但你可以使用Mouse Wheel Controller加快使用鼠標時的滾動。
我的生活取決於使用鍵盤的方向箭頭來提高滾動速度。
不確定在使用鍵盤時如何讓滾動窗格滾動。當我使用鍵盤箭頭時,我無法滾動滾動窗格。發佈您的SSCCE,證明問題。
編輯:
對於我的簡單測試,我只是添加一個JLabel到滾動。由於默認情況下JLabel不是可聚焦的,因此滾動窗格中的組件沒有焦點,因此未調用滾動條的默認動作。通過使標籤可以聚焦,鍵盤滾動工作。
0
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.event.MouseInputAdapter;
public class testeSLider extends JFrame {
private JPanel jp;
private JScrollPane sc;
public testeSLider() {
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
setSize(new Dimension(820, 130));
setBackground(Color.BLACK);
jp = new JPanel();
sc = new JScrollPane(jp);
jp.setBackground(Color.GRAY);
sc.setBounds(0, 0, 100, 400);
sc.setBackground(Color.DARK_GRAY);
sc.getHorizontalScrollBar().setPreferredSize(new Dimension(0, 0));
sc.setBounds(50, 30, 300, 50);
getContentPane().add(sc);
int x = 0;
for (int i = 0; i < 50; i++) {
JPanel item = new JPanel();
x = (87 * i) + (i * 10);
item.setBackground(Color.getHSBColor(new Random().nextInt(255),
new Random().nextInt(255), new Random().nextInt(255)));
item.setBounds(x, 5, 0, 0);
item.setPreferredSize(new Dimension(90, 90));
jp.add(item);
addEfeito(item);
}
}
private void addEfeito(JPanel item) {
MouseInputAdapter adapter = new MouseInputAdapter() {
private JPanel panelTmp;
private int deslocamento = 3;
private int mouseStartX;
private int mouseStartY;
@Override
public void mouseDragged(MouseEvent e) {
final JScrollBar bar = sc.getHorizontalScrollBar();
int currentValue = bar.getValue();
bar.setValue(currentValue + (mouseStartX - e.getX()));
}
@Override
public void mouseEntered(MouseEvent e) {
panelTmp = ((JPanel) e.getSource());
panelTmp.setBounds(panelTmp.getX(), panelTmp.getY(),
panelTmp.getWidth() + deslocamento,
panelTmp.getHeight() + deslocamento);
}
@Override
public void mouseExited(MouseEvent e) {
panelTmp = ((JPanel) e.getSource());
panelTmp.setBounds(panelTmp.getX(), panelTmp.getY(),
panelTmp.getWidth() - deslocamento,
panelTmp.getHeight() - deslocamento);
}
@Override
public void mouseClicked(MouseEvent e) {
mouseStartX = e.getX();
mouseStartY = e.getY();
}
@Override
public void mousePressed(MouseEvent e) {
mouseStartX = e.getX();
mouseStartY = e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
}
};
item.addMouseListener(adapter);
item.addMouseMotionListener(adapter);
}
public static void main(String args[]) {
new testeSLider();
}
}
相關問題
- 1. Intellij設置鍵盤滾動速度?
- 2. Jquery JScrollPane - 設置滾動速度/靈敏度
- 3. 如何提高JScrollPane的慢速滾動速度?
- 4. 加速鍵盤滾動
- 5. 如何在IntelliJ中設置鼠標滾輪的滾動速度?
- 6. 設置OwlCarousel2的滾動速度
- 7. JScrollPane中設置滾動位置
- 8. Javascript - 設置和控制滾動速度
- 9. 滾動速度自動滾動速度
- 10. jScrollPane設置高度
- 11. 設置Primeroll滾動面板的滾動速度
- 12. 啓動JScrollPane後設置元素 - JScrollPane不會滾動
- 13. 在JScrollPane上設置滾動條
- 14. jscrollpane,滾動條角度
- 15. 配置tmux滾動速度
- 16. 如何在拖動JScrollPane中的JTree節點時加快滾動速度
- 17. 我該如何設置滾動查看器速度手機7
- 18. 設置鍵盤協調:如何設置鍵盤x,y協調
- 19. Swing JScrollPane - 如何將垂直滾動條設置到左側?
- 20. 如何更改鍵盤滑入/滑出動畫的速度?
- 21. JCombobox鍵盤輸入速度
- 22. JS鍵盤滾動
- 23. 如何設置滾動高度?
- 24. Swing JTabbedPane如何設置滾動寬度?
- 25. 使用jScrollPane回滾滾動位置
- 26. jscrollpane(jquery)中的滾動條高度
- 27. 如何加快滾動條的速度
- 28. 如何計算RecyclerView的滾動速度
- 29. 如何提高UIWebView的滾動速度?
- 30. 如何獲得ListView的滾動速度?
嗯,你提供了一個鏈接到API,但你沒看過,其中列明瞭API:「這個方法現在已廢棄,請使用getActionMap()和getInputMap中()的組合對於類似的」 。但是,使用密鑰綁定是一種方式,所以+1。 – camickr
對不起,在我的答案。 –
修復了不推薦使用的API –