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);
}
}