📄 basictoolbarui.java
字号:
*/ protected Border createNonRolloverBorder() { UIDefaults table = UIManager.getLookAndFeelDefaults(); return new BorderUIResource.CompoundBorderUIResource(new BasicBorders.ButtonBorder( table.getColor("Button.shadow"), table.getColor("Button.darkShadow"), table.getColor("Button.light"), table.getColor("Button.highlight")), new BasicBorders.RolloverMarginBorder()); } /** * Creates a non rollover border for Toggle buttons in the toolbar. */ private Border createNonRolloverToggleBorder() { UIDefaults table = UIManager.getLookAndFeelDefaults(); return new BorderUIResource.CompoundBorderUIResource(new BasicBorders.RadioButtonBorder( table.getColor("ToggleButton.shadow"), table.getColor("ToggleButton.darkShadow"), table.getColor("ToggleButton.light"), table.getColor("ToggleButton.highlight")), new BasicBorders.RolloverMarginBorder()); } /** * No longer used, use BasicToolBarUI.createFloatingWindow(JToolBar) * @see #createFloatingWindow */ protected JFrame createFloatingFrame(JToolBar toolbar) { Window window = SwingUtilities.getWindowAncestor(toolbar); JFrame frame = new JFrame(toolbar.getName(), (window != null) ? window.getGraphicsConfiguration() : null) { // Override createRootPane() to automatically resize // the frame when contents change protected JRootPane createRootPane() { JRootPane rootPane = new JRootPane() { private boolean packing = false; public void validate() { super.validate(); if (!packing) { packing = true; pack(); packing = false; } } }; rootPane.setOpaque(true); return rootPane; } }; frame.setResizable(false); WindowListener wl = createFrameListener(); frame.addWindowListener(wl); return frame; } /** * Creates a window which contains the toolbar after it has been * dragged out from its container * @return a <code>RootPaneContainer</code> object, containing the toolbar. */ protected RootPaneContainer createFloatingWindow(JToolBar toolbar) { class ToolBarDialog extends JDialog { public ToolBarDialog(Frame owner, String title, boolean modal) { super(owner, title, modal); } public ToolBarDialog(Dialog owner, String title, boolean modal) { super(owner, title, modal); } // Override createRootPane() to automatically resize // the frame when contents change protected JRootPane createRootPane() { JRootPane rootPane = new JRootPane() { private boolean packing = false; public void validate() { super.validate(); if (!packing) { packing = true; pack(); packing = false; } } }; rootPane.setOpaque(true); return rootPane; } } JDialog dialog; Window window = SwingUtilities.getWindowAncestor(toolbar); if (window instanceof Frame) { dialog = new ToolBarDialog((Frame)window, toolbar.getName(), false); } else if (window instanceof Dialog) { dialog = new ToolBarDialog((Dialog)window, toolbar.getName(), false); } else { dialog = new ToolBarDialog((Frame)null, toolbar.getName(), false); } dialog.setTitle(toolbar.getName()); dialog.setResizable(false); WindowListener wl = createFrameListener(); dialog.addWindowListener(wl); return dialog; } protected DragWindow createDragWindow(JToolBar toolbar) { Window frame = null; if(toolBar != null) { Container p; for(p = toolBar.getParent() ; p != null && !(p instanceof Window) ; p = p.getParent()); if(p != null && p instanceof Window) frame = (Window) p; } if(floatingToolBar == null) { floatingToolBar = createFloatingWindow(toolBar); } if (floatingToolBar instanceof Window) frame = (Window) floatingToolBar; DragWindow dragWindow = new DragWindow(frame); return dragWindow; } /** * Returns a flag to determine whether rollover button borders * are enabled. * * @return true if rollover borders are enabled; false otherwise * @see #setRolloverBorders * @since 1.4 */ public boolean isRolloverBorders() { return rolloverBorders; } /** * Sets the flag for enabling rollover borders on the toolbar and it will * also install the apropriate border depending on the state of the flag. * * @param rollover if true, rollover borders are installed. * Otherwise non-rollover borders are installed * @see #isRolloverBorders * @since 1.4 */ public void setRolloverBorders( boolean rollover ) { rolloverBorders = rollover; if ( rolloverBorders ) { installRolloverBorders( toolBar ); } else { installNonRolloverBorders( toolBar ); } } /** * Installs rollover borders on all the child components of the JComponent. * <p> * This is a convenience method to call <code>setBorderToRollover</code> * for each child component. * * @param c container which holds the child components (usally a JToolBar) * @see #setBorderToRollover * @since 1.4 */ protected void installRolloverBorders ( JComponent c ) { // Put rollover borders on buttons Component[] components = c.getComponents(); for ( int i = 0; i < components.length; ++i ) { if ( components[ i ] instanceof JComponent ) { ( (JComponent)components[ i ] ).updateUI(); setBorderToRollover( components[ i ] ); } } } /** * Installs non-rollover borders on all the child components of the JComponent. * A non-rollover border is the border that is installed on the child component * while it is in the toolbar. * <p> * This is a convenience method to call <code>setBorderToNonRollover</code> * for each child component. * * @param c container which holds the child components (usally a JToolBar) * @see #setBorderToNonRollover * @since 1.4 */ protected void installNonRolloverBorders ( JComponent c ) { // Put non-rollover borders on buttons. These borders reduce the margin. Component[] components = c.getComponents(); for ( int i = 0; i < components.length; ++i ) { if ( components[ i ] instanceof JComponent ) { ( (JComponent)components[ i ] ).updateUI(); setBorderToNonRollover( components[ i ] ); } } } /** * Installs normal borders on all the child components of the JComponent. * A normal border is the original border that was installed on the child * component before it was added to the toolbar. * <p> * This is a convenience method to call <code>setBorderNormal</code> * for each child component. * * @param c container which holds the child components (usally a JToolBar) * @see #setBorderToNonRollover * @since 1.4 */ protected void installNormalBorders ( JComponent c ) { // Put back the normal borders on buttons Component[] components = c.getComponents(); for ( int i = 0; i < components.length; ++i ) { setBorderToNormal( components[ i ] ); } } /** * Sets the border of the component to have a rollover border which * was created by <code>createRolloverBorder</code>. * * @param c component which will have a rollover border installed * @see #createRolloverBorder * @since 1.4 */ protected void setBorderToRollover(Component c) { if (c instanceof AbstractButton) { AbstractButton b = (AbstractButton)c; Border border = (Border)borderTable.get(b); if (border == null || border instanceof UIResource) { borderTable.put(b, b.getBorder()); } // Only set the border if its the default border if (b.getBorder() instanceof UIResource) { b.setBorder(rolloverBorder); } rolloverTable.put(b, b.isRolloverEnabled()? Boolean.TRUE: Boolean.FALSE); b.setRolloverEnabled(true); } } /** * Sets the border of the component to have a non-rollover border which * was created by <code>createNonRolloverBorder</code>. * * @param c component which will have a non-rollover border installed * @see #createNonRolloverBorder * @since 1.4 */ protected void setBorderToNonRollover(Component c) { if (c instanceof AbstractButton) { AbstractButton b = (AbstractButton)c; Border border = (Border)borderTable.get(b); if (border == null || border instanceof UIResource) { borderTable.put(b, b.getBorder()); } // Only set the border if its the default border if (b.getBorder() instanceof UIResource) { if (b instanceof JToggleButton) { ((JToggleButton)b).setBorder(nonRolloverToggleBorder); } else { b.setBorder(nonRolloverBorder); } } rolloverTable.put(b, b.isRolloverEnabled()? Boolean.TRUE: Boolean.FALSE); b.setRolloverEnabled(false); } } /** * Sets the border of the component to have a normal border. * A normal border is the original border that was installed on the child * component before it was added to the toolbar. * * @param c component which will have a normal border re-installed * @see #createNonRolloverBorder * @since 1.4 */ protected void setBorderToNormal(Component c) { if (c instanceof AbstractButton) { AbstractButton b = (AbstractButton)c; Border border = (Border)borderTable.remove(b); b.setBorder(border); Boolean value = (Boolean)rolloverTable.remove(b); if (value != null) { b.setRolloverEnabled(value.booleanValue()); } } } public Dimension getMinimumSize(JComponent c) { return getPreferredSize(c); } public Dimension getPreferredSize(JComponent c) { return null; } public Dimension getMaximumSize(JComponent c) { return getPreferredSize(c); } public void setFloatingLocation(int x, int y) { floatingX = x; floatingY = y; } public boolean isFloating() { return floating; } public void setFloating(boolean b, Point p) { if (toolBar.isFloatable() == true) { if (dragWindow != null) dragWindow.setVisible(false); this.floating = b; if (b == true) { if (dockingSource == null) { dockingSource = toolBar.getParent(); dockingSource.remove(toolBar); } Point l = new Point(); toolBar.getLocation(l); constraintBeforeFloating = calculateConstraint(dockingSource, l); if ( propertyListener != null ) UIManager.addPropertyChangeListener( propertyListener ); if (floatingToolBar == null) floatingToolBar = createFloatingWindow(toolBar); floatingToolBar.getContentPane().add(toolBar,BorderLayout.CENTER); setOrientation( JToolBar.HORIZONTAL ); if (floatingToolBar instanceof Window) ((Window)floatingToolBar).pack(); if (floatingToolBar instanceof Window) ((Window)floatingToolBar).setLocation(floatingX, floatingY); if (floatingToolBar instanceof Window) ((Window)floatingToolBar).show(); } else { if (floatingToolBar == null) floatingToolBar = createFloatingWindow(toolBar); if (floatingToolBar instanceof Window) ((Window)floatingToolBar).setVisible(false); floatingToolBar.getContentPane().remove(toolBar); String constraint = getDockingConstraint(dockingSource, p); int orientation = mapConstraintToOrientation(constraint); setOrientation(orientation); if (dockingSource== null) dockingSource = toolBar.getParent(); if ( propertyListener != null ) UIManager.removePropertyChangeListener( propertyListener ); dockingSource.add(constraint, toolBar); } dockingSource.invalidate(); Container dockingSourceParent = dockingSource.getParent(); if (dockingSourceParent != null) dockingSourceParent.validate(); dockingSource.repaint(); } } private int mapConstraintToOrientation(String constraint) { int orientation = toolBar.getOrientation(); if ( constraint != null ) { if ( constraint.equals(BorderLayout.EAST) || constraint.equals(BorderLayout.WEST) ) orientation = JToolBar.VERTICAL; else if ( constraint.equals(BorderLayout.NORTH) || constraint.equals(BorderLayout.SOUTH) ) orientation = JToolBar.HORIZONTAL; } return orientation; } public void setOrientation(int orientation) { toolBar.setOrientation( orientation ); if (dragWindow !=null) dragWindow.setOrientation(orientation); } /** * Gets the color displayed when over a docking area */ public Color getDockingColor() { return dockingColor; } /** * Sets the color displayed when over a docking area */ public void setDockingColor(Color c) { this.dockingColor = c; } /** * Gets the color displayed when over a floating area */ public Color getFloatingColor() { return floatingColor; } /** * Sets the color displayed when over a floating area */ public void setFloatingColor(Color c) { this.floatingColor = c; } public boolean canDock(Component c, Point p) { boolean b = false; if (c.contains(p)) { dockingSensitivity = (toolBar.getOrientation() == JToolBar.HORIZONTAL) ? toolBar.getSize().height : toolBar.getSize().width; // North if (p.y < dockingSensitivity) b = true; // South if (p.y > c.getSize().height-dockingSensitivity) b = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -