在JDesktopPane中添加一个类

时间:2013-08-10 23:20:57

标签: java swing jdesktoppane internal-class

我一直在寻找这个问题,但它对我没用。

我做的是我有这样的目录

|-> 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);
    }
}

有可能吗?或者在jdesktoppane中添加外部类的任何其他方法?

1 个答案:

答案 0 :(得分:3)

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

不要将布局设置为null,为什么要这样做?删除此行并添加如下内部:

public void actionPerformed(ActionEvent ae) {
     PacmanGame obj = new PacmanGame();
     obj.setVisible(true);
     obj.setSize(....);//and so on
     dsktop.add(obj);
}