Grade 12 ISU, need help
Author |
Message |
destry0r
|
Posted: Tue May 15, 2007 1:37 pm Post subject: Grade 12 ISU, need help |
|
|
Well I figured out how to do JFrame an all that stuff up to a certain point. I am doing a veterinary clinic program, and I am at the password input spot. How would I make the program open a new frame, with the main menu, after inputing the correct password? Also how would I get the frame to dispose? I plated around with dispoe, but couldn't get it to work properly.
code: | import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Summative07 extends JFrame
{
JFrame pwind;
Container pPanel;
TextField passlab;
JPasswordField pswdField;
JButton ok, exit;
public static void main (String[] args)
{
JFrame.setDefaultLookAndFeelDecorated (true);
new Summative07 ();
}
public Summative07 ()
{
JFrame pwind = new JFrame ("Veterinary Clinic Program 0.1");
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment ();
setDefaultCloseOperation (DISPOSE_ON_CLOSE);
pwind.addWindowListener (new PasswordFrame ());
pPanel = (Container) pwind.getContentPane ();
JPanel pf = new JPanel ();
pf.setLayout (new GridLayout (2, 1));
pswdField = new JPasswordField (20);
passlab = new TextField ("Please input your password");
passlab.setEnabled (false);
pf.add (pswdField);
pf.add (passlab);
JPanel buttonp = new JPanel ();
buttonp.setLayout (new GridLayout (1, 2));
ok = new JButton ("OK");
exit = new JButton ("Exit");
buttonp.add (ok);
buttonp.add (exit);
pPanel.setLayout (new BorderLayout ());
pPanel.add (pf, BorderLayout.CENTER);
pPanel.add (buttonp, BorderLayout.SOUTH);
pwind.setExtendedState (Frame.MAXIMIZED_BOTH);
pwind.setMaximizedBounds (env.getMaximumWindowBounds ());
pwind.setVisible (true);
exit.addActionListener (new ActionListener ()
{
public void actionPerformed (ActionEvent evt)
{
System.exit (0);
}
}
);
ok.addActionListener (new ActionListener ()
{
public void actionPerformed (ActionEvent evt)
{
Password ();
}
}
);
}
public class PasswordFrame extends WindowAdapter
{
public void windowClosing (WindowEvent e)
{
System.exit (0);
}
}
public void Password ()
{
if (pswdField.getText ().equals ("Ad43O"))
{
passlab.setText ("Password Correct!");
}
else
{
passlab.setText ("Invalid Password!");
}
}
}
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Luny
|
Posted: Tue May 15, 2007 11:02 pm Post subject: RE:Grade 12 ISU, need help |
|
|
First, I would change the return type of the method Password(). It should return a boolean value and that would indicate whether or not you should open a new Password Frame.
Another thing I notice is that a PasswordField's method getText() is deprecated. Use the method getPassword() which returns a character array and then convert the char array into a string object and compare the two string "password field's text" and the "password".
And your PasswordFrame only can extend one class and I do not recommend it being WindowAdapter. I assume you want the full functionality of a JFrame so extend that instead and just have a button to where you can close the frame.
And getting your frame to dispose, you would just use the setVisible() method with false in the argument.
Here are some updates I made to your program.
code: |
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Summative07 extends JFrame
{
JFrame pwind;
Container pPanel;
TextField passlab;
JPasswordField pswdField;
JButton ok, exit;
public static void main (String[] args)
{
JFrame.setDefaultLookAndFeelDecorated (true);
new Summative07 ();
}
public Summative07 ()
{
JFrame pwind = new JFrame ("Veterinary Clinic Program 0.1");
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment ();
setDefaultCloseOperation (DISPOSE_ON_CLOSE);
pPanel = (Container) pwind.getContentPane ();
JPanel pf = new JPanel ();
pf.setLayout (new GridLayout (2, 1));
pswdField = new JPasswordField (20);
passlab = new TextField ("Please input your password");
passlab.setEnabled (false);
pf.add (pswdField);
pf.add (passlab);
JPanel buttonp = new JPanel ();
buttonp.setLayout (new GridLayout (1, 2));
ok = new JButton ("OK");
exit = new JButton ("Exit");
buttonp.add (ok);
buttonp.add (exit);
pPanel.setLayout (new BorderLayout ());
pPanel.add (pf, BorderLayout.CENTER);
pPanel.add (buttonp, BorderLayout.SOUTH);
pwind.setExtendedState (Frame.MAXIMIZED_BOTH);
pwind.setMaximizedBounds (env.getMaximumWindowBounds ());
pwind.setVisible (true);
exit.addActionListener (new ActionListener ()
{
public void actionPerformed (ActionEvent evt)
{
System.exit (0);
}
}
);
ok.addActionListener (new ActionListener ()
{
public void actionPerformed (ActionEvent evt)
{
if(Password ())
new PasswordFrame();
}
}
);
}
private class PasswordFrame extends JFrame
{
public PasswordFrame()
{
super("Password Frame");
setSize(200,200);
setVisible(true);
}
public void windowClosing (WindowEvent e)
{
System.exit (0);
}
}
public boolean Password ()
{
char password[] = pswdField.getPassword();
String passwordString = "";
for(char c : password)
passwordString += c;
if (passwordString.equals ("Ad43O"))
{
passlab.setText ("password valid!");
return true;
}
else
{
passlab.setText ("password invalid!");
return false;
}
}
}
|
|
|
|
|
|
|
destry0r
|
Posted: Wed May 16, 2007 12:44 pm Post subject: Re: Grade 12 ISU, need help |
|
|
Thanks for the help, it definately was appreciated!
I do however have one question, does
code: | for (char c : password)
passwordString += c; |
mean the same thing as
code: | String passwordString = new String (password); |
and then I just compare passwordString to the saved valid password?
I ask because the IDE my school uses gave me an error when I use your code.
(I myself have never seen that type of code before) |
|
|
|
|
|
HellblazerX
|
Posted: Wed May 16, 2007 3:20 pm Post subject: Re: Grade 12 ISU, need help |
|
|
destry0r @ Wed May 16, 2007 12:44 pm wrote:
code: | for (char c : password)
passwordString += c; |
That's just one of the new for loops found in the newer versions of JDK. I don't remember which one. You can pass in an array and Java will automatically iterate through it. If you're using RTP (which I'm betting your school is), it won't support that feature.
destry0r @ Wed May 16, 2007 12:44 pm wrote:
code: | String passwordString = new String (password); |
and then I just compare passwordString to the saved valid password?
I ask because the IDE my school uses gave me an error when I use your code.
(I myself have never seen that type of code before)
That's fine. If anything, it's more efficient. You can't add char to String objects like adding numbers. Every time you do it, Java creates a new String object which has that additional char. |
|
|
|
|
|
Luny
|
Posted: Wed May 16, 2007 4:16 pm Post subject: RE:Grade 12 ISU, need help |
|
|
@HellblazerX: You are indeed correct. His way is more efficent than my for loop. My mistake.
The for loop that I used was introduced in Java 5. I would talk to your instructor and ask what version of Java you are developing under. It is very important because some features aren't available on older versions.
I, myself, love to be always up to date on the JDK and learning about the new features Java has to offer.
Anyways, when you have the input from the PasswordField, pass the char array into a string constructor and use the equals() method to see if both strings match.
code: |
String passwordString = new String(password);
if (password.equals("whatever password saved"))
return true;
else
return false;
|
My way would just return the following condition.
code: |
return password.equals("whatever password saved");
|
|
|
|
|
|
|
destry0r
|
Posted: Wed May 16, 2007 8:41 pm Post subject: Re: Grade 12 ISU, need help |
|
|
@HellblazerX: Good guess with RTP!
@Luny: The IDE my school uses 1.4.2 (I don't know if that's the version you were talking about, since I am unaware of a version 5.1 (1.5?), I use 1.6.0 at home with JCreator)
Sadly there is no sign that RTP will ever get another update. After checking the site, there "newest" update was related to 2001 0o. I think though for the purposes of our class 1.4.2 is enough (I could be wrong though)
Nice to learn something new though that I can use for my own out of school projects
If I need more help with my ISU I will post it in this thread, you guys have been great |
|
|
|
|
|
destry0r
|
Posted: Tue May 22, 2007 12:02 pm Post subject: Re: Grade 12 ISU, need help |
|
|
Hey guys, here's my next question: How do I set the background colour of a panel with only radio buttons (example code):
code: | JPanel sarea = new JPanel ();
sarea.setLayout (new GridLayout (2, 1));
JButton save = new JButton ("Save Changes to:");
JPanel saverad = new JPanel ();
saverad.setLayout (new GridLayout (1, 2));
JRadioButton mains = new JRadioButton ("Main", true);
JRadioButton backs = new JRadioButton ("Back-up");
ButtonGroup savegroup = new ButtonGroup ();
savegroup.add (mains);
savegroup.add (backs);
saverad.add (mains);
saverad.add (backs);
/*
For example, where intblue is a already declared colour
using RGB
*/
|
|
|
|
|
|
|
|
|