为什么我的Java首选项代码不适用于所有计算机?

时间:2013-03-04 13:26:09

标签: java preferences

我正在使用首选项来保存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);
}
}
编辑:我没有解释它的行为有什么问题。

1 个答案:

答案 0 :(得分:2)

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

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

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

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

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