我正在尝试设计一个小游戏,并使用 JInternalFrame 来显示库存。但我遇到的问题是,当我最小化 InternalFrame 时,左下角的图标会自动隐藏在也添加到 JDesktopPane 的 JPanel 后面。当我向下拖动窗口(使窗口垂直变大)时,白色的 DesktopPane 在默认的灰色 JPanel 下方变得可见。另外,当我向下拖动时,InternalFrame 的图标就会显示出来。我尝试发布图片,但我是该网站的新手,所以我不能:(。
我尝试使用 moveToFront() 和 toFront() 方法,并尝试覆盖 InternalFrameListener 来图标化框架,但没有任何效果。如果您认为我使用了不好的设计,请随时告诉我 - 我只是一名学生,我对常见实践没有太多了解。
这是我的代码
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);
}
}
最佳答案
尝试这样的事情怎么样:
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);
}
}
我个人的观点是,在 JDesktopPane
上添加 JPanel
的想法很糟糕。
关于java - 最小化时 JInternalFrame 图标移动到 JPanel 下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13144305/