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:
Code:
Card card = new Card();
card.Card();
- 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:
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);
}
}
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:
Code:
public static void main() {
}
instead of:
Code:
public static void main(String[] args) {
}
writing
Code:
Card card = new Card();
card.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?
Re: Calling a method in another class
Thanks for your answer
I'm trying to instance but i have an error too
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;
}
After instance i think expect something, iv'e tried to put (aluno, id) and i have another error saying can't find symbol "id"
I'm lost...