positionmanager.java
来自「用JGraph编的软件」· Java 代码 · 共 587 行 · 第 1/2 页
JAVA
587 行
JSplitPane.DIVIDER_LOCATION_PROPERTY,
posPropertyChangeListener);
} else
// do not set the position for all components
// use only windows
if (comp instanceof JInternalFrame || comp instanceof Window) {
comp.removeComponentListener(posComponentListener);
} else
throw new IllegalArgumentException(
"We have no position storage implementation for "
+ comp.getClass());
}
/** Sets a value for the component.
*
* You can use the extension to differ
* several values for the component.
*
* If you use the same extension
* at the {@link #getIntPos(Component, String, int)}
* method, you will get back the value.
*
* @param comp the component
* @return the above string
*/
public static void setIntPos(Component comp, String extension, int value) {
Preferences p = getPreferences();
p.putInt(getKey(comp) + extension, value);
}
/** Gets the key for the component.
* The key is addition from the full qualified
* class name and the name property.
* If the name property is null, you only will
* get the full qualified class name.
*
* If you have got two frames with the
* same class but with different
* window positions, then you should
* use different names and everything
* will work fine.
*
* @param comp
* @return
*/
public static String getKey(Component comp) {
/*
*
* a full quallifier
* produces too log keys!
* we only have 80 chars
*
*
Stack names = new Stack();
do {
String name = comp.getName();
if (name != null)
names.push(name);
String className = comp.getClass().getName();
if (className != null);
names.push(className);
comp = comp.getParent();
} while (comp != null);
String key = null;
while (!names.empty()) {
if (key != null)
key = key + "_" + names.pop();
else
key = "" + names.pop();
}
*/
String key = comp.getClass().getName();
if (comp.getName() != null)
key = key + "." + comp.getName();
return key;
}
/** Returns the Preferences Node which can use
* for position storings.
*
* @return
*/
public static Preferences getPreferences() {
return Preferences.userNodeForPackage(PositionManager.class).node(
"positions");
}
/** Returns an int value from the preferences.
*
* You can use the extension to store
* differ values for on component
*
* @param comp the component
* @param extension a value to save different values for the component
* @param defaultValue the default value
* @return the value, if available or the default value
*
* @see #setIntPos(Component, String, int)
*/
public static int getIntPos(
Component comp,
String extension,
int defaultValue) {
Preferences p = getPreferences();
return p.getInt(getKey(comp) + extension, defaultValue);
}
/** Implements a test case
*
* @param args
*/
public static void main(String[] args) {
// Main Frame
GPFrame f = new GPFrame();
f.setName("myFrame");
// Desktop
JDesktopPane dtp = new JDesktopPane();
f.getContentPane().add(dtp);
// internal frame
GPInternalFrame jif = new GPInternalFrame();
//jif.registerDefaultEscAction();
jif.setClosable(true);
jif.setResizable(true);
jif.setVisible(true);
dtp.add(jif);
// panel for th internal frame
JPanel p = new JPanel(new BorderLayout());
jif.getContentPane().add(p);
// Split pane for the panel
GPSplitPane jsp =
new GPSplitPane(
JSplitPane.VERTICAL_SPLIT,
new JPanel(),
new JPanel());
jsp.setName("MyName");
jsp.setSize(400, 400);
p.add(jsp, BorderLayout.CENTER);
// button for the panel
JButton b = new JButton();
b.setName("Ok");
p.add(b, BorderLayout.SOUTH);
// show the window
// don't call the pack method
// otherwise the size will recalculate
//f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
PositionManager.removeComponent((JFrame) e.getSource());
System.exit(0);
}
});
}
}
/** This property change listener
* stores some special properties from
* component objects
* at the user preferences.
*
* @author sven_luzar
* @version 1.0
*
*/
class PosPropertyChangeListener implements PropertyChangeListener {
/** Stores the property value for some special
* properties.
*
* The method considers the property
* <ul>
* <li>{@link JSplitPane#DIVIDER_LOCATION_PROPERTY}</li>
* </ul>
*
* @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
*/
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getSource() == null)
return;
if (evt.getPropertyName() == JSplitPane.DIVIDER_LOCATION_PROPERTY
&& ((Integer) evt.getOldValue()).intValue() != -1) {
if (evt.getSource() instanceof JSplitPane) {
try {
JSplitPane jsp = (JSplitPane) evt.getSource();
PositionManager.setIntPos(
jsp,
PositionManager.DIVIDER_LOCATION,
jsp.getDividerLocation());
} catch (Exception ex) {
System.err.println(ex);
}
}
}
}
}
/** The component listener registers each movement
* from a component and stores the
* bounds from this comonent in the user
* preferences.
*
* @author sven_luzar
* @version 1.0
*
*/
class PosComponentListener implements ComponentListener {
/** calls {@link #storeComponentPosition(ComponentEvent)}
*
*/
public void componentHidden(ComponentEvent e) {
storeComponentPosition(e);
}
/** calls {@link #storeComponentPosition(ComponentEvent)}
*
*/
public void componentMoved(ComponentEvent e) {
storeComponentPosition(e);
}
/** calls {@link #storeComponentPosition(ComponentEvent)}
*
*/
public void componentResized(ComponentEvent e) {
storeComponentPosition(e);
}
/** calls {@link #storeComponentPosition(ComponentEvent)}
*
*/
public void componentShown(ComponentEvent e) {
storeComponentPosition(e);
}
/** Stores the component bounds in the
* user preferences.
*
* @param e
* @see PositionManager#setIntPos(Component, String, int)
*
*/
private void storeComponentPosition(ComponentEvent e) {
Component c = e.getComponent();
try {
// get the bounds
Rectangle b = c.getBounds();
// store the bounds
PositionManager.setIntPos(
c,
PositionManager.FRAME_WIDTH,
b.width);
PositionManager.setIntPos(
c,
PositionManager.FRAME_HEIGHT,
b.height);
PositionManager.setIntPos(c, PositionManager.FRAME_X, b.x);
PositionManager.setIntPos(c, PositionManager.FRAME_Y, b.y);
// will store the state if it is a frame
if (c instanceof Frame) {
Frame f = (Frame) c;
PositionManager.setIntPos(
f,
PositionManager.FRAME_STATE,
f.getState());
}
} catch (Exception ex) {
System.err.println(ex);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?