gpt4 book ai didi

java - 在 JDesktopPane 中添加一个类

转载 作者:行者123 更新时间:2023-11-29 05:39:27 24 4
gpt4 key购买 nike

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

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

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

最佳答案

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

关于java - 在 JDesktopPane 中添加一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18167244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com