I have a JMenuBar
with a help button. When user clicks on "help" I need to open a different window with a .jpg
showing the game instructions. That same window can be closed when the user clicks outside of it. I think I'm missing code because it does not work:
Window.java
public class Window extends JWindow
{
//java.net.URL imgIntro = getClass().getResource("/images/intro.jpg");
ImageIcon imIntro = new ImageIcon(getClass().getResource("/images/intro.jpg"));
//java.net.URL imgRegles = getClass().getResource("/images/rules.jpg");
ImageIcon imRules = new ImageIcon(getClass().getResource("/images/rules.jpg"));
static Thread t = new Thread();
static int thread = 0;
static JButton bouton;
static int X;
static int Y;
public Window( int X, int Y, int type) {
super();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setSize(X,Y);
setLocation( (dim.width - X)/2 , (dim.height - Y)/2);
setVisible(true);
Container fen = getContentPane();
if (type == 1 ) bouton = new JButton(imIntro);
else bouton = new JButton(imRules);
bouton.setPreferredSize(new Dimension(X, Y) );
fen.add( bouton);
bouton.setVisible( true );
show();
/* if window introduction,
just display for 5 secondes */
if( type == 1 ) {
try {
Thread.sleep(5000);
thread = 1;
}
catch( java.lang.InterruptedException ex ) {
JOptionPane.showMessageDialog(null, "erreur");
}
dispose();
}
/* if window of rules
only close it when user clicks */
else if ( type == 2 ) {
bouton.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
}
}
}
Menu.java
public class Menu extends JMenuBar implements ActionListener{
Interface map;
JMenu m5;
public Menu(Interface map){
super();
this.map=map;
m5 = new JMenu ("Help");//dislay instructions / rules
this.add(m5);
public void actionPerformed(ActionEvent evt){
//.../
else if(evt.getSource () == m5){
//new JWindow
Window rules = new Window( 700, 457, 2);
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.URISyntaxException;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
/**
*
* @author Mark
*/
public class Test {
JFrame mainFrame = new JFrame ();
JFrame instructions = new JFrame();
public Test (){
gui ();
}
public void gui (){
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(600, 400);
mainFrame.setVisible(true);
instructions.setSize(200, 200);
JMenuBar mb = new JMenuBar ();
JMenu help = new JMenu("Help");
mb.add(help);
JMenuItem instructionsMenu = new JMenuItem ("Instructions");
help.add(instructions);
mainFrame.setJMenuBar(mb);
instructions.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
instructions.setVisible(true);
mainFrame.dispose();
}
});
}
public static void main(String[] args) throws IOException, URISyntaxException
{
new Test ();
}
}
I suggest you use a JDialog
for the instructions window and not a JFrame
. See here for example.
public class Test {
JFrame mainFrame = new JFrame();
JDialog instructions = new JDialog(mainFrame);
public Test() {
gui();
}
public void gui() {
instructions.setSize(200, 200);
JMenuBar mb = new JMenuBar();
JMenu help = new JMenu("Help");
mb.add(help);
JMenuItem instructionsMenu = new JMenuItem("Instructions");
help.add(instructionsMenu);
instructionsMenu.addActionListener(e -> instructions.setVisible(true));
mainFrame.setJMenuBar(mb);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(600, 400);
mainFrame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Test());
}
}
Notes:
setVisible
as the last thing you do for a window.pack()
instead of setting their size.