-
login problem
Hi everyone.I'm basically new to java but with the help of this forum i hope to get better.I'm currently busy with a user login interface project(see code below) and I'm a bit stuck now and i need help.
PROBLEM STATEMENT
When the login button(submit) is clicked i want the program to connect to a db(which i have already acomplished),and after authentication i want it to dispose the full screen frame,set timerframe visible and launch the count down timer.when the counter reaches zero,the timerframe must dispose and the the full screen frame set visible.
I also want to block out any special keys (e.g ctrl-alt-del , alt F4 ,alt Esc etc)
i'm using the j2sdk1.4.2_04 version.
P.s Feel free to alter or add anything to the existing code.
coments will be appreciated
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*; //Header files used
import java.lang.ClassNotFoundException;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.sql.*;
import javax.swing.*;
public class real implements Runnable
{
JFrame timerframe;
JFrame frame;
JLabel label;
public real(JFrame timerframe, JLabel label, JFrame frame)
{
this.timerframe = timerframe;
this.label = label;
this.frame = frame;
}
//Entry class
public static void main(String args[])
{
JFrame timerframe = new JFrame(); //Create a new JFrame called timerframe;
JTextField username = new JTextField(10); //Create a JTextField called username
JFrame frame = new JFrame("Login"); //Create a new JFrame called frame;
JPasswordField password = new JPasswordField(8); //Create new JPasswordField - password;
JLabel label = new JLabel(); //Create a JLabel called label
JLabel labname = new JLabel( "Enter User Name: "); //Create a JLabel called labname
JLabel labpass = new JLabel( "Enter Password: " ); //Create a JLabel called labpass
JLabel emptyLabel1 = new JLabel(" "); //Create an Empty Label number 1
JLabel emptyLabel2 = new JLabel(" "); //Create an Empty Label number 2
JButton submit = new JButton("Login"); //Created a JButton called submit
JButton exit = new JButton("Exit"); //Created a JButton called exit
label.setBackground(Color.WHITE); //Sets the background to white
label.setForeground(Color.BLACK); //Sets the foreground to black
label.setOpaque(true);
label.setHorizontalAlignment(SwingConstants.CENTER);
username.setFont(new Font("Serif", Font.BOLD, 18)); //Set the username font type
password.setFont(new Font("Serif", Font.BOLD, 24)); //Set the password font type
timerframe.setUndecorated(true);
timerframe.setSize(185, 19); //Sets the size of the JFrame
timerframe.getContentPane().add(label);
timerframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setBackground(Color.white); //Set Framebackground
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().
setFullScreenWindow(frame); //Set Frame Size and position
frame.getContentPane().setLayout(new GridBagLayout()); //set new layout
exit.addActionListener(new ActionListener()
{ //This will set the action when the button is pressed on
public void actionPerformed(ActionEvent ae)
{ //This would be the method that has to be overridden
System.out.println("The Exit button was pushed");
System.exit(0);
}
});
submit.addActionListener(new ActionListener()
{
//This will set the action when the button is pressed on
public void actionPerformed(ActionEvent ae)
{ //This would be the method that has to be overridden
Connection connection = null;
try { //This should connect to a database
String driverName = "org.gjt.mm.mysql.Driver";
Class.forName(driverName);
String serverName = "SEAL";
String mydatabase = "TEST"; //Database connection
String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
String username = "BATTMAN";
String password = "SPIDERMAN";
connection = DriverManager.getConnection(url, username, password);
if(!connection.isClosed())
System.out.println("Successfully connected to MySQL server using TCP/IP...");
} catch (ClassNotFoundException e)
{
System.out.println("Error : "+e);
} catch (SQLException ex)
{
System.out.println("Error : "+ex);
}
}
});
GridBagConstraints gbc = new GridBagConstraints(); //Setting a GridBagConstraints object
gbc.gridy = 0; //set y position
gbc.gridx = 0; //set x position
frame.getContentPane().add(labname, gbc); //Add JLabel "Username" To JFrame
gbc.gridx = 1; //set x position
frame.getContentPane().add(username, gbc); //Add JTextField "Username" To JFrame
gbc.gridx = 2; //set x position
gbc.gridx = 0;
gbc.gridy = 1; //set y position
frame.getContentPane().add(emptyLabel1, gbc); //Add JLabel "Password" To JFrame
gbc.gridx = 0; //set x position
gbc.gridy = 2;
frame.getContentPane().add(labpass, gbc); //Add JTextField Box "password" to JFrame
gbc.gridx = 1; //set y position
frame.getContentPane().add(password, gbc); //Add empty label for space
gbc.gridx = 0; //set x position
gbc.gridy = 3; //set y position
frame.getContentPane().add(emptyLabel2, gbc); //Add empty label for space
gbc.gridy = 4; //set y position
gbc.gridx = 1; //set x position
frame.getContentPane().add(submit, gbc); //Add a submit JButton to the JFrame
gbc.gridy = 5; //set y position
frame.getContentPane().add(exit, gbc); //Add an exit JButton to the JFrame
frame.setVisible(true); //setting the frame to display
new real(timerframe, label, frame).run(); //This will create a timerframe, and start it running.
}
public void run()
{
timerframe.setVisible(true);
int x;
for(x=60; x>0; --x)
{ //Changed the value to 2 minutes (was 60)
label.setText("Min(s) remaining: " +x ); //This will set the text of the JLabel.
try
{
Thread.sleep(60000); //This will sleep for a minute
} catch(InterruptedException ie)
{
System.out.println("Error has occured: "+ie);
}
System.out.println("One minute has passed!");
}
timerframe.dispose();
}
}
-
would you please edit your post so that it is not 3 screens wide.. i find it very difficult to read. (try making the font arial narrow, or setting the size to 1, if you arent prepared to edit your code so that no line is more than 100 characters long
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks