Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Simple JString reader...

align string

  • Please log in to reply
4 replies to this topic

#1 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 17 June 2008 - 08:17 AM

I thought that I could show a thing I worked on this morning. Am pretty new on java and very new too the "java swing" thingy...lol

So here is what I done;

First of all we could start of the menu for input data and output...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Jhello extends JFrame implements ActionListener {
	
	JTextField answer = new JTextField(25);//The size of inputscreen
	JLabel welcoming = new JLabel();//The size of the outputscreen

Now we can start on the function and GUI size of the program...
public Jhello () {
		setLayout(new FlowLayout());//Declaring it's an active app
		add(new JLabel("What is your name? ", JLabel.CENTER));//Postion of "questioning"(which is the "What is your name? ")
		add(answer);//functions - Input
		add(welcoming);/functions - Output
		welcoming.setHorizontalAlignment(JLabel.CENTER);//Position of output "answer"
		answer.addActionListener(this);//This catches your input
		setSize(350, 100);//Size of the window
		setVisible(true);//If the text should be visible nor not
		setDefaultCloseOperation(EXIT_ON_CLOSE);//When you close it
        }

Now the last part first is to set out the main function of the whole program...
public static void main (String[] arg) {
		new Jhello();
	}

And the last thing our method!(This is the important part, without this the program won't have a meaning...(I guess)
public void actionPerformed(ActionEvent e) {//The catcher
		String namne = answer.getText();//The one that catch'd it
		welcoming.setText("Welcome " + namne + "!");//Output
	}
}

So now here is the whole code...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Jhello extends JFrame implements ActionListener {
	
	JTextField answer = new JTextField(25);
	JLabel welcoming = new JLabel();
	
	public Jhello () {
		setLayout(new FlowLayout());
		add(new JLabel("What is your name? ", JLabel.CENTER));
		add(answer);
		add(welcoming);
		welcoming.setHorizontalAlignment(JLabel.CENTER);
		answer.addActionListener(this);
		setSize(350, 100);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	public static void main (String[] arg) {
		new Jhello();
	}
	public void actionPerformed(ActionEvent e) {
		String namne = answer.getText();
		welcoming.setText("Welcome " + namne + "!");
	}
}

I hope this will be useful for people and am truly sorry if I couldn't explain in a better way. I understand thing on my own way and normally can't explain it :(
So thats from me, cheers!
  • 0

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 17 June 2008 - 04:54 PM

Nice tutorial! +rep
  • 0

#3 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 19 June 2008 - 07:20 AM

Nice tutorial! +rep


Thanks :P
  • 0

#4 isuru

isuru

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 249 posts

Posted 14 May 2010 - 04:00 AM

Thanks! Added +. keep this up!!!
  • 0

#5 GMVResources

GMVResources

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 71 posts

Posted 13 June 2010 - 07:25 PM

Good job! This helped me a bit, thanks =P
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download