Go Back   Computer Forums > Articles | Tutorials | Tech Notes > Articles | Computer and Server Software
Join Computer forums Today

Thread Tools Search this Thread Display Modes
Old 02-28-2007, 11:00 AM   #1
In Runtime
Daeva's Avatar
Join Date: Dec 2005
Posts: 407
Send a message via AIM to Daeva Send a message via MSN to Daeva Send a message via Yahoo to Daeva
Default Java program, tutorial

First off, I hope that anyone cares about this. If you do, pm me, or just respond to the post and let me know if i'm boring you. There just doesn't seem to be a whole lot of activity in this post.
Well, I have an assignment in my Advanced Java Web Database Programming class, and I thought I would post the work i'm doing here with comments, and maybe help someone out if they wanted to learn a little bit about gui's and database connectivity in Java.

The problem requires the creation of a class that allows a user to grow flowers. That is the only requirement. I like open ended problems like this, because you can play around a bit. Now, I won't be doing a multi-threaded application for 2 reasons:
A.) I'm still not as strong in that area of java as i'd like
B.) There is no reason to go THAT in depth with this.

After the code i'll have useful links to information about what i'm doing if i don't explain it well.

so to start with, we have to create the gui so we can start inputting values:
//First we import the necessary packages
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;
// Now we create the class 
public class flowers extends JFrame { // We inherit JFrame to build our window
	 Container c; //1
	 BorderLayout myLayout; //2
	 private JLabel label1, label2, label3, label4, label5; //3
	 public flowers() { //4
	 public flowers(String titleText) { //5
		 super(titleText); //6
		 myLayout = new BorderLayout(6,2); //7
		 //Call JLabel's constructor which creates a new object of JLabel, which 
		 in turn gives us a label with text of whatever we pass to it's 
		 label1 = new JLabel("This is the Top");
		 label2 = new JLabel("This is the Left");
		 label3 = new JLabel("This is the Center");
		 label4 = new JLabel("This is the Right");
		 label5 = new JLabel("This is the Bottom");
		 c = getContentPane();//8
		 c.setLayout(myLayout);// Add myLayout to c
		 // Add all the elements we created at the correct location
		 c.add(label1, myLayout.NORTH);
		 c.add(label2, myLayout.WEST);
		 c.add(label3, myLayout.CENTER);
		 c.add(label4, myLayout.EAST);
		 c.add(label5, myLayout.SOUTH); 
	 } // End 1 Argument Overloaded constructor 
	 public static void main(String args[]) { //9
		 flowers f = new flowers("Flowers Program"); //10
		 f.setSize(400,400); // Set window size
		 f.setVisible(true); // Make window vsible 
	 } // End Method Main
} // End of flowers class
Now, that program is 100% compileable and runnable, I wrote it so do what you want to it(Thats my GPL, use it however you want).
Now as for the comments I put with numbers, I didn't want to clutter up the code, so for long comment blocks I just put those numbers which are:

1.) I'm not going to explain the compete class heirarchy but you can check it out here http://java.sun.com/j2se/1.5.0/docs/...Container.html . The Container is the super class for many objects that we will be using. You can look at the heirarchy similar to the HTML DOM. Container is what we put in our JFrame to hold all of our objects. This line creates a Class wide Container object named c

2.) There are many different Layouts in java used to display and organize elements on screen. The borderLayout offers a lot of flexibility without having to know the precise coordinates. You can download a free ide which makes builing a gui extremely easy here http://www.mars3000.com/ .

3.) Simple JLabels. A JLabel simply is a text object for storing/displaying strings

4.) This is the default no Argument Constructor, if you don't want to give the window a name. The JVM will automatically add this if you don't specify one.

5.) Since our class flowers extends JFrame, we inherit all of it's methods as well. This one argument constructor takes a string value.

6.) Now we pass our string value to the JFrame constructor, which then assings the string to the titlebar of the JFrame.

7.) BorderLayout accepts various values for different layouts. This is best one(in my opinion) for gui layouts. You can play around with different values.

8.) See http://java.sun.com/j2se/1.5.0/docs/...tContentPane()

9.) Main method, which usually goes in a separate Driver class, but I see no use in doing that. Now we don't have to compile two classes and run the driver code.

10.) Here we create a new object of our flowers class. We can use this object to access the various methods of our flower class, and since the flower class inherits from JFrame, we can also use JFrame's methods. Here is also where we pass the title of the window.

11.) If the user closes the window, exit the application.

In a little while, we'll add buttons and some more graphical elements. I'll post them in this same post, so if you have any questions/comments about this one, post them here.

**Official Self-proclaimed glorified excessive (insert additional adjectives here) post editor/modifier.
Edit = Best feature ever
Daeva is offline   Reply With Quote
Old 03-05-2007, 02:43 PM   #2
In Runtime
Daeva's Avatar
Join Date: Dec 2005
Posts: 407
Send a message via AIM to Daeva Send a message via MSN to Daeva Send a message via Yahoo to Daeva
Default Re: Java program, tutorial

Ok, so now we need to add some interactivity to the application.
To do this, first we need elements,
So, at the top of the code, underneath the line where we declare all of our Jlabel's, add the following lines of code:
private JButton growButton, bloomButton, witherButton, growWithOptions;
private JPanel buttonPanel;
Now that we have allocated memory for our buttons, we need to add them to our screen. Now, the BorderLayout manager only allows for one gui element per section. For example, we could only add 1 of those buttons to south, 1 to west, and 1 to east, etc...
We can however create a JPanel, which inherits from Container.

So, in our 1 argument constructor add the following lines of code:

buttonPanel = new JPanel();
growButton = new JButton("Grow");
bloomButton = new JButton("Bloom");
bloomButton = new JButton("Wither");
growWithOptions = new JButton("Confirm");
buttonPanel.setLayout(new FlowLayout());

1 Last thing to add the buttons to our window. We need to add them to the container. You see, we added all of our buttons and gave them names to our JPanel named buttonPanel. Now, we have to add buttonPanel to our main Container.
to do this, you have to replace the line of code that says:
c.add(label3, myLayout.CENTER);
change it to:
c.add(buttonPanel, myLayout.CENTER);
**Official Self-proclaimed glorified excessive (insert additional adjectives here) post editor/modifier.
Edit = Best feature ever
Daeva is offline   Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

All times are GMT -5. The time now is 09:04 PM.

Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2016, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0