+ Reply to Thread
Results 1 to 4 of 4

Thread: Java:Tutorial - Making multiple objects work differently

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

    Java:Tutorial - Making multiple objects work differently

    This is the sixth tutorial that will show you how to create graphical user interfaces using java.

    Prerequisites

    You should know how to create a window in Java.
    LINK GOES HERE

    You should know how to add a button to the window in Java.
    LINK GOES HERE

    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. In pervious tutorials, our two buttons did the same thing, but real applications have different buttons that perform different functions.

    Solution
    Lets start with a shell we’ve formulated in previous tutorials:
    Code:
     package cctuts;
    
    import java.awt.event.*;
    import javax.swing.*;
    
    public class InterfaceSix implements ActionListener{
    	
    JFrame interfaceFrame;
    JButton startButton, stopButton;
    	
    	public InterfaceSix(){
        	JFrame.setDefaultLookAndFeelDecorated(true);
        	interfaceFrame = new JFrame("First GUI");
        	interfaceFrame.setSize(200,70);
        	interfaceFrame.setLayout(new java.awt.GridLayout(1,2));
        	
    		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) {
    
    	}
    	
    	public static void main(String[] args){ 
    		new InterfaceSix();
    	}
    }
    Now just to recap what we have here: a class that makes a visible window 200px by 70px. The window looks pretty because we set the default look and feel. The window contains two button objects which are positioned using a grid layout. Each button as an action listener, so when the button is pressed the actionPerformed method is invoked. If anything I just said doesn’t make sense, you should read my previous tutorials. In the actionPerformed method we need to determine which button is pressed so we can have our program act accordingly. We do this by using the a.getSource() method, which tells us which button is being pressed. We can then perform simple logical operations to tell our program what to do.

    For example if the startButton is pressed, a.getSource() == startButton. We then can say something like:
    Code:
    if(a.getSource() == startButton){
    System.out.println(“The start button was pushed…”);
    Incorporated into a full class it would look something like this:
    Code:
     package cctuts;
    
    import java.awt.event.*;
    import javax.swing.*;
    
    public class InterfaceSix implements ActionListener{
    	
    
    	JFrame interfaceFrame;
        JButton startButton, stopButton;
    	
    	public InterfaceSix(){
        	JFrame.setDefaultLookAndFeelDecorated(true);
        	interfaceFrame = new JFrame("First GUI");
        	interfaceFrame.setSize(200,70);
        	interfaceFrame.setLayout(new java.awt.GridLayout(1,2));
        	
    		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) {
    		if(a.getSource() == startButton){
    			System.out.println("Start button was pressed...");
    		}
    		
    		if(a.getSource() == stopButton){
    			System.out.println("Stop button was pressed...");
    		}
    	}
    	
    	public static void main(String[] args){ 
    		new InterfaceSix();
    	}
    }

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

     
  3. #2
    ljimmyd is offline Newbie
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Re: Java:Tutorial - Making multiple objects work differently

    hi John:

    I'm curious that what is the "ActionEvent e". I checked in the Java API showing that it is a class. Does it mean that we are creating an object called 'e' here? If so, why isn't there a 'new' operation? Cheers

  4. #3
    farrell2k is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    60
    Rep Power
    11

    Re: Java:Tutorial - Making multiple objects work differently

    Quote Originally Posted by ljimmyd View Post
    hi John:

    I'm curious that what is the "ActionEvent e". I checked in the Java API showing that it is a class. Does it mean that we are creating an object called 'e' here? If so, why isn't there a 'new' operation? Cheers
    ActionEvent 'e' is an ActionEvent object. It contains info detailing what occured and from where it occured. You are causing an ActionEvent object to be created when you click a button. Java's event dispatch thread creates an ActionEvent object referenced by 'e' when it detects that a button is clicked. The ActionEvent 'e' object is sent by the event dispatch thread as an argument to the actionPerformed() method. ActionPerformed then knows what type of action happened and from which button it originated.

  5. #4
    ljimmyd is offline Newbie
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Re: Java:Tutorial - Making multiple objects work differently

    Quote Originally Posted by farrell2k View Post
    ActionEvent 'e' is an ActionEvent object. It contains info detailing what occured and from where it occured. You are causing an ActionEvent object to be created when you click a button. Java's event dispatch thread creates an ActionEvent object referenced by 'e' when it detects that a button is clicked. The ActionEvent 'e' object is sent by the event dispatch thread as an argument to the actionPerformed() method. ActionPerformed then knows what type of action happened and from which button it originated.
    Cheers mate. Helps a lot.

+ 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. Waiting For Multiple Objects (win32)
    By innerLOL in forum General Programming
    Replies: 12
    Last Post: 01-05-2010, 02:21 PM
  3. Making 3D objects?
    By adem3311 in forum Software Development Tools
    Replies: 10
    Last Post: 04-28-2009, 08:14 AM
  4. Java:Tutorial - Adding more objects to your window
    By John in forum Java Tutorials
    Replies: 0
    Last Post: 01-11-2007, 12:09 PM
  5. Java:Tutorial - Make Your Button Work
    By John in forum Java Tutorials
    Replies: 0
    Last Post: 01-11-2007, 12:04 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