2014-05-01 25 views
2

我在執行該程序的最後一部分時遇到了問題,即在按下Enter鍵的同時按順序(1,2,3,4)將活動字段從一個文本字段移動到下一個字段。如何使用不同的熱鍵添加焦點遍歷?

這需要在不刪除選項卡所使用的焦點循環的情況下完成。

我不知道如何開始這樣做,我在API中發現的唯一的東西是用你自己的替換遍歷策略,但我不想替換它我想創建一個新的與默認的並行運行。

public class unit27 { 

    JButton button1 = new JButton("add to next cup"); 
    JButton button2 = new JButton("add to next cup"); 
    JButton button3 = new JButton("add to next cup"); 
    JButton button4 = new JButton("add to next cup"); 

    JTextField text1 = new JTextField("4"); 
    JTextField text2 = new JTextField("4"); 
    JTextField text3 = new JTextField("4"); 
    JTextField text4 = new JTextField("4"); 

    JLabel label1 = new JLabel("Cup 1"); 
    JLabel label2 = new JLabel("Cup 2"); 
    JLabel label3 = new JLabel("Cup 3"); 
    JLabel label4 = new JLabel("Cup 4"); 

    JFrame frame = new JFrame(); 
    JPanel mainPanel = new JPanel(); 
    JPanel panel1 = new JPanel(); 
    JPanel panel2 = new JPanel(); 
    JPanel panel3 = new JPanel(); 
    JPanel panel4 = new JPanel(); 

    public unit27() { 

     mainPanel.setLayout(new GridLayout(2, 4)); 
     panel1.setLayout(new GridLayout(1, 3)); 
     panel2.setLayout(new GridLayout(1, 3)); 
     panel3.setLayout(new GridLayout(1, 3)); 
     panel4.setLayout(new GridLayout(1, 3)); 
     frame.add(mainPanel); 
     frame.setSize(800, 800); 
     frame.setVisible(true); 
     mainPanel.add(panel1); 
     mainPanel.add(panel2); 
     mainPanel.add(panel3); 
     mainPanel.add(panel4); 

     panel1.add(label1); 
     panel1.add(button1); 
     panel1.add(text1); 

     panel2.add(label2); 
     panel2.add(button2); 
     panel2.add(text2); 

     panel3.add(label3); 
     panel3.add(button3); 
     panel3.add(text3); 

     panel4.add(label4); 
     panel4.add(button4); 
     panel4.add(text4); 

     button1.add(text1); 
     button1.addActionListener(new MyListener1()); 
     button2.addActionListener(new MyListener2()); 
     button3.addActionListener(new MyListener3()); 
     button4.addActionListener(new MyListener4()); 

     // end class 

    } 

    class MyListener1 implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 

      int a = Integer.parseInt(text1.getText()); 
      int b = Integer.parseInt(text2.getText()); 

      int c = a + b; 

      text2.setText(Integer.toString(c)); 

     } 
    } 

    class MyListener2 implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 

      int a = Integer.parseInt(text2.getText()); 
      int b = Integer.parseInt(text3.getText()); 

      int c = a + b; 

      text3.setText(Integer.toString(c)); 

     } 
    } 

    class MyListener3 implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 

      int a = Integer.parseInt(text3.getText()); 
      int b = Integer.parseInt(text4.getText()); 

      int c = a + b; 

      text4.setText(Integer.toString(c)); 

     } 
    } 

    class MyListener4 implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 

      int a = Integer.parseInt(text4.getText()); 
      int b = Integer.parseInt(text1.getText()); 

      int c = a + b; 

      text1.setText(Integer.toString(c)); 

     } 
    } 

    public static void main(String[] args) { 

     new unit27(); 
    } 

} 

回答

0

擺動不允許同時運行2個聚焦週期。如果你真的需要第二個焦點週期(而不是僅僅添加焦點遍歷鍵),那麼你可以做的是:

創建一個確定你的週期的地圖。
在帶有額外週期的每個組件上,爲Enter添加一個鍵綁定,以獲取週期中的下一個可見字段並調用requestFocus。

您將不得不維護地圖並創建自定義檢查以查看下一個組件是否可見或應該跳過。