0

this is a part of the levelselector class. However, when i tried to launch it from the start class, which's the class next, it just get stuck there doing nothing and don't reporting any errors. My tutor also took a look of it and he just said it's weird and can't immediately solve the question... So could someone help? This is the levelselecter class.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class LevelSelectortest implements ActionListener{
private JButton[] S = new JButton[40];
private JButton back = new JButton("Back to menu");
private JPanel levelSelectPanel = new JPanel();
private GridLayout levelSelectLayout = new GridLayout(6, 7);
private Hoppertest h;

public LevelSelectortest(Hoppertest h)
{
    this.h = h;
    levelSelectPanel.setLayout(levelSelectLayout);
    h.getFrame().setContentPane(levelSelectPanel);
}

public void selecting()
{
    for (int i = 0; i <= 39; i++)
    {
        S[i] = new JButton("Level " + i);
        levelSelectPanel.add(S[i]);
        S[i].addActionListener(this);
    }
    levelSelectPanel.add(back);
    back.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
    for (int a = 0; a <= 39; a++)
    {
        if (e.getSource() == S[a])
        {
            h.setLevel(a);
        }
    }
    if (e.getSource() == back)
    {
        h.setStart();
    }
}

}

//this is the start class
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Starttest implements ActionListener{
    private JPanel startPanel = new JPanel();
    private JPanel totalPanel = new JPanel();
    private BorderLayout totalLayout = new BorderLayout();
    private GridLayout startLayout = new GridLayout(1, 3);
    private JButton frogKing = new JButton();
    private JButton startButton = new JButton("Start!");
    private JButton levelSelection = new JButton("Choose a level to play");
    private JButton help = new JButton("How to play the game?");
    private ImageIcon theFrog = new ImageIcon("The Frog King.png");
    private static boolean readable = true;
    private Hoppertest h;

    public Starttest(Hoppertest h)
    {
        this.h = h;
        h.getFrame().setContentPane(totalPanel);
        totalPanel.setLayout(totalLayout);
        totalPanel.add("North", frogKing);
        totalPanel.add("South", startPanel);
        startPanel.setLayout(startLayout);
        frogKing.setPreferredSize(new Dimension(750, 650));
        startPanel.setPreferredSize(new Dimension(750, 50));
        startPanel.add(startButton);
        startPanel.add(levelSelection);
        startPanel.add(help);
        frogKing.setIcon(theFrog);
        startButton.addActionListener(this);
        levelSelection.addActionListener(this);
        help.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == startButton)
        {
            //h.setLevel(1);
        }
        else if (e.getSource() == levelSelection)
        {
            h.setLevelSelector();
        }
        else if (e.getSource() == help && readable == true)
        {
            //do something
        }
    }

}
//this is the main class
import javax.swing.JFrame;
public class Hoppertest {
private JFrame frame = new JFrame();

public Hoppertest()
{
    frame.setTitle("HOOOOOOOOOOOOOOOOOOOOOpers");
    frame.setSize(750, 800);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setStart();
}

public void setStart()
{
    Starttest start = new Starttest(this);
}

public void setLevelSelector()
{
    LevelSelectortest l = new LevelSelectortest(this);
    l.selecting();
}

public void setLevel(int a)
{
    //Board b = new Board(content[a - 1], this);
    //b.levelContent();
}

public JFrame getFrame()
{
    return frame;
}

public static void main(String[] args) 
{
    Hoppertest hopper = new Hoppertest();
}

}

  • When I say "stuck", it's like the frame is freeze there at the start page. The button was clicked virtually, but then I can do nothing about it except close the window. – Albert Yang Mar 19 '20 at 23:25
  • When a Swing program gets "stuck", that suggests that you're somehow calling long-running code on the Swing event thread, blocking that thread from doing its necessary work of drawing the GUI and interacting with the user. I can't tell myself where this might be happening based on the code that you've posted, and it very well can be happening elsewhere in code not shown. Consider creating and posting a valid [mre] with your question, code that we can compile and run and that reproduces the problem for us. This is code small enough to post in your question as code-formatted text. – Hovercraft Full Of Eels Mar 19 '20 at 23:36
  • [tag:j] tag removed. I don't see how on earth this question is related to this – Hovercraft Full Of Eels Mar 19 '20 at 23:36
  • 1
    When changing the state of a JPanel or other container during runtime, you need to call `revalidate()` and then `repaint()` on the container *after* making the changes to tell the GUI to re-layout the new components and remove "dirty" pixels. Even better, use a [CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). – Hovercraft Full Of Eels Mar 20 '20 at 00:31

0 Answers0