Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Help on timer

  Asked By: Donna    Date: Mar 06    Category: Java    Views: 687
  

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

}

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Doyle Gonzalez     Answered On: Mar 06

I looked over your code real quick and I saw too many errors to
name. Try simplifying your logical execution to find your logic
errors. (reminds me of a bunch of jumping beans)...

You have two main's one in ScreenSaver and one in DialogBox... your
original frame  is probably building correctly, but then you start
creating objects and i'm thinking you lost a frame somewhere...
probably with the JFrame / DialogBox incapatability issues... try
using JDialog instead

 
Didn't find what you were looking for? Find more on Help on timer Or get search suggestion and latest updates.




Tagged: