0

I am writing an application with a team, that currently uses 3 different JFrames. From what I have been reading I believe this is the wrong way to go, but I am not sure how to proceed.

When the application starts it opens a small Jframe that allows the user to click on one of 4 buttons. If start game or add images is clicked another window opens. The game window, for the game and the image editing window, for the images. When they close the window the first JFrame reappears.

I've created the first JFrame but have been unable to get the second one to open. I do not know if it is a problem in my code, or if something in the second JFrame isn't set-up properly to call it.

The first JFrame: package mmlgame;

import javax.swing.JFrame;

public class MMLGame extends javax.swing.JFrame {

    public MMLGame() {
        initComponents();
    }

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit(0);
    }

    private void btnStartGameActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
        GameScreen frame = new GameScreen();

        frame.setVisible(true);

    }
    public static void main(String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MMLGame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton btnAddImage;
    private javax.swing.JButton btnCredits;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnStartGame;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblLogo;
    // End of variables declaration
}

I have not set-up all of the buttons yet. I like to get each one working before moving on to the next. The second JFrame: package mmlgame;

public class GameScreen extends javax.swing.JFrame {

    public GameScreen() {
        initComponents();
    }

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GameScreen().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel pnlGameScreen;
    // End of variables declaration                   
}

The second JFrame currently has a label in it. I am the one on the team that suggested using JFrames so if I am wrong please let me know so that I can get the group moving in the right direction. Also I am currently working my way through the Creating a GUI With JFC/Swing. If there is something that explains the concepts better please let me know.

user1793408
  • 113
  • 11
  • 1
    See also [*The Use of Multiple JFrames, Good/Bad Practice?*](http://stackoverflow.com/q/9554636/230513) – trashgod Oct 27 '13 at 06:17
  • It should work this way.. maybe you didn't put the code for event binding, as I assume it's generated by netbeans.. anyway try to do simple system.out.print at first and see if your button is bound to the event handler.. if not you should click on the button in the netbeans gui editor and check events to see if the action listener is bound to this handler.. – Mohammed R. El-Khoudary Oct 27 '13 at 06:25
  • Of course we're not discussing the multiple jframes issue here.. you should read trashgods link.. – Mohammed R. El-Khoudary Oct 27 '13 at 06:28
  • @trashgod One good way would be to replace `JPanels` in `JFrame` ? ;) – An SO User Oct 27 '13 at 06:35
  • 1
    @LittleChild: So many possibilities! I like panels in a `JTabbedPane`, but panels in a `CardLayout` would work, too – trashgod Oct 27 '13 at 07:01
  • @trashgod CardLayout sounds so much better – An SO User Oct 27 '13 at 07:17
  • Should have responded earlier to this. Thank you for the link, we change the layout so only 1 JFrame was being used. – user1793408 Dec 05 '13 at 07:24

3 Answers3

0

Try setting the preferred size and relative location of the second frame and also make sure that the "alwaysOnTop" property for the first frame is cleared. You can try the following to show the first frame from the second frame' button 1. Pass the reference of the first frame to the costructor of the seocnd. 2. Use a object variable of Frame type to hold it. 3. In code of the actionEvent firing of the desired button, set second frame's "visible" to false and first frame's "visible" to true.

0

when user click on the button make the JFRAME visible false or if you do not have any Important data to show . DISPOSE the frame, and open it again in the closing Event of your second frame

 private void btnLogoutActionPerformed(java.awt.event.ActionEvent evt) {                                          

        this.dispose();
        new Login().setVisible(true);
    }  
Deepak Odedara
  • 481
  • 1
  • 3
  • 12
0

I was able to get this to work, but discovered that every time a different JFrame opened it also showed up as another app in the taskbar. So we switched the secondary JFrames to JDialogs and set visible on main to false.

user1793408
  • 113
  • 11