如何在不使用 SwingUtilities 的情况下在 java 中创建两个完全独立的 window?
How to create two completely separate window in java without using SwingUtilities?
免责声明:我知道有一个与我的问题类似的问题,但我的问题不同。
我正在尝试为我正在创建的游戏创建我自己的调试器。我想创建 两个 windows:一个 window 用于我的游戏,另一个 window 用于我的调试器 当我的调试器打开时,我可以在其中管理、查看和更改游戏变量。我希望能够同时使用和查看两者 window。问题是我的调试器 window 连接到我的游戏 window。当我关闭调试器 window 时也关闭了游戏 window,这是一个问题。
我不想改变我创建游戏的方式window,我只想改变调试器window。
这是我用来创建我的游戏的代码window:
// main method starts
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Ultra Ball"); // create application window
Game game = new Game(); // represents game class
frame.add(game); // add the frame
// set size of application window
frame.setMaximumSize(new Dimension(game.getScreenWidth(), game.getScreenHeight()));
frame.setMinimumSize(new Dimension(600, 500)); // set minimum size
frame.setSize(600, 500); // for testing the game size
frame.setVisible(true); // show the application window
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // basic window operations
Image icon = game.createImage("images/logo.png"); // icon
frame.setIconImage(icon); //set the icon
while (true) { // runs throughout the game
game.setBackground(background); // set background color
game.resize(); // resize game component
game.move(); // move game components
game.collisionDetection(); // detect objects collision
game.debug(true); // debug the game
game.repaint(); // redraw the game
Thread.sleep(10); // pauses execution of game
} // while loop ends
} // main method ends
这是我用来创建 调试器的代码 window:
// Debug class begins
public class Debug {
// link classes
@SuppressWarnings("unused")
private Game game; // Game class
// JFrame
JFrame window = new JFrame("Debugger");
// Debug constructor begins
public Debug(Game game) {
this.game = game;
} // Debug constructor ends
// create a debugger
public void debug() {
window.setSize(500, 500);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} // debug method ends
} // Debug class ends
触发调试器:
// debug method begins
public void debug(boolean status) {
if (status == true) {
debug.debug();
}
}
在此先感谢您的帮助。
通过这一行,您可以明确设置调试器 window 以关闭您的应用程序:window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
EXIT_ON_CLOSE (defined in JFrame): Exit the application using the
System exit method.
尝试指定不同的操作甚至完全删除此行,因此默认操作将是 HIDE_ON_CLOSE
。
文档和所有可用选项:
https://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation(int)
快速浏览代码...
game.debug
在每个周期调用...
while (true) { // runs throughout the game
game.setBackground(background); // set background color
game.resize(); // resize game component
game.move(); // move game components
game.collisionDetection(); // detect objects collision
game.debug(true); // debug the game
game.repaint(); // redraw the game
Thread.sleep(10); // pauses execution of game
} // while loop ends
这好像叫...
// debug method begins
public void debug(boolean status) {
if (status == true) {
debug.debug();
}
}
这好像叫...
// create a debugger
public void debug() {
window.setSize(500, 500);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} // debug method ends
这解释了为什么调试框架不断弹出。
想到两个直接的解决方案,@krystainG 演示的第一个解决方案是将 window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
更改为更类似于 window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
的内容
下一个解决方案是更改 "when" 并调用 "how" Debug#debug(boolean)
我要采取的第一步是将 game.debug
调用移出游戏循环
game.debug(true); // debug the game
while (true) { // runs throughout the game
game.setBackground(background); // set background color
game.resize(); // resize game component
game.move(); // move game components
game.collisionDetection(); // detect objects collision
game.repaint(); // redraw the game
Thread.sleep(10); // pauses execution of game
} // while loop ends
我可能做的第二个改变是只在状态实际改变时更新状态
private boolean isDebugging = false;
// debug method begins
public void debug(boolean status) {
if (status != isDebugging) {
isDebugging = status
if (status == true) {
debug.debug();
} else {
// Close the debug window
}
}
}
当 class 已经处于指定状态时,这将阻止方法对状态变化做出反应
免责声明:我知道有一个与我的问题类似的问题,但我的问题不同。
我正在尝试为我正在创建的游戏创建我自己的调试器。我想创建 两个 windows:一个 window 用于我的游戏,另一个 window 用于我的调试器 当我的调试器打开时,我可以在其中管理、查看和更改游戏变量。我希望能够同时使用和查看两者 window。问题是我的调试器 window 连接到我的游戏 window。当我关闭调试器 window 时也关闭了游戏 window,这是一个问题。
我不想改变我创建游戏的方式window,我只想改变调试器window。
这是我用来创建我的游戏的代码window:
// main method starts
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Ultra Ball"); // create application window
Game game = new Game(); // represents game class
frame.add(game); // add the frame
// set size of application window
frame.setMaximumSize(new Dimension(game.getScreenWidth(), game.getScreenHeight()));
frame.setMinimumSize(new Dimension(600, 500)); // set minimum size
frame.setSize(600, 500); // for testing the game size
frame.setVisible(true); // show the application window
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // basic window operations
Image icon = game.createImage("images/logo.png"); // icon
frame.setIconImage(icon); //set the icon
while (true) { // runs throughout the game
game.setBackground(background); // set background color
game.resize(); // resize game component
game.move(); // move game components
game.collisionDetection(); // detect objects collision
game.debug(true); // debug the game
game.repaint(); // redraw the game
Thread.sleep(10); // pauses execution of game
} // while loop ends
} // main method ends
这是我用来创建 调试器的代码 window:
// Debug class begins
public class Debug {
// link classes
@SuppressWarnings("unused")
private Game game; // Game class
// JFrame
JFrame window = new JFrame("Debugger");
// Debug constructor begins
public Debug(Game game) {
this.game = game;
} // Debug constructor ends
// create a debugger
public void debug() {
window.setSize(500, 500);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} // debug method ends
} // Debug class ends
触发调试器:
// debug method begins
public void debug(boolean status) {
if (status == true) {
debug.debug();
}
}
在此先感谢您的帮助。
通过这一行,您可以明确设置调试器 window 以关闭您的应用程序:window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method.
尝试指定不同的操作甚至完全删除此行,因此默认操作将是 HIDE_ON_CLOSE
。
文档和所有可用选项: https://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation(int)
快速浏览代码...
game.debug
在每个周期调用...
while (true) { // runs throughout the game
game.setBackground(background); // set background color
game.resize(); // resize game component
game.move(); // move game components
game.collisionDetection(); // detect objects collision
game.debug(true); // debug the game
game.repaint(); // redraw the game
Thread.sleep(10); // pauses execution of game
} // while loop ends
这好像叫...
// debug method begins
public void debug(boolean status) {
if (status == true) {
debug.debug();
}
}
这好像叫...
// create a debugger
public void debug() {
window.setSize(500, 500);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} // debug method ends
这解释了为什么调试框架不断弹出。
想到两个直接的解决方案,@krystainG 演示的第一个解决方案是将 window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
更改为更类似于 window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
下一个解决方案是更改 "when" 并调用 "how" Debug#debug(boolean)
我要采取的第一步是将 game.debug
调用移出游戏循环
game.debug(true); // debug the game
while (true) { // runs throughout the game
game.setBackground(background); // set background color
game.resize(); // resize game component
game.move(); // move game components
game.collisionDetection(); // detect objects collision
game.repaint(); // redraw the game
Thread.sleep(10); // pauses execution of game
} // while loop ends
我可能做的第二个改变是只在状态实际改变时更新状态
private boolean isDebugging = false;
// debug method begins
public void debug(boolean status) {
if (status != isDebugging) {
isDebugging = status
if (status == true) {
debug.debug();
} else {
// Close the debug window
}
}
}
当 class 已经处于指定状态时,这将阻止方法对状态变化做出反应