添加功能:1.标题栏再加上下划线;2.鼠标移动到按钮上时,对应的按钮会高亮

时间: 2024-04-16 16:28:32 浏览: 11
要实现标题栏的下划线和按钮高亮的功能,你可以使用 `javax.swing.border` 包中的 `MatteBorder` 类来创建下划线效果,并使用鼠标事件监听器来实现按钮的高亮效果。以下是修正后的代码: ```java import javax.swing.*; import javax.swing.border.LineBorder; import javax.swing.border.MatteBorder; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class CustomWindow extends JFrame { private JPanel titleBar; private JButton minimizeButton; private JButton maximizeButton; private JButton closeButton; public CustomWindow() { setSize(400, 300); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setUndecorated(true); // 去掉窗口边框默认样式 // 创建标题栏 createTitleBar(); getContentPane().setBackground(Color.WHITE); // 设置窗口背景色为白色 setVisible(true); } private void createTitleBar() { // 创建标题栏面板 titleBar = new JPanel(); titleBar.setBackground(Color.WHITE); titleBar.setPreferredSize(new Dimension(getWidth(), 30)); // 添加最小化按钮 minimizeButton = createTitleBarButton("-"); minimizeButton.addActionListener(e -> setState(JFrame.ICONIFIED)); // 添加最大化/还原按钮 maximizeButton = createTitleBarButton("□"); maximizeButton.addActionListener(e -> { if (getExtendedState() == JFrame.MAXIMIZED_BOTH) { setExtendedState(JFrame.NORMAL); } else { setExtendedState(JFrame.MAXIMIZED_BOTH); } }); // 添加关闭按钮 closeButton = createTitleBarButton("✕"); closeButton.addActionListener(e -> { dispose(); System.exit(0); }); // 添加按钮到标题栏 titleBar.setLayout(new FlowLayout(FlowLayout.RIGHT)); titleBar.add(minimizeButton); titleBar.add(maximizeButton); titleBar.add(closeButton); // 添加鼠标监听器以实现拖动窗口功能和按钮高亮效果 titleBar.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { initialClick = e.getPoint(); getComponentAt(initialClick); } public void mouseEntered(MouseEvent e) { setButtonHighlight(true); } public void mouseExited(MouseEvent e) { setButtonHighlight(false); } }); titleBar.addMouseMotionListener(new MouseAdapter() { public void mouseDragged(MouseEvent e) { int thisX = getLocation().x; int thisY = getLocation().y; int xMoved = e.getX() - initialClick.x; int yMoved = e.getY() - initialClick.y; int x = thisX + xMoved; int y = thisY + yMoved; setLocation(x, y); } }); // 将标题栏面板添加到窗口的顶部 getContentPane().add(titleBar, BorderLayout.NORTH); } private void setButtonHighlight(boolean isHighlighted) { Color highlightColor = new Color(230, 230, 230); // 高亮颜色 if (isHighlighted) { minimizeButton.setBackground(highlightColor); maximizeButton.setBackground(highlightColor); closeButton.setBackground(highlightColor); } else { minimizeButton.setBackground(Color.WHITE); maximizeButton.setBackground(Color.WHITE); closeButton.setBackground(Color.WHITE); } } private JButton createTitleBarButton(String text) { JButton button = new JButton(text); button.setFocusPainted(false); button.setBackground(Color.WHITE); button.setBorder(null); button.setPreferredSize(new Dimension(30, 30)); return button; } public static void main(String[] args) { SwingUtilities.invokeLater(() -> { CustomWindow window = new CustomWindow(); }); } } ``` 在这个修正后的代码中,我们添加了 `setButtonHighlight()` 方法来设置按钮的高亮效果。当鼠标进入标题栏时,我们调用该方法将按钮的背景色设置为高亮颜色;当鼠标离开标题栏时,将按钮的背景色恢复为白色。 另外,我们使用了 `MatteBorder` 类创建了标题栏的下划线效果。在 `createTitleBar()` 方法中,你可以添加以下代码来创建下划线: ```java // 创建下划线边框 MatteBorder underlineBorder = new MatteBorder(0, 0, 1, 0, Color.WHITE); titleBar.setBorder(underlineBorder); ``` 希望这个修正后的代码能满足你的需求!

相关推荐

最新推荐

recommend-type

js实现移动端tab切换时下划线滑动效果

主要为大家详细介绍了js实现移动端tab切换时下划线滑动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Vue实现textarea固定输入行数与添加下划线样式的思路详解

主要介绍了使用Vue实现textarea固定输入行数与添加下划线样式的思路详解,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

Android中EditText如何去除边框添加下划线

主要介绍了Android中EditText如何去除边框添加下划线的相关资料,需要的朋友可以参考下
recommend-type

YOLOv8中加入CBAM注意力机制

YOLOv8中加入CBAM注意力机制,适合目标检测方向新手小白对YOLOv8作出改进,开箱即用,上传不易,小伙伴拿走的同时请顺手一键三连哈
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

如何用python编写api接口

在Python中编写API接口可以使用多种框架,其中比较流行的有Flask和Django。这里以Flask框架为例,简单介绍如何编写API接口。 1. 安装Flask框架 使用pip命令安装Flask框架: ``` pip install flask ``` 2. 编写API接口 创建一个Python文件,例如app.py,编写以下代码: ```python from flask import Flask, jsonify app = Flask(__name__) @app.route('/api/hello', methods=['GET']) def hello():
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依