Witam, chciałbym, stworzyć grę z możliwością wyboru różnych opcji (przeskakiwanie z okienka na okienko). W tym celu posłużyłem się cardlayout. Ale chcę dodać w pewnym momencie planszę gry z keylistener, która będzie wyskakiwała jako jwindow. (Próbowałem też bez tego, zwyczajnie wsadzić w cardlayout, ale tak to nie działa). Nie mam już pomysłu, a jest to ostatni etap, tego "nudniejszego" pisania programu. Później pozostaną mi tylko opcje gry, co już jest przyjemniejsze w pisaniu. Daję kod źródłowy:
public void x()
{
proba1 proba = new proba1();
setTitle("Gra");
setSize(300, 150);
gra = new JPanel();
cl = new CardLayout();
gra.setLayout(cl);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JLabel lab1 = new JLabel("Card1");
JLabel lab2 = new JLabel("Card2");
p1.add(lab1);
p2.add(lab2);
gra.add(p1, "1");
gra.add(p2, "2");
JPanel buttonPanel = new JPanel();
JButton b1 = new JButton("Previous");
JButton b2 = new JButton("Next");
buttonPanel.add(b1);
buttonPanel.add(b2);
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
if (currentCard > 1)
{
currentCard -= 1;
cl.show(gra, "" + (currentCard));
}
}
});
b2.addActionListener (new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
if (currentCard < 4)
{
currentCard += 1;
cl.show(gra, "" + (currentCard));
}
}
});
getContentPane().add(gra, BorderLayout.NORTH);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
}
class proba1 extends JWindow
{
public void proba()
{
teraz teraz = new teraz();
setBounds(60,60,100,100);
setVisible(true);
}
}
public static void main(String[] args)
{
gra gra = new gra();
gra.x();
gra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gra.setVisible(true);
}
Za wszelkie wskazówki serdeczne dzięki.