📄 windowsupport.java
字号:
* @param width the horizontal size of the window, if less than or equal to * zero the content size will be used. * @param height the vertical size of the window, if less than or equal to * zero the content size will be used. */ public void displayInWindow(int x, int y, int width, int height) { displayInWindow(null, x, y, width, height); } /** * Display the window. * * @param owner Frame for JDialog * @param x the horizontal pixel location for the window. * @param y the vertical pixel location for the window. * @param width the horizontal size of the window, if less than or equal to * zero the content size will be used. * @param height the vertical size of the window, if less than or equal to * zero the content size will be used. */ public void displayInWindow(Frame owner, int x, int y, int width, int height) { if (content == null) { Debug.message("windowsupport", "WindowSupport asked to display window with null content"); return; } if (x < 0 && y < 0) { // See if we can remember where we were... Point loc = getComponentLocation(); if (loc != null) { x = (int) loc.getX(); y = (int) loc.getY(); } } if (display == null) { display = createDisplay(owner); } Container displayWindow = display.getWindow(); checkBounds(displayWindow, x, y, width, height); display.show(x, y); setComponentLocation(displayWindow.getLocation()); setComponentSize(displayWindow.getSize()); } /** * Checks the component's dimensions against the requested values and * against any maximum limits that may have been set in the WindowSupport. * Calls setBounds() on the Component. */ protected void checkBounds(Component comp, int x, int y, int width, int height) { if (comp != null) { if (width <= 0) { width = comp.getWidth(); } if (maxWidth > 0 && width > maxWidth) { width = maxWidth; } if (height <= 0) { height = comp.getHeight(); } if (maxHeight > 0 && height > maxHeight) { height = maxHeight; } comp.setBounds(x, y, width, height); } } /** * For applications, checks where the Environment says the window should be * placed, and then uses the packed height and width to make adjustments. */ protected static void setDefaultPosition(Component comp) { // get starting width and height int w = comp.getWidth(); int h = comp.getHeight(); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); Debug.message("basic", "Screen dimensions are " + d); int x = d.width / 2 - w / 2; int y = d.height / 2 - h / 2; if (Debug.debugging("basic")) { Debug.output("Setting PLG frame X and Y from properties to " + x + " " + y); } // compose the frame, but don't show it here comp.setBounds(x, y, w, h); } /** * Set the window to be hidden and fire a ComponentEvent for * COMPONENT_HIDDEN. Normally, just setting the visibility of the window * would be enough, but we're running into that problem we had with the * layers not firing ComponentEvents when hidden. This method calls * componentHidden, which in turn calls cleanUp. */ public void killWindow() { ComponentEvent ce = null; if (display != null) { ce = display.kill(); } if (ce != null) { componentHidden(ce); } } protected Class persistentDisplayType; /** * Get rid of the window used to display the content. */ protected void cleanUp() { if (display != null) { persistentDisplayType = display.getClass(); WSDisplay wsd = display; setDisplay(null); wsd.dispose(); } // This seems to half-disconnect the window support from the content, // and I am not sure it's necessary to remove the listeners. This was // originally done to make it easier to release memory, but having the // WindowSupport hold on to listeners won't prevent that from happening. // if (content instanceof ComponentListener) { // removeComponentListener((ComponentListener) content); // } } /** * Add a component listener that is interested in hearing about what happens * to the window. */ public void addComponentListener(ComponentListener l) { addListener(l); } /** * Remove a component listener that was interested in hearing about what * happens to the window. */ public void removeComponentListener(ComponentListener l) { removeListener(l); } /** * Return the window displaying the content. May be null. */ public Container getWindow() { if (display != null) { return display.getWindow(); } else { return null; } } public static interface WSDisplay { public void setTitle(String title); public String getTitle(); public Container getWindow(); public void setContent(Component content); public void show(int x, int y); public ComponentEvent kill(); public void dispose(); public Dimension getContentSize(); public void addComponentListener(ComponentListener cl); public void removeComponentListener(ComponentListener cl); } public static class IntrnlFrm extends JInternalFrame implements WSDisplay { public IntrnlFrm(String title) { super(title, /* resizable */true, /* closable */true, /* maximizable */true, /* iconifiable */true); setOpaque(true); JLayeredPane desktop = Environment.getInternalFrameDesktop(); Debug.message("windows", "WindowSupport creating internal frame"); if (desktop != null) { desktop.remove(this); desktop.add(this, JLayeredPane.PALETTE_LAYER); } else { Debug.output("WindowSupport: No desktop set for internal frame"); } } public Container getWindow() { return this; } public ComponentEvent kill() { setVisible(false); return new ComponentEvent(this, ComponentEvent.COMPONENT_HIDDEN); } public void setContent(Component content) { Container cp = getContentPane(); // cp.removeAll(); cp.add(content); pack(); } public Dimension getContentSize() { return getContentPane().getSize(); } public void show(int x, int y) { if (!isVisible()) { super.show(); } toFront(); } } public static class Dlg extends JDialog implements WSDisplay { public Dlg(Frame owner, String title) { super(owner, title); Debug.message("windows", "WindowSupport creating frame"); } public Container getWindow() { return this; } public ComponentEvent kill() { setVisible(false); return new ComponentEvent(this, ComponentEvent.COMPONENT_HIDDEN); } public void setContent(Component content) { Container cp = getContentPane(); cp.removeAll(); cp.add(content); pack(); } public Dimension getContentSize() { return getContentPane().getSize(); } public void show(int x, int y) { if (!isVisible()) { Window owner = getOwner(); if (x <= 0 && y <= 0) { if (owner != null) { setLocationRelativeTo(owner); } else if (owner == null) { setDefaultPosition(this); } } } super.setVisible(true); } } public static class Frm extends JFrame implements WSDisplay { public Frm(String title) { super(title); // Need to call this to get the frame to pay attention to requests // on where to locate it if it should be centered on the screen. setLocation(-1, -1); } public Frm(String title, boolean undecorated) { super(title); setUndecorated(undecorated); // Need to call this to get the frame to pay attention to requests // on where to locate it if it should be centered on the screen. setLocation(-1, -1); } public Container getWindow() { return this; } public ComponentEvent kill() { setVisible(false); return new ComponentEvent(this, ComponentEvent.COMPONENT_HIDDEN); } public void setContent(Component content) { Container cp = getContentPane(); cp.removeAll(); cp.add(content); pack(); } public Dimension getContentSize() { return getContentPane().getSize(); } public void show(int x, int y) { if (!isVisible()) { Window owner = getOwner(); if (x <= 0 && y <= 0) { if (owner != null) { setLocationRelativeTo(owner); } else if (owner == null) { setDefaultPosition(this); } } super.setVisible(true); } toFront(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -