【问题标题】:KeyListener does not work when exported导出时 KeyListener 不起作用
【发布时间】:2016-06-14 22:05:17
【问题描述】:

我刚刚编写了一个 Java 程序(代码如下)。它的目的是作为屏幕保护程序。当我在 Eclipse 中运行代码时,一切正常,只要单击一个键或单击鼠标,程序就会关闭。但是当我将其导出为 .jar 文件时,此功能有时会起作用,但大多数时候却不起作用?!这是为什么?

public class fullscreen extends JPanel implements MouseListener, MouseMotionListener, KeyListener {

public fullscreen() {
    addMouseListener(this);
    addMouseMotionListener(this);
    addKeyListener(this);
    setFocusable(true);
}



public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (InstantiationException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IllegalAccessException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (UnsupportedLookAndFeelException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    System.out.println(System.getProperty("java.io.tmpdir"));

    BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);

    // Create a new blank cursor.
    Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
        cursorImg, new Point(0, 0), "blank cursor");



    BorderLayout bL = new BorderLayout();
    String text = new String();

    // create JFrame
    JFrame myframe = new JFrame();
    myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myframe.setUndecorated(true);
    myframe.setResizable(false);
    myframe.setLayout(bL);  
    myframe.setExtendedState(JFrame.MAXIMIZED_BOTH);
    myframe.setBackground(new Color(144,132,118));
    myframe.getContentPane().setBackground(new Color(144,132,118));
    myframe.validate();
    myframe.setVisible(true);
    // Set the blank cursor to the JFrame.
    myframe.getContentPane().setCursor(blankCursor);
    fullscreen fs = new fullscreen();
    fs.setOpaque(false);

    myframe.getContentPane().add(fs);
    GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(myframe);

    //set font and size
    Font myfont = new Font("Space Colony", Font.PLAIN, 40);


    //get temp path and add the customer.txt
    String tempPath = new String(System.getProperty("java.io.tmpdir"));
    String finalPath = tempPath += "customer.txt";

    System.out.println(finalPath);

    //Dateipfad ersetzen mit dem Pfad, wo die txt Datei liegt.
    File aFile = new File(finalPath);


    //create Label and add to JFrame
    JLabel label = new JLabel("Loading Text...");
    label.setForeground(Color.white);
    label.setFont(myfont);
    label.setBackground(Color.black);
    label.setHorizontalAlignment(SwingConstants.CENTER);
    myframe.add(label, BorderLayout.CENTER);


    try{ Scanner scanner = new Scanner(aFile);
    text = scanner.nextLine();
    System.out.println(text);
    label.setText("Herzlich Willkommen " + text + "!");
    scanner.close();
}catch (Exception e){

     label.setText("Herzlich Willkommen!");
}

}

@Override
public void mouseDragged(MouseEvent e) {
    // TODO Auto-generated method stub
    System.out.println("dragged");
}

@Override
public void mouseMoved(MouseEvent e) {
    // TODO Auto-generated method stub



}

@Override
public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub
    String tempPath = new String(System.getProperty("java.io.tmpdir"));
    String finalPath = tempPath += "customer.txt";

    File filledFile = new File(finalPath);
    try {
        PrintWriter writer = new PrintWriter(filledFile);
        writer.print("");
        writer.close();
        System.out.println("geleert");

    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        System.out.print("Hier");
        e1.printStackTrace();
    }
    System.out.println("klick!");
    System.exit(0);
}

@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}



@Override
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    String tempPath = new String(System.getProperty("java.io.tmpdir"));
    String finalPath = tempPath += "customer.txt";


    File filledFile = new File(finalPath);
    try {
        PrintWriter writer = new PrintWriter(filledFile);
        writer.print("");
        writer.close();
        System.out.println("geleert");

    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        System.out.print("Hier");
        e1.printStackTrace();
    }

    System.exit(0);
}



@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

}



@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub
    System.out.println("typed");
}


}

【问题讨论】:

  • 我通常不会在 java 中编写代码,只需要为这个项目编写代码。所以您能否简要解释一下我为什么会遇到这个问题以及可能的解决方案?
  • 您使用的所有语言都使用左对齐代码吗?如果是这样,它们一定难以阅读,就像您当前的代码一样难以阅读。考虑编辑您的帖子,使您的代码符合标准,包括使用缩进、避免过度使用空行等,以便我们阅读和理解它。
  • 对不起,我只是从邮件中复制代码,所以也许这就是格式有点奇怪的原因。我想也许有人也有这个问题,应该是关键监听器的问题,它的注册还是关键的侦听器方法?或者当我导出它时我做错了什么?回到家我会把密码改成原来的
  • 大多数 Swing 专家避免使用 KeyListeners,而是在适当的地方使用键绑定。您可能还有其他问题,因为看起来您正在执行文件 I/O,并且在从 Jar 执行此操作时,您必须注意您是否正确执行。您无法读取 jar 中的文件(您可以读取“资源”),也无法将文件写入 jar,除非您做体操。因此,您最好确保使用相对于用户工作目录或“user.dir”的正确路径。

标签: java swing file export keylistener


【解决方案1】:

我通过更改第一行来让它工作!实际上我不知道为什么这会产生巨大的差异,但它现在完美无缺。 所以现在我有:

public class fullscreen extends JFrame implements MouseListener, MouseMotionListener, KeyListener {

public fullscreen() {
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
setFocusable(true);
}

当然要改变

JFrame myframe = new JFrame();

到:

fullscreen myframe = new fullscreen();

并删除将全屏面板添加到框架中的部分。

【讨论】:

    猜你喜欢
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 2012-01-28
    • 2013-06-14
    • 1970-01-01
    相关资源
    最近更新 更多