Witam,
Podjąłem się napisania gry w statki.
Hierarchia u mnie wyglada tak: Mam MenuPanel(z 3 buttonami - kazdy ma przechodzic na inny panel[oprócz exit, on mi ladnie wychodzi z gry] button - play(jeszcze nie implementowany) oraz button "Informations" w ktorym chcialem zawrzec pare informacji)), InformationPanel, ktory zawiera Panel z labelem(zeby byl backgroundz z obrazka :) ). Frame - klasa ramki, oraz klase Run w ktorej wywoluje moja new Runnable i powoluje do zycia frame :)
Problem pojawia sie kiedy buttonowi "informations" dodaje actionlistenera. przeciazam metode actionperformed tak, zeby po wcisnieciu buttona "informations" przeskakiwało mi na InformationPanel, gdzie jest label z obrazkiem i informacjami ;) nie moge sobie z tym poradzic. Oto kody do poszczegolnych klas:
MenuPanel
package game;
import javax.swing.JPanel;
public class MenuPanel extends JPanel
{
public MenuPanel()
{
setBackground(UIManager.getColor("Tree.selectionBackground"));
final InformationPanel informationPanel = new InformationPanel();
JButton btnExit = new JButton("Exit");
btnExit.setFont(new Font("Viner Hand ITC", Font.BOLD, 16));
btnExit.setBounds(115, 251, 118, 60);
btnExit.setForeground(new Color(0, 0, 139));
btnExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
setLayout(null);
add(btnExit);
JButton btnPlay = new JButton("Play");
btnPlay.setFont(new Font("Viner Hand ITC", Font.BOLD, 17));
btnPlay.setBounds(115, 34, 118, 60);
btnPlay.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// tu bedzie implementacja menu z textfieldem do wpisania IP gracza z ktorym bedzie prowadzona rozgrywka
}
});
btnPlay.setForeground(new Color(139, 0, 139));
add(btnPlay);
JButton btnInformations = new JButton("Informations");
btnInformations.addActionListener(new ActionListener() // --------------------------------------------- information button
{
public void actionPerformed(ActionEvent arg0)
{
informationPanel.setVisible(true);
repaint();
}
});
btnInformations.setFont(new Font("Viner Hand ITC", Font.BOLD, 11));
btnInformations.setBounds(115, 140, 118, 60);
btnInformations.setForeground(new Color(72, 209, 204));
add(btnInformations);
JLabel lblNewLabel = new JLabel("TheShips");
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\Power\\Desktop\\ship.jpg"));
lblNewLabel.setBounds(0, 0, 800, 600);
add(lblNewLabel);
}
}
InformationPanel
package game;
import javax.swing.JPanel;
public class InformationPanel extends JPanel
{
/**
* Create the panel.
*/
public InformationPanel()
{
JLabel lblNewLabel = new JLabel("The Ships - Informations");
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\Power\\Desktop\\information.jpg"));
add(lblNewLabel);
}
}
Frame
package game;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Frame extends JFrame
{
public Frame()
{
super("The Ships - Menu");
JPanel menuPanel = new MenuPanel();
JPanel informationPanel = new InformationPanel();
setPreferredSize(new Dimension(800, 600));
add(informationPanel);
add(menuPanel);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
Klasa Run wyglada standardowo. EventQueue,newRunnable,new Frame();