使Java的JButton文字隐藏功能的实现(不隐藏按钮的前提)
作者:晓星晨曦 时间:2022-06-01 23:54:33
在网上虽然看到了方法,但是处理感觉很复杂,我的办法,老实说,是突然试一下试到的,哈哈QWQ
OK,开始说明如何整的。
效果如上图所示
代码如下
package csdn.article;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JButtonTextSize extends JFrame {
public static void main(String[] args) {
new JButtonTextSize();
}
JButtonTextSize(){
ImageIcon i = new ImageIcon("./images/1.jpg");
i.setImage(i.getImage().getScaledInstance(50,50,0));//控制图片大小
JButton jb = new JButton("测试文字");
jb.setSize(200,100);
jb.setLocation(0,0);//因JFrame的Layout设置了null,所以需要设置setLocation
jb.setFont(new Font("宋体",1,20));
jb.setIcon(i);
JButton change = new JButton("隐藏上面按钮文字");
change.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jb.setFont(new Font("宋体",1,0));
repaint();
}
});
change.setSize(200,100);
change.setLocation(0,100);
add(jb);
add(change);
setLayout(null);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setLocation(500,500);
}
}
为了方便理解,所以我就加入了图片。其实就是给JButton设置Font,将setFont()方法的第三参数改为0(第三个参数设置的是字体大小),就可以了,偶然发现,哈哈。就是这么简单的啦!
来源:https://blog.csdn.net/u013070710/article/details/111829718
标签:Java,JButton,文字隐藏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringCloud Zuul实现负载均衡和熔断机制方式
2023-08-23 11:56:43
![](https://img.aspxhome.com/file/2023/0/72530_0s.png)
java 字段值为null,不返回该字段的问题
2023-07-13 10:32:34
如何利用Spring MVC实现RESTful风格
2021-06-06 02:02:13
![](https://img.aspxhome.com/file/2023/6/80936_0s.png)
从零实现一个简单的Spring Bean容器的代码案例
2022-07-24 11:42:16
C#定时关闭窗体实例
2023-07-09 22:53:25
Java关于MyBatis缓存详解
2021-11-01 00:40:20
java IO流文件的读写具体实例
2023-08-21 04:44:34
![](https://img.aspxhome.com/file/2023/2/58072_0s.jpg)
java面向对象设计原则之合成复用原则示例详解
2023-11-09 16:55:51
![](https://img.aspxhome.com/file/2023/3/58893_0s.png)
java制作简单验证码功能
2022-12-07 02:12:46
![](https://img.aspxhome.com/file/2023/5/60835_0s.jpg)
mybatis使用foreach查询不出结果也不报错的问题
2023-11-24 22:36:17
![](https://img.aspxhome.com/file/2023/6/60116_0s.png)
Java中定时器Timer致命缺点案例详解
2022-08-27 08:38:45
![](https://img.aspxhome.com/file/2023/0/66200_0s.png)
Java中将File转化为MultipartFile的操作
2021-07-05 21:25:32
Java应用服务器之tomcat会话复制集群配置的示例详解
2022-08-24 07:28:27
![](https://img.aspxhome.com/file/2023/8/83088_0s.png)
基于Java中字符串indexof() 的使用方法
2022-12-09 19:37:38
![](https://img.aspxhome.com/file/2023/7/65587_0s.jpg)
Java.try catch finally 的执行顺序说明
2022-07-06 00:30:25
![](https://img.aspxhome.com/file/2023/2/63312_0s.jpg)
Java爬虫实现Jsoup利用dom方法遍历Document对象
2023-06-15 07:52:36
Spring依赖注入的三种方式小结
2022-08-09 15:56:41
![](https://img.aspxhome.com/file/2023/1/62641_0s.png)
Spring主配置文件(applicationContext.xml) 导入约束详解
2021-09-29 06:21:01
![](https://img.aspxhome.com/file/2023/2/69922_0s.png)
C#语言中字符类char的使用方法(总结)
2022-09-04 17:25:43
Java操作FTP实现上传下载功能
2021-12-07 18:35:04
![](https://img.aspxhome.com/file/2023/1/82311_0s.png)