Java program, tutorial

Daeva

In Runtime
Messages
407
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:
Code:
[color=green]//First we import the necessary packages[/color]
[color=blue]import[/color] java.awt.*;
[color=blue]import[/color] javax.swing.*;
[color=blue]import[/color] java.awt.event.*;
[color=blue]import[/color] java.util.*;
[color=blue]import[/color] java.lang.*;
[color=green]// Now we create the class[/color] 
 
 
[color=blue]public[/color] [color=blue]class[/color] flowers [color=blue]extends[/color] JFrame [color=red]{ [/color][color=green]// We inherit JFrame to build our window[/color]
 
[color=black][color=royalblue]	 Container[/color] c; [/color][color=seagreen]//1[/color]
[color=black][color=royalblue]	 BorderLayout[/color] myLayout; [/color][color=green]//2[/color]
[color=black][color=blue]	 private[/color] JLabel label1, label2, label3, label4, label5; [/color][color=green]//3[/color]
 
[color=#000000][color=blue]	 public[/color] flowers[color=red]() {[/color] [/color][color=green]//4[/color]
[color=red]	 }[/color] 
 
 
[color=#000000][color=blue]	 public[/color] flowers[color=red]([/color][color=royalblue]String[/color] titleText[color=red])[/color] [color=red]{[/color] [/color][color=green]//5[/color][color=#008000][color=blue]		 [/color][/color]
[color=#008000][color=blue]		 super[/color][color=red]([/color][color=black]titleText[/color][color=red])[/color][/color][color=black]; [/color][color=green]//6[/color]
[color=#000000]		 myLayout = [color=blue]new[/color] [color=royalblue]BorderLayout[/color][color=red]([/color]6,2[color=red])[/color]; [/color][color=green]//7[/color]
[color=green]		 //Call JLabel's constructor which creates a new object of JLabel, which [/color]
[color=green]		 in turn [/color][color=green]gives us a label with text of whatever we pass to it's [/color]
[color=green]		 constructor[/color]
 
[color=#000000]		 label1 = [color=blue]new[/color] JLabel[color=red]([/color][color=darkgreen]"[/color][color=darkgreen]This is the Top[/color][color=darkgreen]"[/color][color=red])[/color];[/color]
[color=#000000]		 label2 = [color=blue]new[/color] JLabel[color=red]([/color][color=darkgreen]"[/color][color=darkgreen]This is the Left[/color][color=darkgreen]"[/color][color=red])[/color];[/color]
[color=#000000]		 label3 = [color=blue]new[/color] JLabel[color=red]([/color][color=darkgreen]"[/color][color=darkgreen]This is the Center[/color][color=darkgreen]"[/color][color=red])[/color];[/color]
[color=#000000]		 label4 = [color=blue]new[/color] JLabel[color=red]([/color][color=darkgreen]"[/color][color=darkgreen]This is the Right[/color][color=darkgreen]"[/color][color=red])[/color];[/color]
[color=#000000]		 label5 = [color=blue]new[/color] JLabel[color=red]([/color][color=darkgreen]"[/color][color=darkgreen]This is the Bottom[/color][color=darkgreen]"[/color][color=red])[/color];[/color]
[color=#000000]		 c = getContentPane[color=red]()[/color];[/color][color=green]//8[/color]
[color=#000000]		 c.setLayout[color=red]([/color]myLayout[color=red])[/color];[/color][color=green]/[/color][color=green]/ Add myLayout to c[/color]
[color=green]		 // Add all the elements we created at the correct location[/color]
[color=#000000]		 c.add[color=red]([/color]label1, myLayout.NORTH[color=red])[/color];[/color]
[color=#000000]		 c.add[color=red]([/color]label2, myLayout.WEST[color=red])[/color];[/color]
[color=#000000]		 c.add[color=red]([/color]label3, myLayout.CENTER[color=red])[/color];[/color]
[color=#000000]		 c.add[color=red]([/color]label4, myLayout.EAST[color=red])[/color];[/color]
[color=#000000]		 c.add[color=red]([/color]label5, myLayout.SOUTH[color=red])[/color];[/color] 
 
 
[color=red]	 }[/color] [color=green]// End 1 Argument Overloaded constructor[/color] 
 
[color=blue]	 public static [/color][color=royalblue]void[/color] [color=black]main[/color][color=red]([/color][color=royalblue]String[/color] [color=black]args[/color][color=red][])[/color] [color=red]{ [/color][color=green]//9[/color]
[color=black]		 flowers f = new flowers[/color][color=red]([/color][color=darkgreen]"Flowers Program[/color][color=darkgreen]"[/color][color=red])[/color][color=black]; [/color][color=green]//10[/color]
[color=black]		 f.setSize[/color][color=red]([/color][color=darkslateblue]400[/color],[color=darkslateblue]400[/color][color=red])[/color][color=black]; [/color][color=green]// Set window size[/color]
[color=black]		 f.setDefaultCloseOperation[/color][color=red]([/color][color=black]JFrame.EXIT_ON_CLOSE[/color][color=red])[/color][color=black];[/color]
[color=green]		 //11[/color]
[color=black]		 f.setVisible[/color][color=red]([/color][color=royalblue]true[/color][color=red])[/color][color=black]; [/color][color=green]// Make window vsible[/color] 
[color=red]	 } [/color][color=green]// End Method Main[/color]
 
[color=red]} [/color][color=green]// End of flowers class[/color]

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/api/java/awt/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/api/javax/swing/JApplet.html#getContentPane()

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.
 
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:
Code:
[color=blue]private[/color] [color=black]JButton growButton, bloomButton, witherButton, growWithOptions;[/color]
[color=blue]private[/color] [color=black]JPanel buttonPanel;[/color]

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:

Code:
[color=black]buttonPanel =[/color] [color=blue]new[/color] [color=black]JPanel[color=red]()[/color];[/color]
[color=black]growButton =[/color] [color=blue]new[/color] [color=black]JButton[color=red]([/color][color=teal]"Grow"[/color][color=red])[/color];[/color]
[color=black]bloomButton =[/color] [color=blue]new[/color] [color=black]JButton[color=red]([/color][color=teal]"[/color][color=teal]Bloom"[/color][color=red])[/color];[/color]
[color=black]bloomButton =[/color] [color=blue]new[/color] [color=black]JButton[color=red]([/color][color=teal]"Wither"[/color][color=red])[/color];[/color]
[color=black]growWithOptions =[/color] [color=blue]new[/color] [color=black]JButton[/color][color=red]([/color][color=teal]"[/color][color=teal]Confirm"[/color][color=red])[/color][color=black];[/color]
[color=black]buttonPanel.setLayout[/color][color=red]([/color][color=blue]new[/color] [color=blue]FlowLayout[/color][color=red]())[/color][color=black];[/color]
[color=black]buttonPanel.add[color=red]([/color]growButton[color=red])[/color];[/color]
[color=black]buttonPanel.add[color=red]([/color]bloomButton[color=red])[/color];[/color]
[color=black]buttonPanel.add[color=red]([/color]witherButton[color=red])[/color];[/color]
[color=#000000]
[/color]
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);
 
Back
Top Bottom