1

我正在使用首选项来保存 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);
}
}

编辑:我没有解释它的行为有什么问题。

4

1 回答 1

2

您正在使用systemNodeForPackage。这将返回系统首选项,而不是用户首选项。

它在一个系统上运行的原因可能是因为您在那里以管理员用户身份运行它(我猜您正在使用 WinXP 或类似的东西,通常以管理员用户身份运行)。管理员用户可以毫无问题地写入系统首选项树。

在其他更成熟的操作系统上,如果没有提升的访问权限,就无法写入系统节点。正确的方法是使用用户节点(方法userNodeForPackage)。

Java 有两个单独的偏好根,一个用于系统,一个用于用户。根据您的情况使用正确的。窗口位置显然是用户偏好。

请参阅https://blogs.oracle.com/CoreJavaTechTips/entry/the_preferences_api

于 2013-03-04T13:32:46.163 回答