From b315a6943e6c0d6bdf0d5f7565c570d719154d6c Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期三, 17 十二月 2025 14:56:58 +0800
Subject: [PATCH] 新增了障碍物管理页面
---
src/zhuye/buttonset.java | 128 +++++++++++++++++++++++++++++-------------
1 files changed, 88 insertions(+), 40 deletions(-)
diff --git a/src/zhuye/buttonset.java b/src/zhuye/buttonset.java
index 50bad9c..2d6df18 100644
--- a/src/zhuye/buttonset.java
+++ b/src/zhuye/buttonset.java
@@ -1,70 +1,118 @@
package zhuye;
+import java.awt.BasicStroke;
import java.awt.Color;
+import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
-
-import javax.swing.BorderFactory;
+import java.awt.FontMetrics;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.RenderingHints;
+import java.awt.geom.RoundRectangle2D;
import javax.swing.JButton;
+import javax.swing.border.EmptyBorder;
/**
- * 鎻愪緵缁熶竴鎸夐挳鏍峰紡鐨勫伐鍘傛柟娉曘��
+ * 鎻愪緵缁熶竴鎸夐挳鏍峰紡鐨勫伐鍘傛柟娉曪紙缇庡寲鐗堬級銆�
+ * 椋庢牸涓� ObstacleManagementPage 淇濇寔涓�鑷达細鍦嗚銆佹墎骞冲寲銆佹姉閿娇銆�
*/
public final class buttonset {
+
+ // 榛樿鎸夐挳灏哄
private static final Dimension DEFAULT_SIZE = new Dimension(90, 36);
+ // 鍦嗚鍗婂緞
+ private static final int CORNER_RADIUS = 8;
private buttonset() {
// 宸ュ叿绫讳笉闇�瑕佸疄渚嬪寲
}
/**
- * 鍒涘缓甯︽湁缁熶竴鏍峰紡鐨勬寜閽紝渚涘涓晫闈㈠鐢ㄣ��
+ * 鍒涘缓甯︽湁鐜颁唬鍖栫粺涓�鏍峰紡鐨勬寜閽紙鍦嗚銆佹墎骞抽鏍硷級銆�
*
* @param text 鎸夐挳鏄剧ず鏂囨湰
- * @param backgroundColor 鎸夐挳鑳屾櫙鑹�
+ * @param backgroundColor 鎸夐挳涓昏壊璋冿紙濡傛灉涓簄ull锛岄粯璁や娇鐢ㄧ伆钃濊壊锛�
* @return 宸插簲鐢ㄦ牱寮忕殑 JButton
*/
public static JButton createStyledButton(String text, Color backgroundColor) {
+ // 纭畾鍩虹棰滆壊锛屽鏋滄湭鎸囧畾鍒欎娇鐢ㄩ粯璁ょ殑閽㈣摑鑹�
final Color baseColor = backgroundColor != null ? backgroundColor : new Color(70, 130, 180);
- JButton button = new JButton(text);
+ // 鍒涘缓鑷畾涔夌粯鍒剁殑鎸夐挳
+ JButton button = new JButton(text) {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected void paintComponent(Graphics g) {
+ Graphics2D g2 = (Graphics2D) g.create();
+ // 寮�鍚姉閿娇锛屼娇鍦嗚鍜屾枃瀛楁洿骞虫粦
+ g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+ g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+
+ // 鑾峰彇鎸夐挳鐘舵��
+ boolean isPressed = getModel().isPressed();
+ boolean isRollover = getModel().isRollover();
+ int w = getWidth();
+ int h = getHeight();
+
+ // 璁$畻褰撳墠鑳屾櫙鑹�
+ if (isPressed) {
+ g2.setColor(baseColor.darker()); // 鎸変笅鍙樻繁
+ } else if (isRollover) {
+ // 鎮仠鏃剁◢寰�忔槑鎴栧彉浜紝澧炲姞浜や簰鎰�
+ g2.setColor(new Color(baseColor.getRed(), baseColor.getGreen(), baseColor.getBlue(), 220));
+ } else {
+ g2.setColor(baseColor); // 姝e父棰滆壊
+ }
+
+ // 缁樺埗鍦嗚鐭╁舰鑳屾櫙
+ g2.fill(new RoundRectangle2D.Double(0, 0, w, h, CORNER_RADIUS, CORNER_RADIUS));
+
+ // 濡傛灉闇�瑕佸湪鎸変笅鏃舵湁杞诲井鐨勮竟妗嗘晥鏋滐紙鍙�夛紝澧炲姞绔嬩綋鎰燂級
+ if (isPressed) {
+ g2.setColor(new Color(0, 0, 0, 30));
+ g2.draw(new RoundRectangle2D.Double(0.5, 0.5, w - 1, h - 1, CORNER_RADIUS, CORNER_RADIUS));
+ }
+
+ // 缁樺埗鏂囧瓧
+ g2.setColor(Color.WHITE);
+ g2.setFont(getFont());
+ FontMetrics fm = g2.getFontMetrics();
+
+ // 绮剧‘璁$畻鏂囧瓧灞呬腑浣嶇疆
+ int textX = (w - fm.stringWidth(getText())) / 2;
+ // 娉ㄦ剰锛歽鍧愭爣鏄熀绾夸綅缃紝闇�瑕佸姞涓� Ascent 骞跺噺鍘婚珮搴︾殑涓�鍗�
+ int textY = (h - fm.getHeight()) / 2 + fm.getAscent();
+
+ g2.drawString(getText(), textX, textY);
+
+ g2.dispose();
+ }
+ };
+
+ // 鍩虹灞炴�ц缃�
button.setFont(new Font("寰蒋闆呴粦", Font.BOLD, 14));
- button.setBackground(baseColor);
button.setForeground(Color.WHITE);
- button.setFocusPainted(false);
- button.setBorder(BorderFactory.createEmptyBorder(8, 18, 8, 18));
+ button.setFocusPainted(false); // 鍘婚櫎鐒︾偣铏氱嚎妗�
+ button.setContentAreaFilled(false); // 鍘婚櫎榛樿鑳屾櫙缁樺埗锛堢敱paintComponent鎺ョ锛�
+ button.setBorderPainted(false); // 鍘婚櫎榛樿杈规
+ button.setCursor(new Cursor(Cursor.HAND_CURSOR)); // 榧犳爣鍙樻垚鎵嬪瀷
+
+ // 璁剧疆鍐呰竟璺濓紙铏界劧涓昏鐢眕aintComponent鎺у埗锛屼絾瀵瑰竷灞�璁$畻鏈夊府鍔╋級
+ button.setBorder(new EmptyBorder(8, 18, 8, 18));
- Dimension preferred = button.getPreferredSize();
- int width = Math.max(preferred.width, DEFAULT_SIZE.width);
- int height = Math.max(preferred.height, DEFAULT_SIZE.height);
- Dimension adjustedSize = new Dimension(width, height);
-
- button.setPreferredSize(adjustedSize);
- button.setMinimumSize(new Dimension(DEFAULT_SIZE.width, DEFAULT_SIZE.height));
- button.setMaximumSize(adjustedSize);
-
- button.addMouseListener(new java.awt.event.MouseAdapter() {
- @Override
- public void mouseEntered(java.awt.event.MouseEvent event) {
- button.setBackground(brightenColor(baseColor));
- }
-
- @Override
- public void mouseExited(java.awt.event.MouseEvent event) {
- button.setBackground(baseColor);
- }
- });
+ // 灏哄璁$畻閫昏緫
+ Dimension preferred = button.getPreferredSize();
+ // 纭繚瀹藉害涓嶅皬浜庨粯璁ゅ�硷紝涓旀牴鎹枃瀛楅暱搴﹁嚜閫傚簲
+ int width = Math.max(preferred.width + 20, DEFAULT_SIZE.width);
+ int height = Math.max(preferred.height, DEFAULT_SIZE.height);
+
+ Dimension finalSize = new Dimension(width, height);
+ button.setPreferredSize(finalSize);
+ button.setMinimumSize(DEFAULT_SIZE);
+ button.setMaximumSize(finalSize); // 闃叉鍦ㄦ煇浜涘竷灞�涓杩囧害鎷変几
return button;
}
-
- private static Color brightenColor(Color color) {
- if (color == null) {
- return new Color(200, 200, 200);
- }
- int r = Math.min(255, color.getRed() + 30);
- int g = Math.min(255, color.getGreen() + 30);
- int b = Math.min(255, color.getBlue() + 30);
- return new Color(r, g, b);
- }
-}
+}
\ No newline at end of file
--
Gitblit v1.10.0