Results 1 to 7 of 7
  1. #1
    S-NESH is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Exclamation Needed help in actionPerformed statements

    Question:



    Here's the code that I have done:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class GUI extends JFrame implements ActionListener
    {
    	JButton btn1;
    	JButton btn2;
    	JButton btn3;
    	JTextArea area1;
    
    	GUI()
    	{
    		setLayout (new BorderLayout());
    
    		btn1 = new JButton ("Elephant");
    		btn2 = new JButton ("Mickey");
    		btn3 = new JButton ("Butterfly");
    		area1 = new JTextArea ("");
    
    		JPanel p1 = new JPanel();
    		JPanel p2 = new JPanel();
    
    
    		p1.add (btn1);
    		p1.add (btn2);
    		p1.add (btn3);
    		p2.add (area1);
    
    		add(p2, BorderLayout. CENTER);
    		add(p1, BorderLayout. SOUTH);
    	}
    
    		public static void main (String []args)
    		{
    			String [] photos = {"elephant.jpg", "mickey.jpg", "butterly.jpg"};
    
    			GUI t = new GUI();
    
    			t.setSize(500,150);
    			t.setVisible(true);
    			t.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    			t.setTitle("Button and Icons");
    		}
    		public void actionPerformed(ActionEvent evt)
    		{
    			if(evt.getSource() == btn1)
    			{
    				area1.append();
    			}
    		}
    
    }
    Can anyone help me to tell me what should I write at the 'public void actionPerformed(ActionEvent evt)'?? Thanks (=

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The easiest way to swap images is to create ImageIcons with your images and display them in a JLabel. You can swap the JLabel's displayed ImageIcon by calling setIcon on the JLabel. And in fact on looking at your instructions, it appears that the instructor wants you to do just this, create an array of ImageIcons and swap the icons using this array.

  3. #3
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    here's an example on how to load an image icon.
    Java Code:
    import javax.swing.Icon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.ImageIcon;
    public class IconDemo extends JFrame{
        private JLabel label;
        public IconDemo(){
            label = new JLabel();
            Icon icon = new ImageIcon( getClass().getResource("myimage.gif") );
            label.setIcon(icon);
            add(label);
        }
        public static void main(String[] args){
            IconDemo icondemo = new IconDemo();
            icondemo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            icondemo.setSize(260,180);
            icondemo.setVisible(true);
        }
    }
    try to understand and make use of it in your actionPerformed()

  4. #4
    S-NESH is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by JavaHater View Post
    here's an example on how to load an image icon.
    Java Code:
    import javax.swing.Icon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.ImageIcon;
    public class IconDemo extends JFrame{
        private JLabel label;
        public IconDemo(){
            label = new JLabel();
            Icon icon = new ImageIcon( getClass().getResource("myimage.gif") );
            label.setIcon(icon);
            add(label);
        }
        public static void main(String[] args){
            IconDemo icondemo = new IconDemo();
            icondemo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            icondemo.setSize(260,180);
            icondemo.setVisible(true);
        }
    }
    try to understand and make use of it in your actionPerformed()
    How do I display this label when the specific button's clicked? I still needed to use this public void actionPerformed(ActionEvent evt) for any event to occured right?

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Suggested reading: How to Write an ActionListener, in particular the description of how to add action listeners to components such as JButtons
    Last edited by doWhile; 01-07-2011 at 04:00 AM.

  6. #6
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by S-NESH View Post
    How do I display this label when the specific button's clicked? I still needed to use this public void actionPerformed(ActionEvent evt) for any event to occured right?
    Java Code:
           .........
           add(p2, BorderLayout. CENTER);
    	add(p1, BorderLayout. SOUTH);
        
            ButtonHandler handler = new ButtonHandler(); //create for button handling
            button.addActionListener ( handler ); //add the listener
           ....
        }
        //create inner class for button event handling 
        private class ButtonHandler implements ActionListener {
            public void actionPerformed ( ActionEvent e ){
    			if(evt.getSource() == btn1)
    			{
    				            // load your image here
    			} else if ( ) {
    
                            }
            }
        }

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Cross posted: Java Programming Forums

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Help with actionPerformed
    By mayhewj7 in forum New To Java
    Replies: 8
    Last Post: 02-10-2009, 06:45 PM
  2. trouble with actionPerformed
    By diggitydoggz in forum New To Java
    Replies: 2
    Last Post: 12-26-2008, 02:18 AM
  3. Help with actionPerformed Statements
    By wco5002 in forum New To Java
    Replies: 8
    Last Post: 03-26-2008, 04:02 AM
  4. actionPerformed problem
    By tomitzel in forum New To Java
    Replies: 1
    Last Post: 01-08-2008, 06:10 PM
  5. Problems with jButton ActionPerformed
    By susan in forum AWT / Swing
    Replies: 3
    Last Post: 08-07-2007, 04:19 AM

Posting Permissions

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