2017-06-25 147 views
0

我已經創建了一個jframe,並且我添加了一個按鈕,點擊它後,它會要求您按任何按鈕,該按鈕也會顯示在按鈕上。
(它的顯示應該是這樣 - >「點擊我」 - >「按任意按鈕」 - >「空格鍵」)
我的問題1號是,我不想從「點擊我」去按空格鍵「按任意按鈕」。
而我的第二個問題是,當我在「按任意按鈕」時,按下空格鍵,釋放時,它將返回到「按任意按鈕」而不是停留在「空格鍵」處。
這是我的代碼。如何停止空格鍵觸發按鈕上的點擊效果?

public class Test { 

/** 
* @param args the command line arguments 
*/ 

static class starton implements ActionListener { 
    private JButton button; 
    private JFrame frame; 

    public starton(JButton button, JFrame frame) { 
     this.button = button; 
     this.frame = frame; 
    } 

    public void actionPerformed(ActionEvent e) { 
     button.setText("Press A Button"); 
     button.setSize(button.getPreferredSize()); 
     button.addKeyListener(
     new KeyListener() { 
      @Override 
      public void keyPressed(KeyEvent e){ 
       String text = null; 

        char a = e.getKeyChar(); 
        text = ""+a+""; 
        if (a == ' '){ 
         text = "Space Bar"; 
        } 

       button.setText(""+text+""); 
       button.setSize(button.getPreferredSize()); 
       button.removeKeyListener(this); 
      } 

      @Override 
      public void keyTyped(KeyEvent e) { 
      } 

      @Override 
      public void keyReleased(KeyEvent ke) { 
      } 
     }); 
    } 
} 

public static void main(String[] args) throws IOException { 

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    double width = screenSize.getWidth(); 
    double height = screenSize.getHeight(); 
    int frame1w = 600; 
    int frame1h = 400; 

    JFrame frame1 = new JFrame("Foo"); 
    frame1.setSize(frame1w, frame1h); 
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JPanel contentPane = new JPanel(); 
    contentPane.setBackground(Color.WHITE); 
    contentPane.setLayout(null); 
    frame1.setContentPane(contentPane); 

    JButton button1 = new JButton("Click Me"); 
     button1.setSize(button1.getPreferredSize()); 
     button1.addActionListener(new starton(button1, frame1)); 
      // add more code here 
     contentPane.add(button1); 
     frame1.setVisible(true); 
} 

}

回答

0

這個問題是關於因你的動作監聽內KeyListener的嵌套控制。由於空格鍵是默認的鍵盤按鈕,相當於單擊按鈕,每次點擊時空格鍵actionPerformed按鈕方法ActionListener被觸發。如果您發現ActionEventactionCommand已被觸發並打印出來,您可以看到發生這種情況。您也可以使用它來控制執行的代碼。就像這樣:

public void actionPerformed(ActionEvent e) 
{ 
    System.out.println("Button Action: " + e.getActionCommand()); 
    if(e.getActionCommand().equals("Click Me")) 
    { 
     //Your existing code but remove this line 
     //button.removeKeyListener(this); 
    } 
} 
2

JButton安裝了一系列的鍵綁定來控制用戶輸入

您可以檢查一下這些正在使用類似...

JButton btn = new JButton("Test"); 
InputMap im = btn.getInputMap(); 
for (KeyStroke ik : im.allKeys()) { 
    System.out.println(ik + " = " + im.get(ik)); 
} 

在我的系統,它打印

pressed SPACE = pressed 
released SPACE = released 

這告訴我,空間鍵綁定到操作鍵pressedreleased

爲了禁用這些鍵,您需要提供自己的綁定...

Action blankAction = new AbstractAction() { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
    } 
}; 

ActionMap am = btn.getActionMap(); 
am.put("pressed", blankAction); 
am.put("released", blankAction); 

這只是取代了pressedreleased綁定一個空Action它什麼都不做。

現在,一個警告詞 - 鍵綁定可以在不同的平臺/外觀和感覺上有所不同;您還應該注意用戶通常對某些控件可以或不可以做的事情有預先定義的期望,更改它們會影響他們對您的反應程序

至於您的第二個問題,button仍在使用同樣ActionListener您最初註冊的,因此當你按下空間,它再次引發了ActionListener,並增加了新的KeyListener這是會加劇你的問題

要麼你要使用兩個按鈕單獨ActionListener或者你想在第一次觸發時從按鈕中刪除ActionListener - 我會去第二次,這很容易o瞭解代碼

不,我的意思是,如果我禁用「按」空格鍵,我可以按下它時,我在「按任意按鈕」?

的最簡單的解決方案是使用兩個不同的按鈕,一個與ActionListener裏面設置了另一個按鈕,它有一個KeyListener連接到它。

如果由於某種原因,你「真的」不想這樣做,那麼你就需要從按鈕移除ActionListener時,它首先觸發

static class starton implements ActionListener { 

    //... 

    public void actionPerformed(ActionEvent e) { 
     button.setText("Press A Button"); 
     button.setSize(button.getPreferredSize()); 
     button.addKeyListener(...); 
     button.removeActionListener(this); 
    } 
} 
+0

有關first1,我爲什麼要禁用「按」空格鍵?這不應該是整個點?我的意思是按空格鍵?我也沒有多功能按鈕,它只是1 ..也就第二個問題,我試圖取消成功的動作聽衆,但我想能夠做例行無數次,因爲我想,不只是一次 –

+0

它把你的整個問題作爲*「如何停止空格鍵觸發按鈕點擊效果?*」和*「我不想從」點擊我「去按」空格鍵按任意按鈕「。」*作爲含義,您想要禁用該按鈕的[空格]欄功能。 *「我也沒有多功能按鈕,它只是1」* - 是的,這就是你的「核心」問題; – MadProgrammer

+0

*「我試圖取消成功的動作監聽,但我希望能夠按照我想做的次數做多次,而不僅僅是一次」*和?再次,兩個按鈕將有助於解決您遇到的大部分問題。你可以簡單地將它們交換到適當的位置。因爲你同時在按鈕上註冊了一個KeyListener和一個ActionListener,所以每當你按下[Space]時,它們都會被觸發,在我看來,你只是要求解決問題,通過使用兩個(或更多)按鈕,每個按鈕都有自己的目標功能 – MadProgrammer