Thread: Problem in java
View Single Post
  #2 (permalink)  
Old 01-08-2008, 04:10 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import java.util.Calendar; import javax.swing.*; import javax.swing.event.*; public class JPF1 implements ActionListener { static GQ gq; public void actionPerformed(ActionEvent e) { String ac = e.getActionCommand(); System.out.println("ac = " + ac); if(ac.equals("ended")) { System.out.println("time is up - tell GQ instance gq " + "so it can skip to the results..."); // You will have to make arrangements for this in GQ. //gq.stopAndShowResults(); } } public static void main(String[] argv) { final JPF1 appRef = new JPF1(); final JFrame frame = new JFrame("JPassword"); JLabel jlbPassword = new JLabel("Enter the password: "); JPasswordField jpwName = new JPasswordField(10); jpwName.setEchoChar('*'); jpwName.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JPasswordField input = (JPasswordField)e.getSource(); char[] password = input.getPassword(); if (isPasswordCorrect(password)) { JOptionPane.showMessageDialog(frame, "Correct password."); JOptionPane.showMessageDialog(null, "This is a Geography Quiz"); JOptionPane.showMessageDialog(null, "Good Luck"); JOptionPane.showMessageDialog(null, "You got 50 seconds " + "to answer all the questions in each quiz"); gq = new GQ(); gq.setVisible(true); // calling the class for the count down timer MP mp = new MP(); mp.addCountDownListener(appRef); mp.setVisible(true); } else { JOptionPane.showMessageDialog(frame, "Sorry. Try again.", "Error Message", JOptionPane.ERROR_MESSAGE); } } }); JPanel jplContentPane = new JPanel(new BorderLayout()); jplContentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); jplContentPane.add(jlbPassword, BorderLayout.WEST); jplContentPane.add(jpwName, BorderLayout.CENTER); frame.setContentPane(jplContentPane); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.pack(); frame.setVisible(true); } private static boolean isPasswordCorrect(char[] inputPassword) { char[] actualPassword = { 'q', 'u', 'i', 'z' }; if (inputPassword.length != actualPassword.length) return false; //Return false if lengths are unequal for (int i = 0; i < inputPassword.length; i ++) if (inputPassword[i] != actualPassword[i]) return false; return true; } } class MP extends JPanel implements ActionListener { private JLabel label; private CountDown countDown; public MP() { countDown = new CountDown(10); countDown.addActionListener(this); setSize(100, 100); setLayout(new BorderLayout()); add(getLabel(), BorderLayout.CENTER); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("ended")) { label.setText(String.valueOf(countDown.getDelay() - countDown.getTimePassed())); JOptionPane.showMessageDialog(null, "Time Over"); } else { label.setText(String.valueOf(countDown.getDelay() - countDown.getTimePassed())); } } public JLabel getLabel() { if (label == null) { label = new JLabel(); label.setText(String.valueOf(countDown.getDelay())); label.setHorizontalAlignment(JLabel.CENTER); label.setFont(label.getFont().deriveFont(20f)); } return label; } protected void addCountDownListener(ActionListener al) { countDown.addActionListener(al); } public static void main(String[] args) { JFrame frame = new JFrame("CountDown"); MP panel = new MP(); frame.setSize(panel.getSize()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(panel); frame.setVisible(true); } }
Reply With Quote