Jump to content
xisto Community
Sign in to follow this  
onyxgem

Making a java based program

Recommended Posts

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:

import java.awt.*;import java.awt.event.*;import javax.swing.*;
That's all we need to get all our supplies. Now to start us off.
Skip a couple lines and add:

Code:
public class Tutorial extends JFrame implements ActionListener	  {
Section 2

Let's declare our variables.
Right below the class, add

Code:
private static Tutorial frame;	   private static JTextField text;
I will tell you about this later.

Section 3

This is in all java programs. To start it and load everything, you must add:

Code:
public static main(String[] args){
We must now add the frame, so skip a 2 lines and add:

Code:
frame = new Tutorial();		  frame.setTitle("Tutorial of GUI");		  frame.setSize(400, 100);		  frame.setDefaultCloseOperation(EXIT_ON_CLOSE);		  frame.setVisible(true);
Ending the statement, add one last "}"

Section 4

Now add:

Code:
public Tutorial()	{Next add:

Code:
setLayout(new BorderLayout());			  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();
That adds the button vars.

Section 5

Let's add the buttons now. Add right below that, add:

Code:
panel.setLayout(new FlowLayout());			  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);		 }
That adds the buttons on.

Section 6

Let's make the outcome of clicking the button. Add:

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://www.oracle.com/technetwork/java/index.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 :)

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.