Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Jeff Cunningham   on Mar 22 In Java Category.

  
Question Answered By: Haya Yoshida   on Mar 22

this is my dialog  class

import java.awt.event.*;
import java.awt.*;


public class DialogBox
{

public static void main(String args[])
{
Frame f;
new dialog(f = new Frame(),"Unlock Screen",true);
f.dispose();

}
}

class dialog extends Dialog implements ActionListener
{

Button ok = new Button("OK");

dialog(Frame parent, String title, boolean modal)
{
super(parent,title,modal);

ok.addActionListener(this);

add(BorderLayout.NORTH,new Label("Do you want to
unlock?"));
add(BorderLayout.SOUTH,ok);

/* Size the frame  */
pack();

/* Center the dialog */
Dimension screenDim =
Toolkit.getDefaultToolkit().getScreenSize();
Rectangle frameDim = getBounds();
setLocation((screenDim.width - frameDim.width)
/ 2,(screenDim.height - frameDim.height) / 2);

/* show  the dialog */
setVisible(true);
}

public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() == ok)
setVisible(false);//dialog box  is dispose

//show progressbar when button is click
DetectingBar bar = new DetectingBar();
bar.show();
bar.pack();
}
}

this is my detecting class(which is use in dialog class)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DetectingBar extends JFrame {
public final static int ONE_SECOND = 1000;

private JProgressBar progressBar;
private Timer timer;
private LongTask task;


public DetectingBar() {
super("Detecting");
setSize(40,20);
task = new LongTask();

progressBar = new JProgressBar(0, task.getLengthOfTask());
progressBar.setValue(0);
progressBar.setStringPainted(true);

JPanel panel = new JPanel();
panel.add(progressBar);

JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.add(panel, BorderLayout.NORTH);
contentPane.setBorder(BorderFactory.createEmptyBorder(20, 20,
20, 20));
setContentPane(contentPane);

/* Center the dialog */
Dimension screenDim =
Toolkit.getDefaultToolkit().getScreenSize();
Rectangle frameDim = getBounds();
setLocation((screenDim.width -
frameDim.width)
/ 2,(screenDim.height -
frameDim.height) / 2);

//Create a timer.
timer = new Timer(ONE_SECOND, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
progressBar.setValue(task.getCurrent());

if (task.done())
{
Toolkit.getDefaultToolkit().beep();
timer.stop();

progressBar.setValue(progressBar.getMinimum());

dispose();
}
}
});
task.go();
timer.start();
}

and lastly this is my main class(dialog and detecting class will
appear)

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.MouseListener;
import java.awt.event.KeyEvent;


public class ScreenSaver extends JFrame implements
MouseListener,KeyListener
{
//public final static int ONE_SECOND = 1000;
//Timer timer;

public ScreenSaver()
{
//Get the window size// Create the frame
String title = "Screensaver";
JFrame 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);

}

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')
{

d.setVisible(true);
}
}

public void keyTyped(KeyEvent e){}

public void keyReleased(KeyEvent e){}

public static void main(String[] args)
{

ScreenSaver screensaver = new ScreenSaver();
}

}

Share: 

 

This Question has 2 more answer(s). View Complete Question Thread

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


Tagged: