i have try using timer to close my main class after certain
time..there is no error but it just can't seems to work.did i miss
out anything?can anyone help?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.MouseListener;
import java.awt.event.KeyEvent;
import javax.swing.Timer;
public class ScreenSaver extends JFrame implements
MouseListener,KeyListener
{
public final static int ONE_SECOND = 10;
Timer timer;
JFrame frame;
public ScreenSaver()
{
//Get the window size// Create the frame
String title = "Screensaver";
frame = new JFrame(title);
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
// Show the frame according to the user's window size
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
frame.setSize(screenWidth, screenHeight);
//Set background screen to black
Container screen = getContentPane();
screen.setBackground(Color.black);
frame.getContentPane().add(screen);
frame.addMouseListener(this);
frame.addKeyListener(this);
//Set the frame visible
frame.setVisible(true);
//Diable the resizeable button
frame.setResizable(false);
// Set to ignore the cross button
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e)
{
Frame f = new Frame();
String title = "Unlock Screen";
boolean y = true;
dialog d = new dialog(f,title,y);
d.setVisible(true);
timer = new Timer(ONE_SECOND, new
ActionListener()
{
public void actionPerformed
(ActionEvent evt)
{
timer.start();
if(evt.getSource() == timer)
{
frame.setVisible
(false);
frame.dispose();
}
}
});
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void keyPressed(KeyEvent e)
{
Frame f = new Frame();
String title = "Unlock Screen";
boolean y = true;
dialog d = new dialog(f,title,y);
char x;
if((x = e.getKeyChar()) >= '0' && x <= '9')
{
//e.consume();
d.setVisible(true);
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public static void main(String[] args)
{
ScreenSaver screensaver = new ScreenSaver();
}
}