1

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

我所做的是我有这样的目录

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

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 中添加外部类的方法?

4

1 回答 1

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);
}   
于 2013-08-10T23:37:38.047 回答