Wszystko się kompiluje i działa. Jednak podczas wielokrotnego używania przycisków (wchodzenia i wychodzenia do innych opcji) program zaczyna zwalniać i zużywa coraz więcej procesora.
Zastanawiam się czy poprawnie tu zrobiłem obsługę zdarzeń i tworzyłem dla każdego JButtona oddzielnego "Słuchacza" poprzez oddzielną klasę.
class OknoGlowne extends JFrame{
private JButton SinglePlayerButton=new JButton("Single Player");
private JButton MultiPlayerButton=new JButton("Multi Player");
private JButton InstructionsButton=new JButton("Instructions");
private JButton HighScoresButton=new JButton("High Scores");
private JButton ExitButton=new JButton("Exit");
private JPanel GlownyPanel=new JPanel();
private JPanel MenuPanel=new JPanel();
public JPanel IstructionsPanel=new JPanel();
private JPanel PanelPrzycisk1=new JPanel();
private JPanel PanelPrzycisk2=new JPanel();
private JPanel PanelPrzycisk3=new JPanel();
private JPanel PanelPrzycisk4=new JPanel();
private JPanel PanelPrzycisk5=new JPanel();
private KlasaObrazekGornyPanel ObrazekGornyPanel=new KlasaObrazekGornyPanel("asteroids.gif");
private SluchaczInstructions NasluchiwaczInstructions=new SluchaczInstructions();
private SluchaczSinglePlayer NasluchiwaczSinglePlayer=new SluchaczSinglePlayer();
private SluchaczMultiPlayer NasluchiwaczMultiPlayer=new SluchaczMultiPlayer();
private SluchaczHighScores NasluchiwaczHighScores=new SluchaczHighScores();
private SluchaczExit NasluchiwaczExit=new SluchaczExit();
Color KolorTla=new Color(0,174,239);
Color KolorCzcionkiPrzycisku=Color.WHITE;
int RozmiarTextuNaJButton=19;
OknoGlowne(String NazwaOkna)
{
super(NazwaOkna);
}
public void pokaz()
{
GlownyPanel.removeAll();
Toolkit InfoOSystemieGraficznym=Toolkit.getDefaultToolkit();
Dimension RozmiarEkranuMonitora=InfoOSystemieGraficznym.getScreenSize();
//Dimension RozmiarButton=new Dimension();
//RozmiarButton.setSize(200, 100);
//Rozmiar okna aplikacji
setSize(RozmiarEkranuMonitora.width/2,RozmiarEkranuMonitora.height/2);
//Polozenie na ekranie
setLocation(RozmiarEkranuMonitora.width/4, RozmiarEkranuMonitora.height/4);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GlownyPanel.setLayout(new BorderLayout());
MenuPanel.setLayout(new GridLayout(5,1));
//setMaximumSize(RozmiarButton);
//SinglePlayerButton.setMinimumSize(RozmiarButton);
SinglePlayerButton.addActionListener(NasluchiwaczSinglePlayer);
MultiPlayerButton.addActionListener(NasluchiwaczMultiPlayer);
HighScoresButton.addActionListener(NasluchiwaczHighScores);
InstructionsButton.addActionListener(NasluchiwaczInstructions);
ExitButton.addActionListener(NasluchiwaczExit);
SinglePlayerButton.setFont(new Font(null, Font.BOLD, RozmiarTextuNaJButton) );
MultiPlayerButton.setFont(new Font(null, Font.BOLD, RozmiarTextuNaJButton) );
InstructionsButton.setFont(new Font(null, Font.BOLD, RozmiarTextuNaJButton) );
HighScoresButton.setFont(new Font(null, Font.BOLD, RozmiarTextuNaJButton) );
ExitButton.setFont(new Font(null, Font.BOLD, RozmiarTextuNaJButton) );
SinglePlayerButton.setBackground(KolorTla);
SinglePlayerButton.setForeground(KolorCzcionkiPrzycisku);
MultiPlayerButton.setBackground(KolorTla);
MultiPlayerButton.setForeground(KolorCzcionkiPrzycisku);
InstructionsButton.setBackground(KolorTla);
InstructionsButton.setForeground(KolorCzcionkiPrzycisku);
HighScoresButton.setBackground(KolorTla);
HighScoresButton.setForeground(KolorCzcionkiPrzycisku);
ExitButton.setBackground(KolorTla);
ExitButton.setForeground(KolorCzcionkiPrzycisku);
PanelPrzycisk1.add(SinglePlayerButton);
PanelPrzycisk2.add(MultiPlayerButton);
PanelPrzycisk3.add(InstructionsButton);
PanelPrzycisk4.add(HighScoresButton);
PanelPrzycisk5.add(ExitButton);
PanelPrzycisk1.setBackground(KolorTla);
PanelPrzycisk2.setBackground(KolorTla);
PanelPrzycisk3.setBackground(KolorTla);
PanelPrzycisk4.setBackground(KolorTla);
PanelPrzycisk5.setBackground(KolorTla);
MenuPanel.add(PanelPrzycisk1);
MenuPanel.add(PanelPrzycisk2);
MenuPanel.add(PanelPrzycisk3);
MenuPanel.add(PanelPrzycisk4);
MenuPanel.add(PanelPrzycisk5);
ObrazekGornyPanel.setBackground(KolorTla);
GlownyPanel.add(BorderLayout.CENTER,MenuPanel);
GlownyPanel.add(BorderLayout.NORTH,ObrazekGornyPanel);
GlownyPanel.setBackground(KolorTla);
setBackground(KolorTla);
add(GlownyPanel);
repaint();
setVisible(true);
}
class SluchaczSinglePlayer implements ActionListener
{
TextWPanelu TextInstructionsNaglowek=new TextWPanelu("Single player");
PanelWithButtonPowrot PowrotPanButton=new PanelWithButtonPowrot();
public void actionPerformed(ActionEvent zdarzenie)
{
Graphics g;
GlownyPanel.setVisible(false);
GlownyPanel.removeAll();
GlownyPanel.setLayout(new GridLayout(3,1));
GlownyPanel.add(TextInstructionsNaglowek);
PowrotPanButton.ustaw();
GlownyPanel.add(PowrotPanButton);
GlownyPanel.setVisible(true);
}
}
class SluchaczMultiPlayer implements ActionListener
{
TextWPanelu TextInstructionsNaglowek=new TextWPanelu("MultiPlayer");
PanelWithButtonPowrot PowrotPanButton=new PanelWithButtonPowrot();
public void actionPerformed(ActionEvent zdarzenie)
{
Graphics g;
GlownyPanel.setVisible(false);
GlownyPanel.removeAll();
GlownyPanel.setLayout(new GridLayout(3,1));
GlownyPanel.add(TextInstructionsNaglowek);
PowrotPanButton.ustaw();
GlownyPanel.add(PowrotPanButton);
GlownyPanel.setVisible(true);
}
}
class SluchaczHighScores implements ActionListener
{
TextWPanelu TextInstructionsNaglowek=new TextWPanelu("HighScores");
PanelWithButtonPowrot PowrotPanButton=new PanelWithButtonPowrot();
public void actionPerformed(ActionEvent zdarzenie)
{
Graphics g;
GlownyPanel.setVisible(false);
GlownyPanel.removeAll();
GlownyPanel.setLayout(new GridLayout(3,1));
GlownyPanel.add(TextInstructionsNaglowek);
PowrotPanButton.ustaw();
GlownyPanel.add(PowrotPanButton);
GlownyPanel.setVisible(true);
}
}
class SluchaczInstructions implements ActionListener
{
TextWPanelu TextInstructionsNaglowek=new TextWPanelu("Instructions");
TextWPanelu TextInstructions=new TextWPanelu("Klawiszologia \n" +
"strzalki sterowanie" +
"spacja strzelanie");
PanelWithButtonPowrot PowrotPanButton=new PanelWithButtonPowrot();
public void actionPerformed(ActionEvent zdarzenie)
{
Graphics g;
GlownyPanel.setVisible(false);
GlownyPanel.removeAll();
GlownyPanel.setLayout(new GridLayout(3,1));
GlownyPanel.add(TextInstructionsNaglowek);
GlownyPanel.add(TextInstructions);
PowrotPanButton.ustaw();
GlownyPanel.add(PowrotPanButton);
GlownyPanel.setVisible(true);
}
}
class SluchaczExit implements ActionListener
{
TextWPanelu TextInstructionsNaglowek=new TextWPanelu("Exit");
PanelWithButtonPowrot PowrotPanButton=new PanelWithButtonPowrot();
public void actionPerformed(ActionEvent zdarzenie)
{
Graphics g;
GlownyPanel.setVisible(false);
GlownyPanel.removeAll();
GlownyPanel.setLayout(new GridLayout(3,1));
GlownyPanel.add(TextInstructionsNaglowek);
PowrotPanButton.ustaw();
GlownyPanel.add(PowrotPanButton);
GlownyPanel.setVisible(true);
}
}
/** Klasa dziedzicząca po JPanel z przyciskiem Back i obsluga zdarzenia (powrót do menu glównego)
*/
class PanelWithButtonPowrot extends JPanel
{
JButton PowrotButton=new JButton("Back");
NasluchiwaczBackButton SluchaczBackButton=new NasluchiwaczBackButton();
public void ustaw()
{
PowrotButton.setFont(new Font(null, Font.BOLD,RozmiarTextuNaJButton) );
PowrotButton.setBackground(KolorTla);
PowrotButton.setForeground(KolorCzcionkiPrzycisku);
PowrotButton.addActionListener(SluchaczBackButton);
add(PowrotButton);
}
class NasluchiwaczBackButton implements ActionListener
{
public void actionPerformed(ActionEvent zdarzenie)
{
pokaz();
}
}
}
}
class TextWPanelu extends JPanel
{
Toolkit InfoOSystemieGraficznym=Toolkit.getDefaultToolkit();
Dimension RozmiarEkranuMonitora=InfoOSystemieGraficznym.getScreenSize();
String Text;
TextWPanelu(String str)
{
Text=str;
}
@Override
public Dimension getPreferredSize() {
Dimension Size=new Dimension();
Size.setSize(200, 100);
return Size;
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString(Text, 10,10);
}
}
class KlasaObrazekGornyPanel extends JPanel
{
private Image obrazek;
public KlasaObrazekGornyPanel(String nazwa)
{
obrazek=Toolkit.getDefaultToolkit().getImage(nazwa);
MediaTracker trop=new MediaTracker(this);
trop.addImage(obrazek, 0);
try{trop.waitForID(0);
}
catch(InterruptedException exception){}
}
@Override
public Dimension getPreferredSize() {
Dimension Size=new Dimension();
Size.setSize(200, 100);
return Size;
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//Toolkit InfoOSystemieGraficznym=Toolkit.getDefaultToolkit();
//Dimension RozmiarEkranuMonitora=InfoOSystemieGraficznym.getScreenSize();
int SzerokoscCała= this.getSize().width;
int SzerObrazka=obrazek.getWidth(this);
g.drawImage(obrazek,(SzerokoscCała-SzerObrazka)/2, 0, null);
}
}
public class Main
{
public static void main(String[] args) {
OknoGlowne NoweOkno=new OknoGlowne("Asteroids");
NoweOkno.pokaz();
}
}