2015-10-06 74 views
-1

當VK_ESCAPE被按下並且它首先工作時,我試圖調出密碼對話框。但只要任何JButtons被按下焦點忽略keyListenrs在這裏是我的代碼是否有任何快速修復此。當AcionListener啓動時,keyListener停止工作

package AVAG; 

/** 
* 
* @author allan walls 
*/ 
import java.awt.Color; 
import java.awt.Container; 
import java.awt.GridLayout; 
import java.awt.Image; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 

import javax.swing.BorderFactory; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JTextArea; 
import static org.omg.CORBA.ORB.init; 

public class avagui extends JFrame implements ActionListener, KeyListener { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    JComboBox comboBox; 
    JButton app1, app2, app3, app4; 
    //ImageIcon test1 =new ImageIcon(getClass().getResource("/images/Volunteering.jpg")); 
    //websitelink link = new websitelink(); 
    GridLayout grid = new GridLayout(2, 2, -700, 0); 

    Web1 web1 = new Web1(); 
    Web2 web2 = new Web2(); 
    Web3 web3 = new Web3(); 
    Web4 web4 = new Web4(); 
    JTextArea area = new JTextArea(6, 32); 
     // keyboardgui kg = new keyboardgui(); 

    public avagui() { 

     init(); 
     //setUndecorated(true); 
    } 

    private void init() { 

     Container pane = this.getContentPane(); 
     this.requestFocusInWindow(); 
     pane.addKeyListener(this); 
     pane.setFocusable(true); 
     pane.setLayout(new GridLayout(2, 2, 4, 4)); 
     pane.setBackground(Color.black); 
     ((JComponent) pane).setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); 

     ImageIcon test1 = new ImageIcon(getClass().getResource("/images/Volunteering.jpg")); 
     Image image = test1.getImage(); 

    // reduce by 50% 
     image = image.getScaledInstance((int) (image.getWidth(null)/1.18), (int) (image.getHeight(null)/1.56), Image.SCALE_SMOOTH); 

     test1.setImage(image); 

    // JButton button = new JButton(test1); 
     //pane.setBounds(0, 0, 200, 300); 
     app1 = new JButton(test1); 
     app1.setBackground(Color.BLACK); 

     pane.add(app1); 
     app1.addActionListener(this); 
     ImageIcon test2 = new ImageIcon(getClass().getResource("/images/Aliss.jpg")); 
     Image image2 = test2.getImage(); 
     image2 = image2.getScaledInstance((int) (image2.getWidth(null)/1.18), (int) (image2.getHeight(null)/1.56), Image.SCALE_SMOOTH); 

     test2.setImage(image2); 

     app2 = new JButton(test2); 
     // 
     app2.setBackground(Color.BLACK); 
     app2.addActionListener(this); 
     pane.add(app2); 

     ImageIcon test3 = new ImageIcon(getClass().getResource("/images/jobsearch.jpg")); 
     Image image3 = test3.getImage(); 
     image3 = image3.getScaledInstance((int) (image3.getWidth(null)/1.18), (int) (image3.getHeight(null)/1.56), Image.SCALE_SMOOTH); 

     test3.setImage(image3); 
     app3 = new JButton(test3); 
     app3.setBackground(Color.BLACK); 
     app3.addActionListener(this); 
     pane.add(app3); 

     ImageIcon test4 = new ImageIcon(getClass().getResource("/images/Connect3.jpg")); 
     Image image4 = test4.getImage(); 
     image4 = image4.getScaledInstance((int) (image4.getWidth(null)/1.18), (int) (image4.getHeight(null)/1.56), Image.SCALE_SMOOTH); 

     test4.setImage(image4); 
     app4 = new JButton(test4); 
     app4.setBackground(Color.BLACK); 
     app4.addActionListener(this); 
     pane.add(app4); 

    } 

    public void shutdown() { 
     System.exit(EXIT_ON_CLOSE); 
    } 

    @Override 
    public void keyReleased(KeyEvent e) { 
    } 

    @Override 
    public void keyTyped(KeyEvent e) { 
    } 

    /** 
    * 
    * @param e 
    * @param k 
    */ 
    public void actionPerformed(ActionEvent e) { 
     // TODO Auto-generated method stub 

     if (e.getSource() == app1) { 
      System.out.println("test1"); 
      web1.run(); 
     } 

     if (e.getSource() == app2) { 
      System.out.println("test2"); 
      web2.run(); 
     } 
     if (e.getSource() == app3) { 
      System.out.println("test3"); 
      web3.run(); 
     } 
     if (e.getSource() == app4) { 
      System.out.println("test4"); 
      web4.run(); 
     } 

    } 

    @Override 
    public void keyPressed(KeyEvent e) { 

     PasswordDialog pass = new PasswordDialog(); 
     System.out.println("works"); 
     if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { 

      pass.run(); 

     } 
    } 

} 
+2

你有我們無法測試的代碼,比如Web?類。我們不想看到你所有的代碼,但[mcve]對你和我們都會很好。除此之外,您將希望使用鍵綁定而不是關鍵偵聽器(因爲大多數本網站上類似問題的答案都會告訴您)。 –

+0

無法顯示剩餘的類,因爲它們太大,而且我正在爲組織開發此代碼。對不起 – user3188481

+1

同樣,我明確沒有要求其餘的類,只是你的[mcve]。是的,這將需要你的工作,但它會讓我們能夠回答你的問題。再次嘗試使用Key Bindings,如果這不起作用,那麼用你的[mcve]改善你的問題。 –

回答

4

我想當按下VK_ESCAPE彈出密碼對話框,它工作在第一。但只要任何JButtons被按下焦點忽略密鑰的持有人

確切地說,因爲KeyEvents只調度到焦點組件。所以除非你想把你的轉義邏輯添加到你的所有組件中,否則你不應該使用KeyListener。相反,你應該使用密鑰綁定。

查看Escape Key and Dialog,其中顯示瞭如何通過向對話框的根窗格中添加綁定來按下轉義鍵時關閉對話框的示例。

顯然你的代碼會有所不同,因爲你想顯示一個對話框,但是鍵綁定的概念將是相同的,你只需要創建自定義的Action。

注意:順便說一下,我不確定使用換碼鍵顯示對話框的使用是最好的關鍵筆畫。通常,退出鍵用於關閉對話框,如對話框或打開組合框或彈出式菜單。

+0

感謝使用退出鍵的原因是爲了調出密碼輸入對話框,然後如果輸入正確顯示應用程序頂部的退出欄。它只是一個安全功能,可以阻止ysers遠離應用程序,因爲它將被一般公衆用來搜索作業,並且福利有幫助 – user3188481

+0

@ user3188481,所以問題就回答了。有關逃生鑰匙的說明與答案無關。 – camickr