2014-08-29 65 views

回答

2

另外,有沒有一種方法來設置FocusTraversalPolicyJComponent

是的。看看How to Use the Focus Subsystem。有一個部分Customizing Focus Traversal,可以解釋如何做到這一點,完整的示例FocusTraversalDemo。它基本上創建一個擴展FocusTraversalPolicy一個類中,它會覆蓋一些方法來確定組件遍歷


UPDATE

這裏的排序是使用RandomFocusTraversalPolicy這正是它的名字爲例提示。遍歷是隨機的。

enter image description here

import java.awt.Color; 
import java.awt.Component; 
import java.awt.Container; 
import java.awt.FocusTraversalPolicy; 
import java.awt.GridLayout; 
import java.awt.event.FocusEvent; 
import java.awt.event.FocusListener; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 

public class FocusTraversalDemo extends JComponent { 

    List<Component> components = new ArrayList<Component>(); 

    public FocusTraversalDemo() { 
     setFocusTraversalPolicyProvider(true); 
     setLayout(new GridLayout(5, 5, 10, 10)); 
     initComponents(); 
     setFocusTraversalPolicy(new RandomFocusTraversalPolicy(components)); 
    } 

    private void initComponents() { 
     for (int i = 0; i < 25; i++) { 
      final JButton button = new JButton("Button " + i); 
      final Color defaultColor = button.getForeground(); 
      button.addFocusListener(new FocusListener(){ 
       @Override 
       public void focusGained(FocusEvent e) { 
        button.setForeground(Color.BLUE); 
       } 
       @Override 
       public void focusLost(FocusEvent e) { 
        button.setForeground(defaultColor); 
       } 
      }); 
      this.add(button); 
      components.add(button); 
     } 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable(){ 
      public void run() { 
       JFrame frame = new JFrame(); 
       frame.add(new FocusTraversalDemo()); 
       frame.pack(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class RandomFocusTraversalPolicy extends FocusTraversalPolicy { 

     private List<Component> comps; 

     public RandomFocusTraversalPolicy(List<Component> comps) { 
      this.comps = comps; 
     } 

     @Override 
     public Component getComponentAfter(Container aContainer, Component aComponent) { 
      return comps.get(getRandomIndex()); 

     } 

     @Override 
     public Component getComponentBefore(Container aContainer, Component aComponent) { 
      return comps.get(getRandomIndex()); 
     } 

     @Override 
     public Component getFirstComponent(Container aContainer) { 
      return comps.get(0); 
     } 

     @Override 
     public Component getLastComponent(Container aContainer) { 
      return comps.get(comps.size() - 1); 
     } 

     @Override 
     public Component getDefaultComponent(Container aContainer) { 
      return comps.get(0); 
     } 

     private int getRandomIndex() { 
      Random random = new Random(); 
      return random.nextInt(comps.size()); 
     } 
    } 
} 
+0

的感謝!但你提到的例子使用jpanel而不是jcomponent,我檢查我的組件,它不是一個遍歷提供程序 – user2628641 2014-09-03 13:40:58

+0

它擴展JComponent(它是Container的子類),但isFocusTraversalPolicyProvider()返回false – user2628641 2014-09-03 14:15:15

+0

然後只需將其設置爲true。 'setFocusTraversalPolicyProvider(true)' – 2014-09-03 14:34:51