2016-06-14 84 views
0

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

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"); 
} 


} 
+1

[Oracle Java代码约定](http://www.oracle.com/technetwork/java/codeconvtoc-136057.html) –

+0

我通常不用java代码,只是为了这个项目..所以可以你可能会给我一个简短的解释,为什么我有这个问题,也许是一个解决方案? –

+0

您使用的所有语言都使用左对齐码吗?如果是这样,他们必须难以阅读,就像您当前的代码要读取一样困难。考虑编辑你的帖子,使你的代码符合标准,包括使用缩进,避免过度使用空行等,以便我们可以阅读和理解它。 –

回答

0

我通过改变第一行来得到它的工作!其实我不知道为什么这会产生巨大的差异,但它现在完美地运作。 所以现在我有:

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

,并删除其中的全屏是一个面板添加到帧的部分。