【问题标题】:keyListener stops working when AcionListener starts当 AcionListener 启动时 keyListener 停止工作
【发布时间】:2016-01-03 14:46:55
【问题描述】:

我试图在按下 VK_ESCAPE 时调出密码对话框,并且它首先可以工作。但是,只要按下任何 JButton,焦点就会忽略 keyListenrs,这是我的代码,是否有任何快速修复。

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

        }
    }

}

【问题讨论】:

  • 您有我们无法测试的代码,例如 Web?类。我们不想看到您的所有代码,但minimal reproducible example 对您和我们都非常有用。除此之外,您将希望使用键绑定而不是键侦听器(因为本网站上类似问题的大多数答案都会告诉您)。
  • 无法显示其余的类,因为它们太大了,而且我正在为一个组织开发此代码,抱歉
  • 再一次,我明确地没有要求其余的课程,只是你的minimal reproducible example。是的,这需要您的工作,但它将使我们能够回答您的问题。再次尝试使用 Key Bindings,如果这不起作用,请使用 minimal reproducible example 改进您的问题。

标签: java swing jbutton keylistener


【解决方案1】:

我试图在按下 VK_ESCAPE 时调出密码对话框,它一开始就可以工作。但是只要按下任何 JButtons,焦点就会忽略 keyListenrs

没错,因为 KeyEvents 只会分派给有焦点的组件。因此,除非您想将转义逻辑添加到所有组件中,否则您不应该使用 KeyListener。相反,您应该使用键绑定。

查看Escape Key and Dialog 的示例,该示例显示了如何通过向对话框的根窗格添加绑定来在按下转义键时关闭对话框。

显然你的代码会有所不同,因为你想显示一个对话框,但是键绑定的概念是一样的,你只需要创建自定义动作。

注意:顺便说一下,我不确定您是否使用转义键来显示对话框是最好的击键方式。通常,转义键用于关闭事物,例如对话框或打开组合框或弹出菜单。

【讨论】:

  • 感谢我使用转义键的原因是打开密码输入对话框,如果输入正确,则会在应用程序顶部显示退出栏。它只是一个阻止 ysers 离开应用程序的安全功能,因为它将被公众使用它来寻找工作和福利帮助
  • @user3188481,问题已得到解答。关于转义键的注释与答案并不真正相关。
猜你喜欢
  • 2017-07-28
  • 2011-11-24
  • 1970-01-01
  • 1970-01-01
  • 2017-03-15
  • 2020-05-30
  • 1970-01-01
  • 2012-01-11
  • 2021-01-14
相关资源
最近更新 更多