I just coded a Java program (code down below). It's meant to be a screensaver. When I run the code in eclipse everything works perfectly, as soon as a key is clicked or the mouse is clicked the program gets closed. But when I export it as a .jar file, this function sometimes works, but most of the time it doesn't?! Why is that?
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");
}
}
Aucun commentaire:
Enregistrer un commentaire