Results 1 to 4 of 4
- 05-23-2012, 01:50 AM #1
Member
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
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:
Race 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); } }
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?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) { } }
I know this is probably something simple, but I really could use some help and ,like I said, I am very new to Java.
- 05-23-2012, 01:55 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
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)
- 05-23-2012, 02:02 AM #3
Member
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
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.
- 05-23-2012, 03:58 AM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Re: Using a Button From Another Class
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.
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
-
GUI Passing JTextField value to Logic class upon Button Click
By Redefine12 in forum New To JavaReplies: 5Last Post: 02-25-2012, 03:56 AM -
pressing a button making a instance in another class
By SpicyElectricity in forum New To JavaReplies: 3Last Post: 02-18-2012, 10:48 PM -
Make a button class that uses your button image.
By eLancaster in forum New To JavaReplies: 1Last Post: 04-26-2011, 11:32 AM -
How to call a class when push Button?
By Lund01 in forum New To JavaReplies: 3Last Post: 03-17-2011, 06:38 AM -
calling a public void method from a class button
By supa_kali_frajilistik in forum AWT / SwingReplies: 4Last Post: 05-23-2008, 01:05 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks