Cześć, mam pytanie. Jaki jest najprostszy sposób aby po naciśnięciu przycisku można było przejść do 2 okna ? Stworzyłem klasę Menu w której mam 3 buttony i jeden z nich to "control", zrobiłem też klasę Control ale nie wiem jak teraz się do niej odwołać, żeby w ActionPerformed można było do niej przejść. Czy da się to jakoś połączyć ? Stworzyć panele czy coś podobnego ?
public class Menu extends JFrame implements ActionListener
{
private JLabel background;
private JButton startGame, control, exit;
public Menu()
{
setSize(1000, 600);
setTitle("Pocket Tanx");
setLayout(null);
startGame = new JButton("Start Game");
startGame.setBounds(450, 400, 100, 30);
setLayout(null);
add(startGame);
startGame.addActionListener(this);
control = new JButton("Control");
control.setBounds(450, 440, 100, 30);
add(control);
control.addActionListener(this);
exit = new JButton("Exit");
exit.setBounds(450, 480, 100, 30);
add(exit);
exit.addActionListener(this);
background = new JLabel(new ImageIcon("images/background.jpg"));
background.setOpaque(true);
background.setBounds(0, 0, 1000, 600);
add(background);
}
public void actionPerformed(ActionEvent ae)
{
Object source = ae.getSource(); //sprawdza który przycisk nacisneliśmy
if (source == startGame)
{
} else if (source == control)
{
} else if (source == exit)
{
dispose();
}
}
public static void main(String[] args)
{
Menu frame = new Menu();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
i klasa Control
public class Control extends JFrame
{
public Control()
{
setSize(1000, 600);
setTitle("Control");
}
public static void main (String[] args)
{
Control frame2 = new Control();
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setVisible(true);
}
}
Nie wiem czy powinny być 2 funkcje main, w sumie pewnie jest trochę błędów ale liczę ze je znajdziecie
I teraz potrzebuję po nacisnięciu buttona "Control" przeskoczyć do 2 okna w kótrym też musi znaleźć się button " back " i wrócić do pierwszego ;)