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:
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:
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.
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)
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.
Re: Using a Button From Another Class
Quote:
Originally Posted by
Shane457
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
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