Results 1 to 4 of 4
  1. #1
    Shane457 is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Using a Button From Another Class

    Hello everyone. I'm very new to Java. I recently started just a month ago. I was having this problem with my code and I would greatly appreciate some help.
    My problem is that I'm trying to create a racing game where when you click the move button a line moves (or changes coordinates). However, my move button is in the GUI Window class
    and I want to bring it to the Race class to use it. Here is my code:

    NOTE - There are no errors in either class but I am unsure of how to go about doing this.

    GUI Window Class:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class GUIWindow{
        public static void main(String [] args){
            JFrame theGUI = new JFrame();
            JButton move = new JButton("Move");
            theGUI.setTitle("GUI Program");
            theGUI.setSize(400,400);
            theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Race pane1 = new Race(Color.white);
            Container pane = theGUI.getContentPane();
            pane.add(pane1);
            pane1.add(move);
            theGUI.setVisible(true);
        }
    }
    Race Class:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Race extends JPanel implements ActionListener {
            
           public Race(Color backColor){
                setBackground(backColor);
        }
        
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            
            //make line 1
            g.setColor(Color.black);
            g.drawLine(50,100,100,100);
        }
        
        public void actionPerformed(ActionEvent e) {
            
        }
    }
    So my two main questions are : 1. How would I bring the button into the Race class? 2. How would I use the Action Listener so that when I click the move button, the lines coordinates change?

    I know this is probably something simple, but I really could use some help and ,like I said, I am very new to Java.

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

    Default Re: Using a Button From Another Class

    1) Define 'bring the button into the Race class'...you can add the JButton to the Race JPanel in your main method, or have the Race JPanel contain a JButton variable and add it to the JPanel in the constructor.
    2) add the appropriate ActionListener to the JButton, and define its method body which would set the coordinates appropriately and repaint the JPanel (the coordinates would be variables, not hard-coded values that they are now). See How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

  3. #3
    Shane457 is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Re: Using a Button From Another Class

    Can you please provide an example of how to add the JButton to the Race JPanel? I'm not sure how to do that. And also I don't understand when you say "define its method body" in #2. Sorry, I know these are very simple questions.

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

    Default Re: Using a Button From Another Class

    Quote Originally Posted by Shane457 View Post
    Can you please provide an example of how to add the JButton to the Race JPanel?
    You did not define what you mean, or what the requirements are. It is hard to answer a question that is ill defined without guessing, and guessing can be quite unproductive.

    Quote Originally Posted by Shane457 View Post
    I'm not sure how to do that. And also I don't understand when you say "define its method body" in #2. Sorry, I know these are very simple questions.

    Write the necessary code in the actionPerformed method. You have an actionPerformed method in the class already, but it is empty. You need to provide the necessary code, and my post above should hopefully lean you in the right direction. There are also examples in the link I provided above

Similar Threads

  1. Replies: 5
    Last Post: 02-25-2012, 03:56 AM
  2. pressing a button making a instance in another class
    By SpicyElectricity in forum New To Java
    Replies: 3
    Last Post: 02-18-2012, 10:48 PM
  3. Make a button class that uses your button image.
    By eLancaster in forum New To Java
    Replies: 1
    Last Post: 04-26-2011, 11:32 AM
  4. How to call a class when push Button?
    By Lund01 in forum New To Java
    Replies: 3
    Last Post: 03-17-2011, 06:38 AM
  5. calling a public void method from a class button
    By supa_kali_frajilistik in forum AWT / Swing
    Replies: 4
    Last Post: 05-23-2008, 01:05 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
  •