Results 1 to 4 of 4
Thread: Problem with JTextField
- 05-17-2012, 04:53 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 18
- Rep Power
- 0
Problem with JTextField
I know swing decently well, but im having a weird problem with a JTextField.
Here's the code:
It should dispose of the frame if the text in passwordField is the same as password (in this case "tree"), but even if it's the same it runs the else statement. Do i do something wrong? I really don't know what's wrong.Java Code:import javax.swing.*; import java.awt.event.*; public class Frame { public static void main(String[] args) { new Frame(); } JFrame checkerFrame; JButton logon; JTextField passwordField; String password = "tree"; public Frame() { checkerFrame = new JFrame("Password"); checkerFrame.setSize(200,200); checkerFrame.setResizable(false); checkerFrame.setLayout(null); checkerFrame.setLocationRelativeTo(null); checkerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); checkerFrame.setVisible(true); logon = new JButton("Enter"); logon.setBounds(65,140,75,25); checkerFrame.add(logon); passwordField = new JTextField(); passwordField.setBounds(25,25,150,25); checkerFrame.add(passwordField); logon.addActionListener(new Handler()); } private class Handler implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource() == logon) { if(passwordField.getText() == password) { checkerFrame.dispose(); } else { JOptionPane.showMessageDialog(checkerFrame,"Incorrect Password. Please Try Again."); } } } } }
- 05-17-2012, 04:56 AM #2
Member
- Join Date
- Jan 2012
- Posts
- 18
- Rep Power
- 0
Re: Problem with JTextField
Oh, wow. Nevermind, stupid mistake on my part. I got it.
-
Re: Problem with JTextField
Yeah, you've got problems in that code
- Using == to compare Strings
- Using Strings for passwords period
- Using null layout, absolute positioning and setSize(...) rather than preferredSizes and layout managers
- Giving your class the same name as a core Java class, one similar to what you are using.
- ...
You'll want to read the Java tutorials and especially the Swing tutorials to fix these things, especially the layouts.
- 05-17-2012, 06:42 AM #4
Member
- Join Date
- Jan 2012
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
problem with JTextfield..
By Shahnawaz in forum New To JavaReplies: 2Last Post: 10-04-2011, 08:16 AM -
Problem Loading JTextField in JFrame / Problema al cargar JTextField en JFrame
By thor_inc in forum AWT / SwingReplies: 0Last Post: 08-30-2011, 09:18 AM -
Problem with JTextField.. please help!!
By Asvin in forum New To JavaReplies: 21Last Post: 12-18-2010, 03:49 PM -
Problem's with JTextField
By DC% in forum AWT / SwingReplies: 4Last Post: 03-10-2009, 05:33 PM -
Got problem with JtextField
By hungleon88 in forum AWT / SwingReplies: 4Last Post: 12-06-2008, 03:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks