Results 1 to 3 of 3
  1. #1
    SpicyHero is offline Member
    Join Date
    Sep 2013
    Posts
    11
    Rep Power
    0

    Arrow My simple GUI program could use a bit of help.

    I've been learning java for a month so I'm pretty new. Anyways, I wanted my program to ask you your name. And once you've typed it in and pressed enter it would display what you typed. But once you set your name, nothing happens.

    Java Code:
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JOptionPane;
    
    public class thomas extends JFrame
    {
    	private JTextField EnterName;
    	private JLabel CurrentName;
    	
    	public thomas()
    	{
    		super("Name Colors");
    		setLayout(new FlowLayout());
    		
    		EnterName = new JTextField("Enter Name Here");
    		add(EnterName);
    		
    		thehandler handler = new thehandler();
    		EnterName.addActionListener(handler);
    		
    	}
    	
    	private class thehandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			String Name = "Name";
    			if(event.getSource()==EnterName)
    			{
    				Name=String.format("You have set your name to: %s", event.getActionCommand());
    				CurrentName = new JLabel("Current Name is: %s");
    				add(CurrentName);
    			}
    			
    			JOptionPane.showMessageDialog(null, Name);
    			
    		}
    	}
    }
    It's not displaying the CurrentName string I enteted. Any help? (If you need to see my main class file just say.)

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

    Default Re: My simple GUI program could use a bit of help.

    You should try to follow java class/variable naming conventions, which you can find via Google. It makes it easier for other people to read your code.


    As for your problem, one quick way to fix this is to add a JLabel to the frame and pass a reference to it to your handler (shown below). There are also better ways to do this - perhaps adding a method in your JFrame called setLabelText(String name) and you simply call it to set the name in the label. These are things you can think about as you learn Java better.

    Java Code:
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    
    public class thomas extends JFrame
    {
    	public static void main(String[] args)
    	{
    		new thomas().setVisible(true);
    	}
    	
    	private JTextField EnterName;
    	private JLabel CurrentName;
    	
    	public thomas()
    	{
    		super("Name Colors");
    		setLayout(new FlowLayout());
    		
    		EnterName = new JTextField("Enter Name Here");
    		add(EnterName);
    		JLabel label = new JLabel("");
    		add(label);
    		thehandler handler = new thehandler(label);
    		EnterName.addActionListener(handler);
    	}
    	
    	private class thehandler implements ActionListener
    	{
    		private JLabel label;
    		
    		public thehandler(JLabel label)
    		{
    			this.label = label;
    		}
    		
    		public void actionPerformed(ActionEvent event)
    		{
    			String Name = "Name";
    			if(event.getSource() == EnterName)
    			{
    				Name = String.format("You have set your name to: %s", event.getActionCommand());
    				label.setText("Current Name is: %s");
    			}
    			
    			JOptionPane.showMessageDialog(null, Name);
    			
    		}
    	}
    }
    Last edited by AndrewM16921; 09-21-2013 at 10:05 PM.

  3. #3
    SpicyHero is offline Member
    Join Date
    Sep 2013
    Posts
    11
    Rep Power
    0

    Default Re: My simple GUI program could use a bit of help.

    Thanks a ton man, you rock.

Similar Threads

  1. D&D simple program help
    By shvigity in forum New To Java
    Replies: 7
    Last Post: 09-11-2013, 10:02 AM
  2. help with ' simple if ' program
    By thenamesake in forum New To Java
    Replies: 9
    Last Post: 02-13-2013, 06:37 PM
  3. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 05:12 AM
  4. Simple program
    By 479320 in forum New To Java
    Replies: 6
    Last Post: 04-08-2011, 08:27 AM
  5. Please help with simple program.. Very simple.
    By jonytek in forum New To Java
    Replies: 7
    Last Post: 02-14-2011, 12:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •