📄 panelwindowcontainer.java
字号:
{ g.setColor(color1); g.drawLine(x + i * 4 + 2,y + 3, x + i * 4 + 2,y + 3); g.setColor(color2); g.drawLine(x + i * 4 + 3,y + 4, x + i * 4 + 3,y + 4); g.setColor(color1); g.drawLine(x + i * 4 + 4,y + 5, x + i * 4 + 4,y + 5); g.setColor(color2); g.drawLine(x + i * 4 + 5,y + 6, x + i * 4 + 5,y + 6); } } //}}} //{{{ paintVertBorder() method private void paintVertBorder(Graphics g, int x, int y, int height) { g.setColor(color3); g.fillRect(x,y,SPLITTER_WIDTH,height); for(int i = 0; i < height / 4 - 1; i++) { g.setColor(color1); g.drawLine(x + 3,y + i * 4 + 2, x + 3,y + i * 4 + 2); g.setColor(color2); g.drawLine(x + 4,y + i * 4 + 3, x + 4,y + i * 4 + 3); g.setColor(color1); g.drawLine(x + 5,y + i * 4 + 4, x + 5,y + i * 4 + 4); g.setColor(color2); g.drawLine(x + 6,y + i * 4 + 5, x + 6,y + i * 4 + 5); } } //}}} //{{{ updateColors() method private void updateColors() { if(UIManager.getLookAndFeel() instanceof MetalLookAndFeel) { color1 = MetalLookAndFeel.getControlHighlight(); color2 = MetalLookAndFeel.getControlDarkShadow(); color3 = MetalLookAndFeel.getControl(); } else { color1 = color2 = color3 = null; } } //}}} } //}}} //{{{ RotatedTextIcon class public static class RotatedTextIcon implements Icon { public static final int NONE = 0; public static final int CW = 1; public static final int CCW = 2; //{{{ RotatedTextIcon constructor public RotatedTextIcon(int rotate, Font font, String text) { this.rotate = rotate; this.font = font; FontRenderContext fontRenderContext = new FontRenderContext(null,true,true); this.text = text; glyphs = font.createGlyphVector(fontRenderContext,text); width = (int)glyphs.getLogicalBounds().getWidth() + 4; //height = (int)glyphs.getLogicalBounds().getHeight(); LineMetrics lineMetrics = font.getLineMetrics(text,fontRenderContext); ascent = lineMetrics.getAscent(); height = (int)lineMetrics.getHeight(); renderHints = new RenderingHints( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); renderHints.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); renderHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); } //}}} //{{{ getIconWidth() method public int getIconWidth() { return (int)(rotate == RotatedTextIcon.CW || rotate == RotatedTextIcon.CCW ? height : width); } //}}} //{{{ getIconHeight() method public int getIconHeight() { return (int)(rotate == RotatedTextIcon.CW || rotate == RotatedTextIcon.CCW ? width : height); } //}}} //{{{ paintIcon() method public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2d = (Graphics2D)g; g2d.setFont(font); AffineTransform oldTransform = g2d.getTransform(); RenderingHints oldHints = g2d.getRenderingHints(); g2d.setRenderingHints(renderHints); g2d.setColor(c.getForeground()); //{{{ No rotation if(rotate == RotatedTextIcon.NONE) { g2d.drawGlyphVector(glyphs,x + 2,y + ascent); } //}}} //{{{ Clockwise rotation else if(rotate == RotatedTextIcon.CW) { AffineTransform trans = new AffineTransform(); trans.concatenate(oldTransform); trans.translate(x,y + 2); trans.rotate(Math.PI / 2, height / 2, width / 2); g2d.setTransform(trans); g2d.drawGlyphVector(glyphs,(height - width) / 2, (width - height) / 2 + ascent); } //}}} //{{{ Counterclockwise rotation else if(rotate == RotatedTextIcon.CCW) { AffineTransform trans = new AffineTransform(); trans.concatenate(oldTransform); trans.translate(x,y - 2); trans.rotate(Math.PI * 3 / 2, height / 2, width / 2); g2d.setTransform(trans); g2d.drawGlyphVector(glyphs,(height - width) / 2, (width - height) / 2 + ascent); } //}}} g2d.setTransform(oldTransform); g2d.setRenderingHints(oldHints); } //}}} //{{{ Private members private int rotate; private Font font; private String text; private GlyphVector glyphs; private float width; private float height; private float ascent; private RenderingHints renderHints; //}}} } //}}} //{{{ ButtonLayout class class ButtonLayout implements LayoutManager { //{{{ addLayoutComponent() method public void addLayoutComponent(String name, Component comp) {} //}}} //{{{ removeLayoutComponent() method public void removeLayoutComponent(Component comp) {} //}}} //{{{ preferredLayoutSize() method public Dimension preferredLayoutSize(Container parent) { Insets insets = ((JComponent)parent).getBorder() .getBorderInsets((JComponent)parent); Component[] comp = parent.getComponents(); if(comp.length == 2) { // nothing 'cept close box and popup button return new Dimension(0,0); } else { if(position.equals(DockableWindowManager.TOP) || position.equals(DockableWindowManager.BOTTOM)) { return new Dimension(0, comp[2].getPreferredSize().height + insets.top + insets.bottom); } else { return new Dimension( comp[2].getPreferredSize().width + insets.left + insets.right,0); } } } //}}} //{{{ minimumLayoutSize() method public Dimension minimumLayoutSize(Container parent) { return preferredLayoutSize(parent); } //}}} //{{{ layoutContainer() method public void layoutContainer(Container parent) { Insets insets = ((JComponent)parent).getBorder() .getBorderInsets((JComponent)parent); Component[] comp = parent.getComponents(); if(comp.length != 2) { boolean closeBoxSizeSet = false; boolean noMore = false; popupButton.setVisible(false); Dimension parentSize = parent.getSize(); int pos = (position.equals(DockableWindowManager.TOP) || position.equals(DockableWindowManager.BOTTOM) ) ? 0 : insets.left; for(int i = 2; i < comp.length; i++) { Dimension size = comp[i].getPreferredSize(); if(position.equals(DockableWindowManager.TOP) || position.equals(DockableWindowManager.BOTTOM)) { if(!closeBoxSizeSet) { closeBox.setBounds(pos, insets.top, size.height,size.height); pos += size.height; closeBoxSizeSet = true; } if(noMore || pos + size.width > parentSize.width - (i == comp.length - 1 ? 0 : closeBox.getWidth())) { popupButton.setBounds( parentSize.width - size.height - insets.right, insets.top,size.height, size.height); popupButton.setVisible(true); comp[i].setVisible(false); noMore = true; } else { comp[i].setBounds(pos,insets.top, size.width,size.height); comp[i].setVisible(true); pos += size.width; } } else { if(!closeBoxSizeSet) { closeBox.setBounds(insets.left, insets.top,size.width,size.width); pos += size.width; closeBoxSizeSet = true; } if(noMore || pos + size.height > parentSize.height - (i == comp.length - 1 ? 0 : closeBox.getHeight())) { popupButton.setBounds( insets.top, parentSize.height - size.width, size.width,size.width); popupButton.setVisible(true); comp[i].setVisible(false); noMore = true; } else { comp[i].setBounds(insets.left, pos,size.width,size.height); comp[i].setVisible(true); pos += size.height; } } } } } //}}} } //}}} //{{{ DockablePanel class class DockablePanel extends JPanel { //{{{ DockablePanel constructor DockablePanel() { super(new CardLayout()); ResizeMouseHandler resizeMouseHandler = new ResizeMouseHandler(); addMouseListener(resizeMouseHandler); addMouseMotionListener(resizeMouseHandler); } //}}} //{{{ getWindowContainer() method PanelWindowContainer getWindowContainer() { return PanelWindowContainer.this; } //}}} //{{{ showDockable() method void showDockable(String name) { ((CardLayout)getLayout()).show(this,name); } //}}} //{{{ getMinimumSize() method public Dimension getMinimumSize() { return new Dimension(0,0); } //}}} //{{{ getPreferredSize() method public Dimension getPreferredSize() { if(current == null) return new Dimension(0,0); else { if(dimension <= 0) { int width = super.getPreferredSize().width; dimension = width - SPLITTER_WIDTH - 3; } if(position.equals(DockableWindowManager.TOP) || position.equals(DockableWindowManager.BOTTOM)) { return new Dimension(0, dimension + SPLITTER_WIDTH + 3); } else { return new Dimension(dimension + SPLITTER_WIDTH + 3, 0); } } } //}}} //{{{ ResizeMouseHandler class class ResizeMouseHandler extends MouseAdapter implements MouseMotionListener { boolean canDrag; int dragStartDimension; Point dragStart; //{{{ mousePressed() method public void mousePressed(MouseEvent evt) { dragStartDimension = dimension; dragStart = evt.getPoint(); } //}}} //{{{ mouseMoved() method public void mouseMoved(MouseEvent evt) { Border border = getBorder(); if(border == null) { // collapsed return; } Insets insets = border.getBorderInsets(DockablePanel.this); int cursor = Cursor.DEFAULT_CURSOR; canDrag = false; //{{{ Top... if(position.equals(DockableWindowManager.TOP)) { if(evt.getY() >= getHeight() - insets.bottom) { cursor = Cursor.N_RESIZE_CURSOR; canDrag = true; } } //}}} //{{{ Left... else if(position.equals(DockableWindowManager.LEFT)) { if(evt.getX() >= getWidth() - insets.right) { cursor = Cursor.W_RESIZE_CURSOR; canDrag = true; } } //}}} //{{{ Bottom... else if(position.equals(DockableWindowManager.BOTTOM)) { if(evt.getY() <= insets.top) { cursor = Cursor.S_RESIZE_CURSOR; canDrag = true; } } //}}} //{{{ Right... else if(position.equals(DockableWindowManager.RIGHT)) { if(evt.getX() <= insets.left) { cursor = Cursor.E_RESIZE_CURSOR; canDrag = true; } } //}}} setCursor(Cursor.getPredefinedCursor(cursor)); } //}}} //{{{ mouseDragged() method public void mouseDragged(MouseEvent evt) { if(!canDrag) return; if(dragStart == null) // can't happen? return; //{{{ Top... if(position.equals(DockableWindowManager.TOP)) { dimension = evt.getY() + dragStartDimension - dragStart.y; } //}}} //{{{ Left... else if(position.equals(DockableWindowManager.LEFT)) { dimension = evt.getX() + dragStartDimension - dragStart.x; } //}}} //{{{ Bottom... else if(position.equals(DockableWindowManager.BOTTOM)) { dimension += (dragStart.y - evt.getY()); } //}}} //{{{ Right... else if(position.equals(DockableWindowManager.RIGHT)) { dimension += (dragStart.x - evt.getX()); } //}}} if(dimension <= 0) dimension = dragStartDimension; wm.invalidate(); wm.validate(); } //}}} //{{{ mouseExited() method public void mouseExited(MouseEvent evt) { setCursor(Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR)); } //}}} } //}}} } //}}} //}}}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -