Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Tutorials > Java Tutorials

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

Java Tutorials Tutorials and Code for Java

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-11-2007, 02:09 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 3,205
Last Blog:
Passwords
Credits: 842
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
Default Java:Tutorial - Adding more objects to your window

This is the fifth of six tutorials that will show you how to create graphical user interfaces using java.

Prerequisites
This tutorial is moderately difficult. If you know the Java basics please refer to my previous tutorials. If you do know the Java basics you should be familiar with my previous tutorials on GUI's. To read my tutorials please refer to my INDEX

The Idea
In order for your program to be attractive, the user must be able to easily navigate through your program. By creating a GUI the user is presented with all the features of the program in a clear and coherent manner. Adding buttons to your interface allow for user interaction which is the reason for a GUI.

Solution
Now if you have read my previous tutorials and were experimental, I’m sure you have tried to add another button to your program with no luck. I said that using the add() method “adds” a object to your window. Well it does, but you have to specify where to place the object. If you don’t specify, they are placed on top of each other causing you not to see the other objects. There are a few ways to specify the locations and ill discuss two layouts here. Here is the code we created in a previous tutorial.

Code:
 package cctuts;

import java.awt.event.*;
import javax.swing.*;

public class InterfaceFour implements ActionListener {
    JFrame interfaceFrame;
    JButton startButton;
    
    public InterfaceFour() {
    	JFrame.setDefaultLookAndFeelDecorated(true);
    	interfaceFrame = new JFrame("First GUI");
    	interfaceFrame.setSize(200,70);
    	interfaceFrame.setVisible(true);
    	
		startButton = new JButton("Start");
		startButton.addActionListener(this);
		interfaceFrame.add(startButton);
    
		interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		interfaceFrame.setVisible(true);
    }

	public void actionPerformed(ActionEvent a) {
		for(int i = 0; true; i++){
			System.out.println(i);
		}
	}
	
    public static void main(String[] args) {
        new InterfaceFour();
    }
}
To add the layout we are going to use the addLayout() method available to us, and im sure you wont be surprised that that method requires an object. The most basic layout you can use is called the FlowLayout. It basically takes your objects and centers them in your window depending on your dimensions. To do this we add this line of code in our constructor.
Code:
 interfaceFrame.setLayout(new java.awt.FlowLayout());
So if you create your code with two buttons, it should look similar to this:
Code:
 package cctuts;

import java.awt.event.*;
import javax.swing.*;

public class InterfaceFive implements ActionListener {
    JFrame interfaceFrame;
    JButton startButton, stopButton;
    
    public InterfaceFive() {
    	JFrame.setDefaultLookAndFeelDecorated(true);
    	interfaceFrame = new JFrame("First GUI");
    	interfaceFrame.setSize(200,70);
    	interfaceFrame.setVisible(true);
    	interfaceFrame.setLayout(new java.awt.FlowLayout());
    	
		startButton = new JButton("Start");
		startButton.addActionListener(this);
		interfaceFrame.add(startButton);
		
		stopButton = new JButton("Stop");
		stopButton.addActionListener(this);
		interfaceFrame.add(stopButton);
    
		interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		interfaceFrame.setVisible(true);
    }

	public void actionPerformed(ActionEvent a) {
		for(int i = 0; true; i++){
			System.out.println(i);
		}
	}
	
    public static void main(String[] args) {
        new InterfaceFive();
    }
}
Image:


The second way, and my preference, to do this is to use a grid layout. It works very similar to a HTML table. To create a grid we are again going to use the addLayout() method and give it a parameter, but this time rather than using java.awt.FlowLayout() we are going to use java.awt.GridLayout() which itself requires a x and y integer dimension. So it would look like this:

Code:
interfaceFrame.setLayout(new java.awt.GridLayout(1,2));
Giving you a similar look as the previous method:

Last edited by John; 01-11-2007 at 03:21 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Java:Tutorial - Tic-Tac-Toe John Java Tutorials 25 02-27-2008 06:41 PM
! Need urgent help ! Drawing program using cygwin siren C and C++ 0 05-26-2007 10:51 PM
Java:Tutorial - Making multiple objects work differently John Java Tutorials 0 01-11-2007 02:10 PM
Java:Tutorial - Adding Buttons to your Interface John Java Tutorials 0 01-11-2007 02:03 PM
Java:Tutorial - Making A Window John Java Tutorials 0 01-11-2007 02:02 PM


All times are GMT -5. The time now is 03:52 AM.

Contest Stats

Xav ........ 1024.41
MeTh0Dz|Reb0rn ........ 974.08
morefood2001 ........ 850.04
John ........ 841.93
WingedPanther ........ 661.52
marwex89 ........ 575.59
Brandon W ........ 456.18
chili5 ........ 292.12
orjan ........ 187.41
Steve.L ........ 181.88

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 79%

Ads