+ Reply to Thread
Results 1 to 1 of 1

Thread: Java:Tutorial - Make Your Button Work

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

    Java:Tutorial - Make Your Button Work

    This is the third 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. Although buttons look good, if they don’t do anything they have no meaning.

    Solution
    Since this tutorial assumes you know how to create a window and add a button to the window we will start off with that code.
    Code:
     package cctuts;
    
    import javax.swing.*;
    
    public class InterfaceThree extends JFrame{
    	
    	public InterfaceThree(){
    	   setSize(400,400);
    	   JButton startButton = new JButton("Start");
    	   setDefaultCloseOperation(EXIT_ON_CLOSE);
    	   setVisible(true);
    	}
    	
    	public static void main(String[] args){ 
    		new InterfaceThree();
    	}
    }
    The first thing we need to do is import another package and implement the ActionListerner class within that package to handle events. The package is java.awt.event (awt meaning abstract windowing toolkit). This looks like:
    Code:
    package cctuts;
    
    import java.awt.event.*;
    import javax.swing.*;
    
    public class InterfaceThree extends JFrame implements ActionListener{
    	
    	public InterfaceThree(){
    	   setSize(400,400);
    	   JButton startButton = new JButton("Start");
    	   add(startButton);
    	   setDefaultCloseOperation(EXIT_ON_CLOSE);
    	   setVisible(true);
    	}
    	
    	public static void main(String[] args){ 
    		new InterfaceThree();
    	}
    }
    By implementing the ActionListener interface, our class is required to use the actionPerformed method.
    Code:
     package cctuts;
    
    import java.awt.event.*;
    import javax.swing.*;
    
    public class InterfaceThree extends JFrame implements ActionListener{
    	
    	public InterfaceThree(){
    	   setSize(400,400);
    	   JButton startButton = new JButton("Start");
    	   add(startButton);
    	   setDefaultCloseOperation(EXIT_ON_CLOSE);
    	   setVisible(true);
    	}
    	
    	public void actionPerformed(ActionEvent a) {
    
    	}
    	
    	public static void main(String[] args){ 
    		new InterfaceThree();
    	}
    }
    Now you are wondering, How does the program know if the button is pushed? Well at the moment it doesn’t. To do so, we must add an action listener to the startButton object. Like this:
    Code:
     package cctuts;
    
    import java.awt.event.*;
    import javax.swing.*;
    
    public class InterfaceThree extends JFrame implements ActionListener{
    	
    	public InterfaceThree(){
    	   setSize(400,400);
    	   JButton startButton = new JButton("Start");
    	   startButton.addActionListener(this);
    	   add(startButton);
    	   setDefaultCloseOperation(EXIT_ON_CLOSE);
    	   setVisible(true);
    	}
    	
    	public void actionPerformed(ActionEvent a) {
    
    	}
    	
    	public static void main(String[] args){ 
    		new InterfaceThree();
    	}
    }
    Now anything in the actionPerformed method will be executed every time that button is pressed. So add something in there and your done! Here is my final code to demonstrate the idea:
    Code:
     package cctuts;
    
    import java.awt.event.*;
    import javax.swing.*;
    
    public class InterfaceThree extends JFrame implements ActionListener{
    	
    	public InterfaceThree(){
    	   setSize(400,400);
    	   JButton startButton = new JButton("Start");
    	   startButton.addActionListener(this);
    	   add(startButton);
    	   setDefaultCloseOperation(EXIT_ON_CLOSE);
    	   setVisible(true);
    	}
    	
    	public void actionPerformed(ActionEvent a) {
    		for(int i = 0; true; i++){
    			System.out.println(i);
    		}
    	}
    	
    	public static void main(String[] args){ 
    		new InterfaceThree();
    	}
    }
    Which will out put this:
    Last edited by John; 01-11-2007 at 01:10 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 2 users browsing this thread. (0 members and 2 guests)

Similar Threads

  1. Replies: 3
    Last Post: 06-19-2010, 06:00 AM
  2. Replies: 5
    Last Post: 06-13-2010, 06:55 PM
  3. Video Tutorial on using Button Control VB
    By gokuajmes in forum Visual Basic Tutorials
    Replies: 0
    Last Post: 03-17-2010, 07:59 AM
  4. VB.net Button Auticlick Tutorial
    By iluxon4ik in forum Video Tutorials
    Replies: 0
    Last Post: 01-31-2010, 07:19 AM
  5. Beginner ?: Why won't the submit button work?
    By jackolantern in forum PHP Development
    Replies: 3
    Last Post: 07-24-2009, 08:59 AM

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