basictaskpaneui.java
来自「java实现浏览器等本地桌面的功能」· Java 代码 · 共 600 行 · 第 1/2 页
JAVA
600 行
} public int getIconWidth() { return 6; } public void paintIcon(Component c, Graphics g, int x, int y) { if (up) { g.drawLine(x + 3, y, x, y + 3); g.drawLine(x + 3, y, x + 6, y + 3); } else { g.drawLine(x, y, x + 3, y + 3); g.drawLine(x + 3, y + 3, x + 6, y); } } } /** * The border around the content pane */ protected static class ContentPaneBorder implements Border { Color color; public ContentPaneBorder(Color color) { this.color = color; } public Insets getBorderInsets(Component c) { return new Insets(0, 1, 1, 1); } public boolean isBorderOpaque() { return true; } public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { g.setColor(color); g.drawLine(x, y, x, y + height - 1); g.drawLine(x, y + height - 1, x + width - 1, y + height - 1); g.drawLine(x + width - 1, y, x + width - 1, y + height - 1); } } /** * The border of the taskpane group paints the "text", the "icon", the * "expanded" status and the "special" type. * */ protected class PaneBorder implements Border { protected Color borderColor; protected Color titleForeground; protected Color specialTitleBackground; protected Color specialTitleForeground; protected Color titleBackgroundGradientStart; protected Color titleBackgroundGradientEnd; protected Color titleOver; protected Color specialTitleOver; protected JLabel label; public PaneBorder() { borderColor = UIManager.getColor("TaskPane.borderColor"); titleForeground = UIManager.getColor("TaskPane.titleForeground"); specialTitleBackground = UIManager .getColor("TaskPane.specialTitleBackground"); specialTitleForeground = UIManager .getColor("TaskPane.specialTitleForeground"); titleBackgroundGradientStart = UIManager .getColor("TaskPane.titleBackgroundGradientStart"); titleBackgroundGradientEnd = UIManager .getColor("TaskPane.titleBackgroundGradientEnd"); titleOver = UIManager.getColor("TaskPane.titleOver"); if (titleOver == null) { titleOver = specialTitleBackground.brighter(); } specialTitleOver = UIManager.getColor("TaskPane.specialTitleOver"); if (specialTitleOver == null) { specialTitleOver = specialTitleBackground.brighter(); } label = new JLabel(); label.setOpaque(false); label.setIconTextGap(8); } public Insets getBorderInsets(Component c) { return new Insets(getTitleHeight(), 0, 0, 0); } public boolean isBorderOpaque() { return true; } /** * Calculates the preferred border size, its size so all its content fits. */ public Dimension getPreferredSize(JXTaskPane group) { // calculate the title width so it is fully visible // it starts with the title width configureLabel(group); Dimension dim = label.getPreferredSize(); // add the title left offset dim.width += 3; // add the controls width dim.width += TITLE_HEIGHT; // and some space between label and controls dim.width += 3; dim.height = getTitleHeight(); return dim; } protected void paintTitleBackground(JXTaskPane group, Graphics g) { if (group.isSpecial()) { g.setColor(specialTitleBackground); } else { g.setColor(titleBackgroundGradientStart); } g.fillRect(0, 0, group.getWidth(), getTitleHeight() - 1); } protected void paintTitle( JXTaskPane group, Graphics g, Color textColor, int x, int y, int width, int height) { configureLabel(group); label.setForeground(textColor); g.translate(x, y); label.setBounds(0, 0, width, height); label.paint(g); g.translate(-x, -y); } protected void configureLabel(JXTaskPane group) { label.applyComponentOrientation(group.getComponentOrientation()); label.setFont(group.getFont()); label.setText(group.getTitle()); label.setIcon( group.getIcon() == null ? new EmptyIcon() : group.getIcon()); } protected void paintExpandedControls(JXTaskPane group, Graphics g, int x, int y, int width, int height) {} protected Color getPaintColor(JXTaskPane group) { Color paintColor; if (isMouseOverBorder()) { if (mouseOver) { if (group.isSpecial()) { paintColor = specialTitleOver; } else { paintColor = titleOver; } } else { if (group.isSpecial()) { paintColor = specialTitleForeground; } else { paintColor = titleForeground; } } } else { if (group.isSpecial()) { paintColor = specialTitleForeground; } else { paintColor = titleForeground; } } return paintColor; } public void paintBorder( Component c, Graphics g, int x, int y, int width, int height) { JXTaskPane group = (JXTaskPane)c; // calculate position of title and toggle controls int controlWidth = TITLE_HEIGHT - 2 * ROUND_HEIGHT; int controlX = group.getWidth() - TITLE_HEIGHT; int controlY = ROUND_HEIGHT - 1; int titleX = 3; int titleY = 0; int titleWidth = group.getWidth() - getTitleHeight() - 3; int titleHeight = getTitleHeight(); if (!group.getComponentOrientation().isLeftToRight()) { controlX = group.getWidth() - controlX - controlWidth; titleX = group.getWidth() - titleX - titleWidth; } // paint the title background paintTitleBackground(group, g); // paint the the toggles paintExpandedControls(group, g, controlX, controlY, controlWidth, controlWidth); // paint the title text and icon Color paintColor = getPaintColor(group); // focus painted same color as text if (group.hasFocus()) { g.setColor(paintColor); BasicGraphicsUtils.drawDashedRect( g, 3, 3, width - 6, getTitleHeight() - 6); } paintTitle( group, g, paintColor, titleX, titleY, titleWidth, titleHeight); } protected void paintRectAroundControls(JXTaskPane group, Graphics g, int x, int y, int width, int height, Color highColor, Color lowColor) { if (mouseOver) { int x2 = x + width; int y2 = y + height; g.setColor(highColor); g.drawLine(x, y, x2, y); g.drawLine(x, y, x, y2); g.setColor(lowColor); g.drawLine(x2, y, x2, y2); g.drawLine(x, y2, x2, y2); } } protected void paintOvalAroundControls(JXTaskPane group, Graphics g, int x, int y, int width, int height) { if (group.isSpecial()) { g.setColor(specialTitleBackground.brighter()); g.drawOval( x, y, width, height); } else { g.setColor(titleBackgroundGradientStart); g.fillOval( x, y, width, height); g.setColor(titleBackgroundGradientEnd.darker()); g.drawOval( x, y, width, width); } } protected void paintChevronControls(JXTaskPane group, Graphics g, int x, int y, int width, int height) { ChevronIcon chevron; if (group.isExpanded()) { chevron = new ChevronIcon(true); } else { chevron = new ChevronIcon(false); } int chevronX = x + width / 2 - chevron.getIconWidth() / 2; int chevronY = y + (height / 2 - chevron.getIconHeight()); chevron.paintIcon(group, g, chevronX, chevronY); chevron.paintIcon( group, g, chevronX, chevronY + chevron.getIconHeight() + 1); } /** * Default implementation returns false. * * @return true if this border wants to display things differently when the * mouse is over it */ protected boolean isMouseOverBorder() { return false; } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?