我什么时候需要调用此方法Runtime.getRuntime().addShutdownHook()以及何时或为何需要关闭我的应用程序.有谁可以通过举一些例子来解释我这个.
谢谢
使用这样的代码处理JFrame的好方法是什么?我想处理Window出口和窗口关闭.
我知道我们不应该使用System.exit();
public class JavaCellularAutomataSquare {
public static final String TITLE = "Cellular Automata - Squaring Example";
private int maxWidth = 600;
private int maxHeight = 600;
public void launch() {
final JFrame frame = new JFrame(TITLE);
frame.setLocation(20, 20);
frame.setPreferredSize(new Dimension(maxWidth, maxHeight));
frame.setResizable(false);
frame.setFocusable(true);
final JPanel panel = new JPanel();
panel.setLocation(20, 20);
panel.setVisible(true);
panel.setPreferredSize(new Dimension(maxWidth, maxHeight));
panel.setFocusable(true);
panel.setBackground(Color.white);
// Panel setup, toggle visibility on frame
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud) 说我有以下代码:
try {
//Do something with File
} catch (FileNotFoundException e) {
outputInfo("Error in IO Redirection", true);
e.printStackTrace();
System.exit(1);
}
Run Code Online (Sandbox Code Playgroud)
我的程序在此catch位置之后立即退出,是一个单线程(一个主方法)程序,不应该期望从这样的异常中恢复.
我真的应该用System.exit(1);吗?
我正在尝试通过创建垄断银行家程序来学习 Java 和 OOP。但是,我希望在退出程序后保存一些对象变量,以便在玩了一半的垄断游戏后,我可以退出我的程序,然后在保存所有玩家余额的情况下重新启动它。
我猜这需要某种数据库?
这是我的代码的一部分;退出程序后,我试图为所有对象(玩家)保存“平衡”变量。
public class Monopoly {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//creates the users (name classes)
players player1 = new players();
players player2 = new players();
players player3 = new players();
players player4 = new players();
while (true) {
player1.balance=player1.finalbalance;
player2.balance=player2.finalbalance;
player3.balance=player3.finalbalance;
player4.balance=player4.finalbalance;
Run Code Online (Sandbox Code Playgroud) 我想在申请结束前做一些动作.
我看到有动作听众,但我没有框架或类似的东西.只是一个java文件.
我怎样才能设置某种控制?