简体   繁体   English

使用Timer问题将jLabel更改为特定字符串

[英]Change jLabel to specific string using Timer issue

I have two different scripts, both written in Java. 我有两个不同的脚本,都用Java编写。 The first one is used to check what key the user has pressed(logging). 第一个用于检查用户按下(登录)了什么键。 That script then turns the key into a string and returns in a function. 然后,该脚本将键转换为字符串并returns一个函数。 The second script is then a regular jFrame, which has a label. 然后,第二个脚本是具有标签的常规jFrame。 This jFrame calls the first scripts function to return/get the key logged string. 此jFrame调用第一个脚本函数以返回/获取键记录的字符串。 The label in the jFrame is then set to change to that string every 1 second using a timer. 然后使用计时器将jFrame中的标签设置为每1秒更改为该字符串。 There is just the issue that it doesn't seem like the second script (jFrame) is actually getting the key string, as the label isn't changing when I run the script. 只是有一个问题,似乎第二个脚本(jFrame)实际上并没有获得密钥字符串,因为运行脚本时标签没有改变。 The label just stays blank, with no new information. 标签只是保持空白,没有新信息。 Whenever I run it I have also set it to print the string to check if it gets it, which it doesn't as it wont print it. 每当我运行它时,我也将其设置为打印字符串以检查是否得到它,但不会,因为它不会打印它。 The timer function is working I am sure, as I am printing a line every second in the timer which tells me timer is working . 我确定计时器功能正在工作,因为我每秒在计时器中打印一行,告诉我timer is working I am not completely sure what the reason is to this problem, I am aware of some difficulties when using static variables and the GUI declaration system. 我不完全确定问题的原因是什么,我知道使用静态变量和GUI声明系统时会遇到一些困难。 But I am not sure how to work around it, to make my code work. 但是我不确定如何解决它,以使我的代码正常工作。 I don't get any errors either, which makes it even more difficult. 我也没有任何错误,这使得它变得更加困难。

The first script is the following, it is the one which logs the keystrokes. 第一个脚本如下,它是记录击键的脚本。

import java.io.FileNotFoundException;
import java.io.IOException;
import org.jnativehook.GlobalScreen;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;

public class lauH1 implements NativeKeyListener {

    public static String stringCarry = "";

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

       checkBut(); //call keycheck function

       int x = 0;
       int y = 4;

       PopUpJframe f = new PopUpJframe();

       while (x<y){
       if (f.isShowing()){
       } else {
           f.setVisible(true);
       }
       }

    }

    static void checkBut(){
        try {
            GlobalScreen.registerNativeHook();
        } catch(Exception e) {
            e.printStackTrace();
    }
    GlobalScreen.getInstance().addNativeKeyListener(new lauH1());
    }

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

    @Override
    public void nativeKeyReleased(NativeKeyEvent e) {
    }

    @Override
    public void nativeKeyTyped(NativeKeyEvent e) {
    }

    public static String getString() {
       return stringCarry;
    }

}

The second script is the one below, this is the jFrame which is supposed to get the logged string. 第二个脚本是下面的脚本,这是应该获取记录的字符串的jFrame。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.Timer;

public class lauH2 extends javax.swing.JFrame{

    String stringFK = lauH1.getString(); //this is where I get the string

    Timer timer = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            jLabel2.setText(stringFK);
            System.out.println("Timer works");
            System.out.println(stringFK);
        }
    });

    public lauH2() {
        initComponents();
        setResizable(false);
        timer.start();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")                                                             

    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new lauH2().setVisible(true);
            }
        });  
    }

    // Variables declaration - do not modify                    
    private javax.swing.JLabel jLabel2;
    // End of variables declaration                  
}
String stringFK = lauH1.getString(); //this is where I get the string

Well that only gets the String once when the class is created. 好吧,当创建类时,它只会获取一次String。

You need to invoke the getString() method in the ActionListener of the Timer . 您需要在TimerActionListener中调用getString()方法。

public class lauH1 implements NativeKeyListener 公共类lauH1实现NativeKeyListener

Class names should start with an upper case character! 类名应以大写字母开头! Show me a class in the API that doesn't start with an upper case character. 请向我展示API中不以大写字母开头的类。 Follow Java naming conventions which are easy to learn just by following examples. 遵循Java命名约定,只需遵循以下示例即可轻松学习。

I am aware of some difficulties when using static variables 我知道使用静态变量时会遇到一些困难

Yes, you should not be using a static variable to access another class. 是的,您不应该使用静态变量来访问另一个类。 If you want one class to have access to methods of another class then pass a parameter to the class. 如果希望一个类可以访问另一个类的方法,则将参数传递给该类。

For example: 例如:

LauH1 l1 = new LauH1();
LauH2 l2 = new LauH2(l1);

Then in the constructor of the LauH2 class you save the Lauh1 variable as an instance variable in the class and now any method of the LauH2 class can access the LauH1 class and its methods. 然后,在LauH2类的构造函数中,将Lauh1变量另存为该类中的实例变量,现在LauH2类的任何方法都可以访问LauH1类及其方法。

   while (x<y)
   {
       if (f.isShowing()){
       } else {
           f.setVisible(true);
       }
    }

What is the point of the above code? 上面的代码有什么意义? The values of x/y never change so you just created an infinite loop. x / y的值永远不变,因此您创建了一个无限循环。

public class lauH2 extends javax.swing.JFrame{ 公共类lauH2扩展了javax.swing.JFrame {

An application should only have a single JFrame. 一个应用程序应该只有一个JFrame。 If you need a second window then use a JDialog as the child window to the main frame. 如果需要第二个窗口,请使用JDialog作为主框架的子窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM