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...
Now we can start on the function and GUI size of the program...Code: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 the last part first is to set out the main function of the whole program...Code: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 }
And the last thing our method!(This is the important part, without this the program won't have a meaning...(I guess)Code:public static void main (String[] arg) { new Jhello(); }
So now here is the whole code...Code:public void actionPerformed(ActionEvent e) {//The catcher String namne = answer.getText();//The one that catch'd it welcoming.setText("Welcome " + namne + "!");//Output } }
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 itCode: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 + "!"); } }
So thats from me, cheers!
Nice tutorial! +rep
Thanks! Added +. keep this up!!!
Good job! This helped me a bit, thanks =P
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks