我正在使用首选项来保存 JFrame 的位置,因此当它再次出现时,它应该与之前退出的位置相同。我让它在我的电脑上工作,但它似乎不适用于我测试过的其他 3 台电脑。我将我的项目导出为 jar 文件,并让人们使用 jar 文件执行程序。
框架的位置没有保存,它总是出现在屏幕的左上角。
这是我第一次使用首选项,我多次查看 API,但似乎看不出我做错了什么。有人可以解释为什么它只适用于我的电脑吗?
首选项设置位于类的底部。
SSCCE:
public class PreferencesTest implements ActionListener {
private JButton wrongButton, level1Button, level2Button, level3Button,
level4Button, level5Button, dontInterruptButton, reviewCardButton;
private JLabel frontTextLabel, reverseTextLabel, frontLabel;
private JPanel frontOfCardPanel, reverseOfCardPanel, buttonPanel,
reverseButtonPanel, reverseTextPanel, frontLabelPanel;
private JFrame frame;
private String windowTitle;
private int windowX;
private int windowY;
private Preferences preferences = null;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new PreferencesTest();
}
});
}
public PreferencesTest() {
this.windowX = getPreferences().getInt("window_x", -1);
this.windowY = getPreferences().getInt("window_y", -1);
frame = new JFrame();
frontOfCardPanel = new JPanel(new BorderLayout());
reverseOfCardPanel = new JPanel(new BorderLayout());
wrongButton = new JButton("Wrong");
level1Button = new JButton("Almost");
level2Button = new JButton("OK");
level3Button = new JButton("Good");
level4Button = new JButton("Great");
level5Button = new JButton("Excellent");
dontInterruptButton = new JButton("Don't interrupt this program");
reviewCardButton = new JButton("Review");
frontTextLabel = new JLabel("test", JLabel.CENTER);
reverseTextLabel = new JLabel("test", JLabel.CENTER);
frontLabel = new JLabel("test", JLabel.CENTER);
frontTextLabel.setFont(new Font("Serif", Font.PLAIN, 20));
frontLabel.setFont(new Font("Serif", Font.PLAIN, 40));
reverseTextLabel.setFont(new Font("Serif", Font.PLAIN, 40));
buttonPanel = new JPanel(new FlowLayout());
reverseButtonPanel = new JPanel(new FlowLayout());
reverseTextPanel = new JPanel(new GridLayout(2, 1));
reverseButtonPanel.add(wrongButton);
reverseButtonPanel.add(level1Button);
reverseButtonPanel.add(level2Button);
reverseButtonPanel.add(level3Button);
reverseButtonPanel.add(level4Button);
reverseButtonPanel.add(level5Button);
frontLabelPanel = new JPanel();
frontLabelPanel.add(frontLabel);
frontOfCardPanel.add(buttonPanel, BorderLayout.SOUTH);
frontOfCardPanel.add(frontLabelPanel, BorderLayout.NORTH);
frame.pack();
frame.setContentPane(frontOfCardPanel);
reverseTextPanel.add(reverseTextLabel);
reverseTextPanel.add(frontTextLabel);
buttonPanel.add(reviewCardButton);
buttonPanel.add(dontInterruptButton);
reverseOfCardPanel.add(reverseTextPanel, BorderLayout.CENTER);
reverseOfCardPanel.add(reverseButtonPanel, BorderLayout.SOUTH);
wrongButton.addActionListener(this);
level1Button.addActionListener(this);
level2Button.addActionListener(this);
level3Button.addActionListener(this);
level4Button.addActionListener(this);
level5Button.addActionListener(this);
dontInterruptButton.addActionListener(this);
reviewCardButton.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 220);
frame.setResizable(false);
frame.toFront();
frame.repaint();
frame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
setWindowXY();
frame.setVisible(false);
}
});
frame.setLocation(windowX, windowY);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent event) {
if (event.getActionCommand() == "Review") {
frame.remove(frontOfCardPanel);
frame.setContentPane(reverseOfCardPanel);
frame.validate();
frame.repaint();
} else if (event.getActionCommand() == "Wrong") {
setWindowXY();
System.exit(1);
flashcardAction(0);
} else if (event.getActionCommand() == "Almost") {
setWindowXY();
System.exit(1);
flashcardAction(1);
} else if (event.getActionCommand() == "OK") {
setWindowXY();
System.exit(1);
flashcardAction(2);
} else if (event.getActionCommand() == "Good") {
setWindowXY();
System.exit(1);
flashcardAction(3);
} else if (event.getActionCommand() == "Great") {
setWindowXY();
System.exit(1);
flashcardAction(4);
} else if (event.getActionCommand() == "Excellent") {
setWindowXY();
System.exit(1);
flashcardAction(5);
} else if (event.getActionCommand() == "Don't interrupt this program") {
System.exit(1);
System.out.println(" window title in event = " + windowTitle);
frame.setVisible(false);
}
}
public void flashcardAction(int userResponse) {
System.out.println(frame.getLocationOnScreen());
setVisible(false);
}
public void setVisible(Boolean visible) {
frame.setVisible(visible);
}
// ////// preferences
public Preferences getPreferences() {
if (preferences == null) {
preferences = Preferences
.systemNodeForPackage(PreferencesTest.class);
}
return preferences;
}
public int getWindowX() {
return windowX;
}
public int getWindowY() {
return windowY;
}
public void setWindowXY() {
windowX = frame.getX();
getPreferences().putInt("window_x", windowX);
windowY = frame.getY();
getPreferences().putInt("window_y", windowY);
}
}
编辑:我没有解释它的行为有什么问题。