Results 1 to 10 of 10
- 08-03-2007, 02:48 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 13
- Rep Power
- 0
Calling a method in another class
This must be a classic 'if I had a dollar' kind of question, but I'm confused about calling methods in other classes. The problem snippet is:
- which to the best of my knowledge should work, but doesn't. What I want to do, for the purpose of experimenting with CardLayout, is initiate the Card Layout tutorial (in the class Card) from another class by calling the method Card();. Here's the whole thing:Java Code:Card card = new Card(); card.Card();
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Menu { public static void main(String[] args) { JFrame frame = new JFrame("Menu"); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.setSize(1200,800); frame.setVisible(true); } Card card = new Card(); card.main(); } class Card extends JPanel { CardLayout cards = new CardLayout(); public Card() { setLayout(cards); ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { cards.next(Card.this); } }; JButton button; button = new JButton("one"); button.addActionListener(listener); add(button, "one"); button = new JButton("two"); button.addActionListener(listener); add(button, "two"); button = new JButton("three"); button.addActionListener(listener); add(button, "three"); } public static void main(String[] args) { JFrame frame = new JFrame("Card"); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.setSize(200, 200); frame.setLocation(200, 200); frame.setContentPane(new Card()); frame.setVisible(true); } }Last edited by levent; 08-03-2007 at 03:16 PM. Reason: Code placed inside [code] tag.
- 08-03-2007, 03:14 PM #2
Member
- Join Date
- Jul 2007
- Posts
- 41
- Rep Power
- 0
1) are both classes in the same package?
2) you have to write
in the main of the menu classJava Code:Card card = new Card(); card.main();
- 08-03-2007, 03:19 PM #3levent Guest
Hi uncopywritable,
Please use [code] tag to wrap your codes. It makes the code much more readable and it is very easy to use!
And i did not understand what you really want. As far as i see your card class does not have a Card() method.
- 08-03-2007, 04:33 PM #4
Member
- Join Date
- Jul 2007
- Posts
- 13
- Rep Power
- 0
Re: Calling a method in another class
First off, gabriel - I tried your suggestion and it works if I declare the main method of Card:
instead of:Java Code:public static void main() { }
writingJava Code:public static void main(String[] args) { }
in the main of Menu doesn't work! It would be great if I could activate methods regardless of the argument they take. Is there a way to do this?Java Code:Card card = new Card(); card.main(String[] args);
- 08-03-2007, 04:38 PM #5
Member
- Join Date
- Jul 2007
- Posts
- 13
- Rep Power
- 0
Sorry levent, I just didn't realise about the tags, my browser seems to find the code and tag it anyway, which is odd.
The method Card() is in the class Card and goes like this:
All my attempts to call this method have failed.Java Code:public Card() { setLayout(cards); ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { cards.next(Card.this); } }; JButton button; button = new JButton("one"); button.addActionListener(listener); add(button, "one"); button = new JButton("two"); button.addActionListener(listener); add(button, "two"); button = new JButton("three"); button.addActionListener(listener); add(button, "three"); }
- 08-03-2007, 04:53 PM #6levent Guest
But it is constructor, not a method. You should at least add void keyword in front of the method name to make it a method.All my attempts to call this method have failed.
- 08-03-2007, 05:01 PM #7
Member
- Join Date
- Jul 2007
- Posts
- 13
- Rep Power
- 0
Ah. Right. I was confused between the class declaration and the constructor.
Thanks for pointing that out! :)
- 01-14-2008, 12:10 PM #8
Member
- Join Date
- Jan 2008
- Posts
- 1
- Rep Power
- 0
Hello
My self anuj , I will see how I am on this forum
- 01-14-2008, 04:37 PM #9
Welcome anuj. Please do not revive old threads unless you have something substantial to add, notifying everyone that you're on this forum by posting to an old thread disqualifies as a substantial attribute. Can I have my minute and fifteen seconds back please?
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 10-22-2012, 04:01 PM #10
Member
- Join Date
- Jan 2012
- Posts
- 27
- Rep Power
- 0
Re: Calling a method in another class
Thanks for your answer
I'm trying to instance but i have an error too
After instance i think expect something, iv'e tried to put (aluno, id) and i have another error saying can't find symbol "id"Java Code:public boolean addAluno(Aluno aluno) { boolean estado = false; Aluno t= new Aluno(); // found no arguments --- t.getID(); for(int i=0; i<alunos.size(); i++) { } alunos.add(aluno); return true; }
I'm lost...
Similar Threads
-
Dynamic method calling
By Java Tip in forum Java TipReplies: 0Last Post: 02-15-2008, 08:46 AM -
method calling?
By frejon26 in forum New To JavaReplies: 4Last Post: 01-25-2008, 03:38 AM -
Calling method from another class
By asahli in forum New To JavaReplies: 1Last Post: 12-15-2007, 06:24 PM -
Help with Calling a method
By Albert in forum New To JavaReplies: 3Last Post: 07-10-2007, 03:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks