2

I am trying to design a small game, and using a JInternalFrame to display the Inventory. The problem I am having though, is that when I minimize the InternalFrame, the icon in the bottom left corner automatically hides behind the JPanel that is also added to the JDesktopPane. When I drag the window down (making the window larger vertically), the white DesktopPane becomes visible below the default gray JPanel. Also, as I drag down, the icon of the InternalFrame is revealed. I tried to post pictures but I'm new to the site so I can't :(.

I've tried using the moveToFront() and toFront() methods and tried overriding the InternalFrameListener for iconifying the frame, but nothing has worked. If you think I am using a bad design feel free to tell me - I'm just a student and I don't have too much knowledge of common practice.

Here's my code

public class GameTester extends JFrame {

    private JDesktopPane pane;
    private Inventory i; //class that extends JInternalFrame

    public static void main(String[] args) {
        GameTester frame = new GameTester();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1280, 720);
    }       

    public GameTester() {
        setFocusable(true);
        i = new Inventory("Inventory");

        GamePanel g = new GamePanel();
        g.setBounds(0, 0, 1280, 720);

        pane = new JDesktopPane();
        pane.add(i);
        setContentPane(pane);
        getContentPane().add(g);            
        }
}
tyler
  • 53
  • 5

1 Answers1

2

What about to try something like this:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;


public class GameStarter extends JFrame {

    private JDesktopPane pane = new JDesktopPane();
    private JInternalFrame i = new JInternalFrame(); 

    private JPanel panel = new JPanel();

    public static void main(String[] args) {
        GameStarter frame = new GameStarter();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1024,768);
        frame.setVisible(true);
    }       

    public GameStarter() {
        setLayout(new BorderLayout());
        i.setClosable(true);
        i.setIconifiable(true);
        i.setMaximizable(true);
        i.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
        i.setSize(320,240);
        i.setVisible(true);
        pane.setPreferredSize(new Dimension(1024,320));
        pane.setBackground(Color.GRAY);
        pane.add(i);    
        panel.setBackground(Color.YELLOW);

        add(pane,BorderLayout.SOUTH);
        add(panel,BorderLayout.CENTER);
    }
}

enter image description here

My personal opinion is that the idea of adding JPanel on JDesktopPane is bad.

Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85
  • @MadProgrammer Thanks! I would suggest him to make his own frame (rectangle drawn on JPanel with text(those would be options), moveable when mouse is dragged), but I supose that would be hard for him as a beginner. – Branislav Lazic Oct 30 '12 at 23:50
  • 1
    You could suggest the use a `JLayeredPane` I guess, but it's a little hard to figure out why the OP is going down the path... – MadProgrammer Oct 30 '12 at 23:58
  • Thanks MadProgrammer, I just went ahead and scratch the desktoppane. I'm using the JLayeredPane and it works perfectly. Instead of using window buttons for controlling the frame I have it keybound. – tyler Oct 31 '12 at 04:45