gpt4 book ai didi

java - 将 JInternalFrame 添加到 fsem 框架

转载 作者:行者123 更新时间:2023-12-01 14:43:09 25 4
gpt4 key购买 nike

我正在尝试将内部框架添加到处于全屏独占模式的框架中。然而,当我添加它时,它会用白色填充整个屏幕。这是我的代码。

    JFrame f = new JFrame();
f.setTitle("Platform Game");
f.setLocationRelativeTo(null);
f.setUndecorated(true);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
gs.setFullScreenWindow(f);
f.setVisible(true);
createFrame(f);


protected static void createFrame(JFrame f)
{
JInternalFrame frame = new JInternalFrame("Options", false, true, false, false);
frame.setVisible(true);
frame.setSize(10, 10);
frame.setLocation(10, 10);
JDesktopPane pane = new JDesktopPane();
pane.add(frame);
f.setContentPane(pane);

try
{
frame.setSelected(true);
}
catch (java.beans.PropertyVetoException e)
{
e.printStackTrace();
}
}

如果有人知道如何正确执行此操作,请发布它,或告诉我我做错了什么。

详情

操作系统:Windows 7

JDK:jdk 1.7.0_11

集成开发环境:eclipse

我在jframe上使用面板,并且我在面板上绘图

绘画代码

public void paintComponent(Graphics g)
{
super.paintComponent(g);

for(int i = 0; i<1000; i++)
g.drawImage(bi, i*bi.getWidth()-getXs(), 0, bi.getWidth(), Main.HEIGHT, null);

g.setColor(Color.WHITE);

for(int x = 0; x<Main.X_TILES; x++)
{
for(int y = 0; y<Main.Y_TILES; y++)
{
Block block = map[x][y];

if(block!=null && x*Block.WIDTH>=xs-Block.WIDTH && x*Block.WIDTH<=xs+Main.WIDTH)
g.drawImage(block.getImage(x,y), x*Block.WIDTH-getXs(), y*Block.HEIGHT, Block.WIDTH, Block.HEIGHT, null);
}
}

for(Entity entity : entities)
{
if(entity!=null && entity.getX()>=xs-Block.WIDTH && entity.getX()<=xs+Main.WIDTH)
g.drawImage(entity.getImage(), entity.getX()-getXs(), entity.getY(), entity.getWidth(), entity.getHeight(), null);
}

if(displayDebug)
{
g.drawString("Free memory "+Runtime.getRuntime().freeMemory()/1024+" KB", 10, 12);
g.drawString("Total memory "+Runtime.getRuntime().totalMemory()/1024+" KB", 10, 24);
g.drawString("Max memory "+Runtime.getRuntime().maxMemory()/1024+" KB", 10, 36);
g.drawString("X "+character.getX(), 10, 48);
g.drawString("Y "+character.getY(), 10, 60);
g.drawString("XS "+xs, 10, 72);
}

g.setColor(Color.BLACK);
g.drawString("Life "+character.getHealth(), 700, 12);
g.dispose();
}

我看到了类似问题的答案JInternalFrame in full-screen mode据说是直接放在面板上,但我尝试了,但还是不行。

编辑:当我注释掉我的绘画代码,并将其直接添加到面板中时,它起作用了,但是它不会拖来拖去,所以我的新问题是如何让它在绘画工作正在进行时出现完毕。他们是使用绘图组件绘制组件的方法吗?

为了澄清,当添加到没有绘画代码的面板时,它仍然无法正常工作。

最佳答案

您说的是向框架中添加 JPanel(第二个代码片段),但实际上您正在使用以下行中的 JDesktopPane 替换框架的全部内容:

f.setContentPane(pane);

因此,除非您将游戏面板(具有重写的paintComponent方法的面板)添加到框架玻璃 Pane 上的某处或类似奇怪的地方,否则它将永远不会显示,因为JDesktopPane(以及您添加到其中的JInternalFrame)是此时只有框架上的组件。

此外,为了完整起见,您应该将 setVisible(true) 调用移至初始化的最后(即在 createFrame(f); 之后),并完全删除对 dispose() 的调用在您的paintComponent方法中(g.dispose();)。

至少这是我想象的情况,如果没有帮助,请尝试发布一个更简洁且独立的示例来展示您遇到的问题。

这是一个修改后的示例,它将在同一个全屏组件中显示自定义图形(在本例中仅显示文本)和 JInternalFrame:

public class FrameTest {

public static void main(String[] args) {
JFrame f = new JFrame();
f.setTitle("Platform Game");
f.setLocationRelativeTo(null);
f.setUndecorated(true);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
gs.setFullScreenWindow(f);

JDesktopPane pane = new MyPanel();
JInternalFrame frame = new JInternalFrame("Options", false, true, false, false);
frame.setVisible(true);
frame.setSize(100, 100);
frame.setLocation(10, 10);
pane.add(frame);
f.setContentPane(pane);

f.setVisible(true);
}

private static class MyPanel extends JDesktopPane {
public void paintComponent(Graphics g) {
super.paintComponent(g);

g.setColor(Color.BLACK);

g.drawString("Free memory " + Runtime.getRuntime().freeMemory() / 1024 + " KB", 10, 12);
g.drawString("Total memory " + Runtime.getRuntime().totalMemory() / 1024 + " KB", 10, 24);
g.drawString("Max memory " + Runtime.getRuntime().maxMemory() / 1024 + " KB", 10, 36);
}
}
}

关于java - 将 JInternalFrame 添加到 fsem 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15736740/

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