|
|
|
|
![]() ![]() |
Jun 26 2007, 03:42 AM
Post
#1
|
|
|
Newbie ![]() Group: Members Posts: 7 Joined: 26-June 07 Member No.: 45,460 |
Java GUI
Making a Little Java Program Sec. 1: Imports and starting it off Sec. 2: Variables Sec. 3: Frame and Stuff Sec. 4: Declaring buttons Sec. 5: Adding buttons Sec. 6: Action Listening Sec. 7: Using this for a learning experience Section 1 Now, let's think. What imports do we need? We obviously need GUI imports. We also need the action Listener. So, let's declare this at the very top: Code: CODE import java.awt.*; That's all we need to get all our supplies. Now to start us off.import java.awt.event.*; import javax.swing.*; Skip a couple lines and add: Code: CODE public class Tutorial extends JFrame implements ActionListener Section 2{ Let's declare our variables. Right below the class, add Code: CODE private static Tutorial frame; I will tell you about this later.private static JTextField text; Section 3 This is in all java programs. To start it and load everything, you must add: Code: CODE public static main(String[] args) We must now add the frame, so skip a 2 lines and add:{ Code: CODE frame = new Tutorial(); Ending the statement, add one last "}"frame.setTitle("Tutorial of GUI"); frame.setSize(400, 100); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setVisible(true); Section 4 Now add: Code: CODE public Tutorial() {Next add: Code: CODE setLayout(new BorderLayout()); That adds the button vars.JTextField text = new JTextField(10); JButton button = new JButton("JButton1"); JButton button1 = new JButton("JButton2"); JButton button2 = new JButton("JButton3"); JButton button3 = new JButton("JButton4"); JPanel panel = new JPanel(); Section 5 Let's add the buttons now. Add right below that, add: Code: CODE panel.setLayout(new FlowLayout()); That adds the buttons on.panel.add(button); panel.add(button1); panel.add(button2); panel.add(button3); add(panel, BorderLayout.NORTH); button.addActionListener(this); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); } Section 6 Let's make the outcome of clicking the button. Add: Code: CODE public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(frame, " was selected." ); } }That tells you what button was selected. Now save and compile. Now run the program and see what the outcome is. Section 7 You can view other things like this at: http://java.sun.com/developer/online.....;/contents.html This will help you understand all the functions of java GUI. -Bill for Unkn note:i copied this from my word and pasted it here hope this helps ya out im sorry if i misspelled anything i type fast |
|
|
|
Jun 27 2007, 04:55 PM
Post
#2
|
|
|
Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software. ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 975 Joined: 25-September 05 From: The dungeon deep below the foundation of trap17 Member No.: 12,251 |
Great tutorial! I still hate java though. I remember learning gui in java about four years ago, and it was hell for me. This probably would have made it a bit easier but I much prefer c++ to java when making gui applications. Anyway great tut! Keep up the good work!
|
|
|
|
Jun 29 2007, 06:47 AM
Post
#3
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 225 Joined: 5-January 07 Member No.: 36,560 |
These languages confuse me but at the same time fascinate me as some gibberish can turn into such good creation such as a great and a profitable web site. Thanks man love you as I wanted to just learn this.
|
|
|
|
Jun 29 2007, 11:32 AM
Post
#4
|
|
|
I'm back... well, sort of. ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 697 Joined: 26-December 05 From: somewhere in the middle of nowhere Member No.: 16,226 ![]() |
the reason why i like java more is because i could just use the exception handling for the errors. but i have to admit i'm not a great programmer at all. i never really appreciated c/c++ because of the complicated pointers. programming with pointers was hell for me.
anyways, great tutorial for starters. |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 27th July 2008 - 01:34 AM |