Results 1 to 5 of 5
  1. #1
    mbschutlz97 is offline Member
    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    0

    Default how do i make a button that will change the label?

    How can i code this differently so that when i click one of the button it changes the image to the red.jpg or green.jpg?

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    public class GUI{
    	public static void main(String[] args){
    		JFrame window = new JFrame("Test");
    		window.setSize(600,400);
    		window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    		window.setVisible(true);
    		
    		JPanel panel = new JPanel();
    		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    		JPanel panel2 = new JPanel();
    		JPanel panel3 = new JPanel();
    		panel.add(panel2);
    		panel.add(panel3);
    		
    		ImageIcon red = new ImageIcon("/images/red.jpg");
    		ImageIcon green = new ImageIcon("/images/green.jpg");
    		
    		JLabel label = new JLabel("Made by: Matthew Schultz",SwingConstants.CENTER);
    		JLabel label2 = new JLabel();
    		window.add(label,BorderLayout.NORTH);
    		
    		JButton button = new JButton("red");
    		JButton button2 = new JButton("blue");
    		button.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    			label2.setIcon(red);
    			}
    		});
    		button2.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    			label2.setIcon(green);
    			}
    		});
    		
    		panel2.add(button);
    		panel2.add(button2);
    		
    		window.add(panel);
    	}
    }
    errors:

    GUI.java:30: local variable red is accessed from within inner class; needs to be declared final
    label2.setIcon(red);
    ^
    GUI.java:30: local variable label2 is accessed from within inner class; needs to be declared final
    label2.setIcon(red);
    ^
    GUI.java:35: local variable green is accessed from within inner class; needs to be declared final
    label2.setIcon(green);
    ^
    GUI.java:35: local variable label2 is accessed from within inner class; needs to be declared final
    label2.setIcon(green);
    ^
    4 errors

  2. #2
    mbschutlz97 is offline Member
    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    0

    Default Re: how do i make a button that will change the label?

    when i take out the actionlisteners, compile it, and run it i don't see the label or the buttons.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,984
    Rep Power
    9

    Default Re: how do i make a button that will change the label?

    Did you try googling the error? You're using an inner class, which means you can't access a local variable from the outer class inside the inner class- unless that local variable is declared final.

    Your options are to make the variables final or to pass them into a non-anonymous class.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    mbschutlz97 is offline Member
    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    0

    Default Re: how do i make a button that will change the label?

    Quote Originally Posted by KevinWorkman View Post
    Did you try googling the error? You're using an inner class, which means you can't access a local variable from the outer class inside the inner class- unless that local variable is declared final.

    Your options are to make the variables final or to pass them into a non-anonymous class.
    if i make them outside of the main method will i be able to use them inside an inner class?

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,984
    Rep Power
    9

    Default Re: how do i make a button that will change the label?

    Quote Originally Posted by mbschutlz97 View Post
    if i make them outside of the main method will i be able to use them inside an inner class?
    What happened when you tried that?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 0
    Last Post: 05-02-2013, 09:19 AM
  2. Using scrollBars to change the color of a label
    By aortell24 in forum New To Java
    Replies: 4
    Last Post: 06-07-2012, 03:46 AM
  3. Replies: 5
    Last Post: 05-21-2012, 10:33 AM
  4. unicode ok for text pane but not for button label
    By madroadbiker in forum Advanced Java
    Replies: 6
    Last Post: 06-01-2011, 05:58 PM
  5. can we change label from action class?
    By kishan in forum Advanced Java
    Replies: 2
    Last Post: 04-28-2009, 02:12 PM

Posting Permissions

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