+ Reply to Thread
Results 1 to 1 of 1

Thread: Java:Tutorial - Adding more objects to your window

  1. #1
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    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 01:21 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Java:Tutorial - Making A Window
    By John in forum Java Tutorials
    Replies: 7
    Last Post: 08-31-2011, 04:26 PM
  2. Replies: 3
    Last Post: 06-19-2010, 06:00 AM
  3. java applet window size??
    By stack in forum Java Help
    Replies: 5
    Last Post: 07-24-2007, 07:00 AM
  4. Java:Tutorial - Adding Buttons to your Interface
    By John in forum Java Tutorials
    Replies: 0
    Last Post: 01-11-2007, 12:03 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts