package jp.gr.java_conf.skrb.prefs; import java.util.prefs.*; import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; public class PrefsGUISample1 { protected Preferences prefs; protected static final String LOCATION_X = "locationX"; protected static final String LOCATION_Y = "locationY"; protected static final String WIDTH = "width"; protected static final String HEIGHT = "height"; protected int locationX; protected int locationY; protected int width; protected int height; protected JLabel labelLocationX; protected JLabel labelLocationY; protected JLabel labelWidth; protected JLabel labelHeight; protected JFrame frame; public PrefsGUISample1(){ prefs = Preferences.userNodeForPackage(this.getClass()); loadPrefs(); frame = new JFrame("Preferences GUI Sample"); frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent event){ savePrefs(); System.exit(0); } }); frame.setBounds(locationX, locationY, width, height); initFrame(); frame.setVisible(true); } protected void initFrame(){ Container pane = frame.getContentPane(); JPanel panel = new JPanel(); panel.setBorder(new EmptyBorder(10, 10, 10, 10)); panel.setLayout(new GridLayout(0, 2)); JLabel label = new JLabel("Location X:"); panel.add(label); labelLocationX = new JLabel(String.valueOf(locationX), SwingConstants.RIGHT); panel.add(labelLocationX); label = new JLabel("Location Y:"); panel.add(label); labelLocationY = new JLabel(String.valueOf(locationY), SwingConstants.RIGHT); panel.add(labelLocationY); label = new JLabel("Width:"); panel.add(label); labelWidth = new JLabel(String.valueOf(width), SwingConstants.RIGHT); panel.add(labelWidth); label = new JLabel("Height:"); panel.add(label); labelHeight = new JLabel(String.valueOf(height), SwingConstants.RIGHT); panel.add(labelHeight); pane.add(panel, BorderLayout.CENTER); JButton button = new JButton("Exit"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ savePrefs(); System.exit(0); } }); pane.add(button, BorderLayout.SOUTH); } protected void loadPrefs(){ locationX = prefs.getInt(LOCATION_X, 0); locationY = prefs.getInt(LOCATION_Y, 0); width = prefs.getInt(WIDTH, 100); height = prefs.getInt(HEIGHT, 400); } protected void savePrefs(){ Rectangle rect = frame.getBounds(); prefs.putInt(LOCATION_X, rect.x); prefs.putInt(LOCATION_Y, rect.y); prefs.putInt(WIDTH, rect.width); prefs.putInt(HEIGHT, rect.height); } public static void main(String[] args){ new PrefsGUISample1(); } }