Question

I've been searching for this problem but it doesn't work for me.

What I did is that I have directories like this

|-> Games (folder)
|----> GamesCombined.java and classes
|----> games (folder)
|--------> pacman (folder)
|------------> PacmanGame.java and classes

Inside Games.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import games.pacman.*;

public class GamesCombined extends JFrame {
    private JDesktopPane dsktp = new JDesktopPane();
    private JMenuBar menuBar = new JMenuBar(); 
    private JMenu gamesmenu = new JMenu("Games");
    private JMenuItem play_pacman = new JMenuItem("Pacman"); 
    public GamesCombined() {
        super("Games");
        setLayout(null);
        Container c = getContentPane();
        add(dsktp);
        setJMenuBar(menuBar);
        menuBar.add(gamesmenu);
        gamesmenu.add(play_pacman);
        play_pacman.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                //I want to add PacmanGame.java as an InternalFrame of this DesktopPane.
            }   
        });
        int frameWidth = 800;
        int frameHeight = 850;
        setSize(frameWidth, frameHeight);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

Is it possible? or any other ways to add external class in a jdesktoppane?

Was it helpful?

Solution

 setLayout(null);  // That's your problem

Don't set the layout to null, why are you doing this? Remove this line and add the internals like this:

public void actionPerformed(ActionEvent ae) {
     PacmanGame obj = new PacmanGame();
     obj.setVisible(true);
     obj.setSize(....);//and so on
     dsktop.add(obj);
}   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top