Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: problems with jbutton/actionlistener

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problems with jbutton/actionlistener

    hi.. i am new to gui and i have some trouble.
    i'm trying to create button that change background color to random color.
    this is what i have done:

    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.util.Random;
    import javax.swing.*;
     
     
    public class Mainp extends JFrame 
    {	
    private ImageIcon image1;
    private JLabel label1;
    private JButton startbutton;
     
     
    public void rndbackground()
    {
    Random rand = new Random();
    float r = rand.nextFloat();
    float g = rand.nextFloat();
    float b = rand.nextFloat();
    Color randomcolor=new Color(r,g,b);
    getContentPane().setBackground(randomcolor); 
    }
     
    public void creation() 
    {
    	setLayout(new FlowLayout());
    	image1= new ImageIcon(getClass().getResource("sun_2.png"));
    	label1=new JLabel(image1);
    	startbutton = new JButton("Start");
    	add(label1);
    	add(startbutton);
    }
     
    Mainp()
    {
    creation();
    rndbackground();
    }
     
    public static void main(String[] args)
    {
    Mainp gui= new Mainp();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setVisible(true);
    gui.pack();
    gui.setTitle("title");
    }
    }


    when i change
    public class Mainp extends JFrame
    to
    public class Mainp extends JFrame implements ActionListener
    it make errors and doesn't recognize it.. what to do?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: problems with jbutton/actionlistener

    Quote Originally Posted by nirro View Post
    it make errors and doesn't recognize it.. what to do?
    Always include the full text of the error message with your code and question

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problems with jbutton/actionlistener

    Description Resource Path Location Type
    The serializable class Mainp does not declare a static final serialVersionUID field of type long Mainp.java ‪/Gui-test/src‬ line 13 Java Problem
    The import java.awt.event.ActionEvent is never used Mainp.java ‪/Gui-test/src‬ line 8 Java Problem
    The import java.awt.event.ActionListener is never used Mainp.java ‪/Gui-test/src‬ line 7 Java Problem

    how can i make the button start rndbackground()?

  4. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: problems with jbutton/actionlistener

    Hello.
    You need to learn about ActionListener in java.
    Using this listener you connect your button to the method which sets the background.
    A post is already available in the forum I think regarding this. Just check it out. http://www.javaprogrammingforums.com...ton-swing.html

    Syed.

  5. #5
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: problems with jbutton/actionlistener


  6. The Following User Says Thank You to PhHein For This Useful Post:

    jps (September 6th, 2013)

  7. #6
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: problems with jbutton/actionlistener

    Is Mainp the name of your primary class (your program)? You don't implement Action Listener on that. You have to create an inner class, a listener class, and implement on that. Something like this... although I can't remember the precise syntax off the top of my head and I don't have time right now to look it up.

    public class main_program extends JFrame
    {
       public static void main(String[] args1)
         {
                ...statements to execute when the program first runs...
         }
     
        class HandleButtonClicks implements ActionListener
           {
           public void actionPerformed(actionEvent e)
               {
                  ....statements to execute when a button is clicked...
                }
            }
     
    }

  8. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: problems with jbutton/actionlistener

    Quote Originally Posted by nirro View Post
    The serializable class Mainp does not declare a static final serialVersionUID field of type long
    Declare the missing variable as the warning states: a static final variable of type long with the name serialVersionUID, and give it a value. Search keyword serialVersionUID for details on what it is for.

    Quote Originally Posted by nirro View Post
    The import java.awt.event.ActionEvent is never used Mainp.java ‪/Gui-test/src‬ line 8 Java Problem
    The import java.awt.event.ActionListener is never used Mainp.java ‪/Gui-test/src‬ line 7 Java Problem
    These two imports are not used, so either remove them or use them.
    You said when you add "implements ActionListener" you get an error... implement ActionListener like you need to, and post that error if you need help with it. Follow the tutorial suggestion in post #4 or this one here.

    Also see this post on cross posting

Similar Threads

  1. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Swing Tutorials
    Replies: 17
    Last Post: April 24th, 2013, 05:14 PM
  2. Adding an actionlistener to a JButton with no text
    By captain in forum AWT / Java Swing
    Replies: 1
    Last Post: April 5th, 2012, 02:20 PM
  3. Error when adding ActionListener to JButton
    By grimrader22 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2011, 06:53 AM
  4. JButton.... actionListener.... help!!!!....
    By eiramae in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2011, 05:54 AM
  5. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 15
    Last Post: February 10th, 2011, 02:21 AM