在Jar导出后,Keylistener将无法在Jframe中工作

时间:2017-01-05 12:22:12

标签: java swing netbeans actionlistener keylistener

我有两个不同的脚本,它们都是用Java编写的。第一个脚本有一个keylistener,由main函数调用。在该main函数中,我还调用了一个特定的Jframe,它有一个Jlabel,它获取一个包含keylistener返回的变量的变量。整个过程都有效,唯一的问题是每当我在Netbeans中构建和运行时都没有问题,而且它完美无缺。但是,只要我构建完整的Jar并将其自身作为计算机上的常规应用程序运行,Jframe就能正常工作,但它似乎无法获得变量,因为它没有显示它。对我来说有点奇怪的是,当它在Netbeans中运行时它会起作用,但是只要它在外面的地方使用它,它就不起作用。

我的两个脚本可以在下面看到。

带有监听器的脚本1

这是具有keylistener并创建包含keylistener值的变量(字符串)的那个。

public class Stang2 implements NativeKeyListener {

    public static String getSfF = "";
    static File bilDi = new File("file.txt");

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

        if(!bilDi.exists()){
           //do something later
        } else {
            tastCall();
        }

    }

    static void tastCall(){
        botShik();      
    }

    static void botShik(){
        try {
            GlobalScreen.registerNativeHook();
        } catch(Exception e) {
            e.printStackTrace();
    }

    GlobalScreen.getInstance().addNativeKeyListener(new Stang2());
    }

    @Override
    public void nativeKeyPressed(NativeKeyEvent e) {
                getSfF = NativeKeyEvent.getKeyText(e.getKeyCode());
                System.out.println(stringCarry);
    }

    @Override
    public void nativeKeyReleased(NativeKeyEvent e) {

    }

    @Override
    public void nativeKeyTyped(NativeKeyEvent e) {

    }

    public static String pubSen() {
        return getSfF;
    }

}

脚本2,它是实际的Jframe

这是从侦听器记录的密钥获取变量(字符串)的那个。然后将其设置为将其显示为Jlabel,它由计时器每500毫秒更新一次。

public class TingS extends javax.swing.JFrame{

    Timer timer = new Timer(500, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String senTen = Stang2.pubSen();
            jLabel2.setText(senTen);
            System.out.println(senTen);
        }
    });

    public TingS() {
        initComponents();
        timer.start();
    }

}

0 个答案:

没有答案