0

Good day everyone! I just got back into programming, and I'm facing some problem with the construction of my code. What I'm trying to do is a menu for a future application that could help me and my friends when we play a certain board game.

So right now, I'm having difficulty to figure out how I should structure my code. I have 3 class planned so far. the Main class, which create a JFrame. a MainFrame class which help the main setup the frame, and finally the third class is MainPanel, which display the proper JPanel on the frame depending on the state of the program.

The plan JPanel classes are:

  • a menu Panel, with 3 button on it: New, Load and Exit.
  • a loading Panel, which just display "Loading" while the application load stuff (will be use when someone click New)
  • a loadBoard Panel, which display the list of created board (when someone click Load)

I'm having 2 issue right now:

  • Where do I fit the code to monitor the Key event (such as KeyListener on the frame, so independently of where I am at in the program, the program Exit when I press the Esc key.)
  • How do I make sure the repaint() is handle properly? I have a problem where the Label "Loading" change location when I resize the window.

I know my code ain't construct ideally, that's why I'm posting this thread. I would like to better understand how to setup the code to have a similar result of what I have right now, but well construct.

This is my code:

MAIN Class

package test.project
import java.awt.Dimension;

public class Main 
{
    public static void main(String[] args) 
    {      
        Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

        javax.swing.SwingUtilities.invokeLater(() -> 
        {         
            MainFrame theWindow = new MainFrame("Test",screenSize);
            theWindow.setLocationRelativeTo(null);
            theWindow.getContentPane().add(new MenuPanel(theWindow,theWindow.getSize()));
        });
    }
}

MAINFRAME Class

package test.project;
import java.awt.Dimension;
import javax.swing.JFrame;

public class MainFrame extends JFrame
{
    @SuppressWarnings("LeakingThisInConstructor")
    public MainFrame(String windowName, Dimension theSize)
    {
        setTitle(windowName);
        setSize(theSize.width/2,theSize.height/2);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        setVisible(true);
    }
}

MAINPANEL Class

package test.project;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class MainPanel extends JPanel
{
    JFrame theHolder;
    public MainPanel(JFrame holder, Dimension theSize)
    {
        theHolder = holder;
        setSize(theSize);
    }
}

class MenuPanel extends MainPanel implements ActionListener
{
    JButton newButton = new JButton("NEW");
    JButton loadButton = new JButton("LOAD");
    JButton exitButton = new JButton("EXIT");    

    @SuppressWarnings("LeakingThisInConstructor")
    public MenuPanel(JFrame holder, Dimension theSize)
    {
        super(holder, theSize);
        setLayout(new GridLayout(0,1));
        SetButton(this);
        setVisible(true);
        theHolder.repaint();
    }

    private void SetButton(JPanel thePanel)
    {
        newButton.addActionListener(this);
        loadButton.addActionListener(this);
        exitButton.addActionListener(this);
        thePanel.add(newButton);
        thePanel.add(loadButton);
        thePanel.add(exitButton);
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource().equals(newButton))
        {
            theHolder.getContentPane().remove(this);
            theHolder.getContentPane().add(new LoadingPanel(theHolder,theHolder.getSize()));
            theHolder.repaint();
         }
        if(e.getSource().equals(loadButton))
        {

        }
        else if(e.getSource().equals(exitButton))
        {
            System.exit(0);
        }
    }
}

class LoadingPanel extends MainPanel
{
    JLabel loadingLabel = new JLabel("LOADING");

    @SuppressWarnings("LeakingThisInConstructor")
    public LoadingPanel(JFrame holder, Dimension theSize)
    {
        super(holder, theSize);
        setLayout(new GridLayout(0,1));
        SetLabel(this);
        setVisible(true);
        theHolder.repaint();
    }

    private void SetLabel(JPanel thePanel)
    {   
        thePanel.add(loadingLabel);
        loadingLabel.setSize(thePanel.getSize());
        loadingLabel.setHorizontalAlignment(SwingConstants.CENTER);
        loadingLabel.setVerticalAlignment(SwingConstants.CENTER); 
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
P4ndaH3ro
  • 9
  • 3
  • 2
    Your question is overly broad in that it isn't a single question. I suggest that you divide your questions up so that you ask one question per post, and show relevant code and information for that one question. – Hovercraft Full Of Eels Jun 28 '15 at 18:56
  • 1
    1) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). 2) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) 3) `setVisible(true); theHolder.repaint();` Panels default to visibility 'true' when the panel is added to a top level container that is made visible. It seems that panel is never added to anything. As such, calling `repaint()` on the frame is pointless. – Andrew Thompson Jun 28 '15 at 19:11
  • @HovercraftFullOfEels Well, I'm not sure how I could divide my code to make multiple post, since the big problem I think I have is overall code construction. Like: should I keep swaping JPanel in my JFrame, or should I just create 1 JPanel and actually modify it to fit the state of the application? – P4ndaH3ro Jun 28 '15 at 19:11
  • Tip: Add @HovercraftFullOfEels (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Jun 28 '15 at 19:12
  • @AndrewThompson the Panel is add to the JFrame when it's created theWindow.getContentPane().add(new MenuPanel(theWindow,theWindow.getSize())); – P4ndaH3ro Jun 28 '15 at 19:17

0 Answers0