Hello everyone. I'm having trouble about Internal JFrames as my code is compiling but it doesn't run. Can someone give me an advice as to how to make it work? Also the reason why I wanted to make internal JFrames is that I'm using layout managers on my panels so I figured that I'd use internal JFrames to set the size of my panels.

Here's my code:

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

public class HangManGUI extends JFrame{
    //instantiate variables

    //two main panels
    private JPanel primaryPanel;
    private JPanel secondaryPanel;

    //instantiate internal frames for sizing the two main panels
    private JFrame primaryInternalFr;
    private JFrame secondaryInternalFr;

    //for the secondaryPanel
    private JButton startBtn;
    private JButton resetBtn;
    private JButton exitBtn;
    private JPanel imagePanel;
    private JPanel timerPanel;

    //for the imagePanel
    private JLabel imageLabel;

    //for the mainPanel
    private JPanel btnsPanel;
    private JButton[] letterBtns;

    public HangManGUI() {
        //instantiate two main panels
        secondaryPanel = new JPanel();
        primaryPanel = new JPanel();

        //for the secondarypanel
        startBtn = new JButton("Start");
        resetBtn = new JButton("Rest");
        exitBtn = new JButton("Exit");
        imagePanel = new JPanel();
        timerPanel = new JPanel();

        imageLabel = new JLabel("Sample");

        secondaryPanel.setLayout(new GridLayout(5, 1)); //5x1 for secondaryPanel

        btnsPanel = new JPanel();

        btnsPanel.setLayout(new GridLayout(1,1)); //3x9 for btnsPanel

        //instantiate buttons

        letterBtns = new JButton[26];
        for (int i = 0; i <letterBtns.length; i++) {
            char first = 'A';
            int add = (int) first + i;
            letterBtns[i] = new JButton(Character.toString((char) add));
        } //add alphabets using Character

        btnsPanel = new JPanel(new GridLayout(3,9));
        for (int ii = 0; ii < letterBtns.length; ii++) {


        //add panels to internal jframes

        //set the sizes of internal frames
        secondaryInternalFr.setSize(250, 500);
        primaryInternalFr.setSize(600, 500);

        //add internal frames to main frames

        //set the frame
        setLayout(new GridLayout(1,1));
        setTitle("Hang Man Game");
        //setSize(450, 300);

     * @param args the command line arguments
    public static void main(String[] args) {
        try {
            HangManGUI run = new HangManGUI();
        } catch (Exception e) {

Edited 3 Years Ago by ExtraKanin: additional info

define "doesn't run".
does it give you a stacktrace, or does it just not run?
I tried your code here. you get NullPointerExceptions, because you don't instantiate your 'internal JFrames' which are actually just JFrames, not internal jframes.

start with instantiating those.

There's no reason at all to use JFrames like that. (In fact it's not even legal to add a JFrame inside another JFrame.) Just use JPanels, add your components to them, and pack() them. The layout manager will sort out the sizes.
If you do have a suicidal comittment to fixing a size in pixels, use setSize, setPreferredSize and/or setMinimumSize on you JPanels - different layout managers respect or ignore those settings differently.

Edited 3 Years Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.