2011-07-06 66 views
2

A JPanel有一個JScrollPane,其中包含另一個JPanel或兩個。我的生活取決於使用鍵盤的方向箭頭來提高滾動速度。經過仔細考慮後,決定的權力:sc.getVerticalScrollBar().setUnitIncrement(240);應該只適用於鼠標,以巧妙的方式引發Java開發人員之間的輕微麻煩。有什麼可以做的,以提高滾動速度?我的生活處於平衡狀態。Java - 如何設置JScrollPane的鍵盤滾動速度

回答

4

必須使用InputMap.putActionMap.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或至少爲你服務的起點。

+0

嗯,你提供了一個鏈接到API,但你沒看過,其中列明瞭API:「這個方法現在已廢棄,請使用getActionMap()和getInputMap中()的組合對於類似的」 。但是,使用密鑰綁定是一種方式,所以+1。 – camickr

+0

對不起,在我的答案。 –

+0

修復了不推薦使用的API –

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(); 
    } 

}