package com.xindao.ocr.swingui.swing.utils;
|
|
import javax.swing.*;
|
import javax.swing.border.EmptyBorder;
|
import java.awt.*;
|
import java.awt.geom.RoundRectangle2D;
|
|
/**
|
* 生成自定义主键工具类
|
*/
|
public class GenerateCustomizeComponent {
|
|
private static final Color PRIMARY_LIGHT = new Color(100, 150, 255);
|
private static final Color PRIMARY_COLOR = new Color(66, 133, 244);
|
private static final Color TEXT_COLOR = new Color(51, 51, 51);
|
|
private static final Color SECONDARY_COLOR = new Color(76, 175, 80);
|
private static final Color CARD_COLOR = new Color(255, 255, 255);
|
|
// 创建卡片式面板(带阴影和圆角)
|
public static JPanel createCardPanel() {
|
JPanel panel = new JPanel() {
|
@Override
|
protected void paintComponent(Graphics g) {
|
super.paintComponent(g);
|
Graphics2D g2d = (Graphics2D) g.create();
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
// 绘制背景
|
g2d.setColor(CARD_COLOR);
|
g2d.fill(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), 10, 10));
|
|
// 绘制阴影
|
g2d.setColor(new Color(0, 0, 0, 10));
|
for (int i = 0; i < 3; i++) {
|
g2d.draw(new RoundRectangle2D.Double(i, i, getWidth() - 2*i, getHeight() - 2*i, 10, 10));
|
}
|
|
g2d.dispose();
|
}
|
};
|
panel.setOpaque(false);
|
panel.setBackground(CARD_COLOR);
|
return panel;
|
}
|
|
|
// 创建样式化面板
|
public static JPanel createStyledPanel(LayoutManager layout) {
|
JPanel panel = new JPanel(layout);
|
panel.setOpaque(false);
|
return panel;
|
}
|
|
// 创建主要按钮(强调色)
|
public static JButton createPrimaryButton(String text,Font DEFAULT_FONT) {
|
JButton button = new JButton(text) {
|
@Override
|
protected void paintComponent(Graphics g) {
|
Graphics2D g2d = (Graphics2D) g.create();
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
// 渐变背景
|
GradientPaint gradient;
|
if (getModel().isPressed()) {
|
gradient = new GradientPaint(0, 0, PRIMARY_COLOR.darker(), 0, getHeight(), PRIMARY_COLOR.darker().darker());
|
} else if (getModel().isRollover()) {
|
gradient = new GradientPaint(0, 0, PRIMARY_LIGHT, 0, getHeight(), PRIMARY_COLOR);
|
} else {
|
gradient = new GradientPaint(0, 0, PRIMARY_COLOR, 0, getHeight(), PRIMARY_LIGHT);
|
}
|
|
g2d.setPaint(gradient);
|
g2d.fill(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), 6, 6));
|
super.paintComponent(g);
|
g2d.dispose();
|
}
|
};
|
|
button.setFont(new Font(DEFAULT_FONT.getName(), Font.BOLD, 14));
|
button.setForeground(Color.WHITE);
|
button.setBorder(new EmptyBorder(8, 15, 8, 15));
|
button.setContentAreaFilled(false);
|
button.setFocusPainted(false);
|
button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
|
return button;
|
}
|
|
// 创建样式化按钮
|
public static JButton createStyledButton(String text,Font font) {
|
JButton button = new JButton(text) {
|
@Override
|
protected void paintComponent(Graphics g) {
|
Graphics2D g2d = (Graphics2D) g.create();
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
if (getModel().isPressed()) {
|
g2d.setColor(new Color(230, 230, 230));
|
} else if (getModel().isRollover()) {
|
g2d.setColor(new Color(240, 240, 240));
|
} else {
|
g2d.setColor(new Color(235, 235, 235));
|
}
|
|
g2d.fill(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), 6, 6));
|
super.paintComponent(g);
|
g2d.dispose();
|
}
|
};
|
|
button.setFont(font);
|
button.setForeground(TEXT_COLOR);
|
button.setBorder(new EmptyBorder(8, 15, 8, 15));
|
button.setContentAreaFilled(false);
|
button.setFocusPainted(false);
|
button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
|
return button;
|
}
|
|
}
|