当前位置: 首页 > news >正文

【GUI】-- 08 JButton、JRadioButton、JCheckBox

GUI编程

03 Swing

3.5 JButton

  1. 图片置于按钮之上的JButton:
package com.duo.lesson05;import javax.swing.*;
import java.awt.*;
import java.net.URL;public class JButtonDemo01 extends JFrame {public JButtonDemo01() {Container contentPane = getContentPane();URL url = JButtonDemo01.class.getResource("通用头像.jpg");assert url != null;ImageIcon imageIcon = new ImageIcon(url);JButton jButton = new JButton();jButton.setIcon(imageIcon);jButton.setToolTipText("Fire");contentPane.add(jButton);setTitle("JButton测试窗口");setVisible(true);setSize(650, 500);setLocationRelativeTo(null);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JButtonDemo01();}
}

运行结果:

图1

如上所示,当鼠标悬浮于按钮之上时,会有tip显示。

  1. 单选按钮 & 多选按钮
  • 单选按钮JRadioButton:
package com.duo.lesson05;import javax.swing.*;
import java.awt.*;
import java.net.URL;public class JButtonDemo02 extends JFrame {public JButtonDemo02() {Container contentPane = getContentPane();//单选按钮框,只能选择一个。通常要对按钮进行分组JRadioButton jRadioButton1 = new JRadioButton("按钮1");JRadioButton jRadioButton2 = new JRadioButton("按钮2");JRadioButton jRadioButton3 = new JRadioButton("按钮3");ButtonGroup buttonGroup = new ButtonGroup();buttonGroup.add(jRadioButton1);buttonGroup.add(jRadioButton2);buttonGroup.add(jRadioButton3);contentPane.add(jRadioButton1, BorderLayout.NORTH);contentPane.add(jRadioButton2, BorderLayout.CENTER);contentPane.add(jRadioButton3, BorderLayout.SOUTH);setTitle("单选按钮测试窗口");setVisible(true);setSize(650, 500);setLocationRelativeTo(null);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JButtonDemo02();}
}

运行结果:

图2

  • 多选按钮JCheckBox:
package com.duo.lesson05;import javax.swing.*;
import java.awt.*;public class JButtonDemo03 extends JFrame {public JButtonDemo03() {Container contentPane = getContentPane();//多选按钮框JCheckBox jCheckBox1 = new JCheckBox("A");JCheckBox jCheckBox2 = new JCheckBox("B");contentPane.add(jCheckBox1, BorderLayout.NORTH);contentPane.add(jCheckBox2, BorderLayout.SOUTH);setTitle("多选按钮测试窗口");setVisible(true);setSize(650, 500);setLocationRelativeTo(null);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new JButtonDemo03();}
}

运行结果:

图3


http://www.lryc.cn/news/233402.html

相关文章:

  • 【postgresql】CentOS7 安装Pgweb
  • 基于python和定向爬虫的商品比价系统
  • 使用GPT-4训练数据微调GPT-3.5 RAG管道
  • 二十三种设计模式全面解析-深入解析模板方法模式的奇妙世界
  • 【Spring】加载properties文件
  • react中间件的理解
  • React函数组件状态Hook—useState《进阶-对象数组》
  • linux 网络 cat /proc/net/dev 查看测试网络丢包情况
  • 记录配置VS,使用opencv与Eigen
  • uart控制led与beep
  • Linux修改root密码
  • C/C++模板类模板与函数模板区别,以及用法详解
  • van-dialog弹窗异步关闭-校验表单
  • Dynamic Wallpaper 16.7中文版
  • ​如何使用ArcGIS Pro制作渐变河流效果
  • 《网络协议》06. HTTP 补充 · HTTPS · SSL/TLS
  • Python winreg将cmd/PowerShell(管理员)添加到右键菜单
  • redis运维(九)字符串(二)字符串过期时间
  • 【C++】多线程的学习笔记(3)——白话文版(bushi
  • kotlin--3.集合操作
  • 自动驾驶-BEV感知综述
  • 面试题-3
  • C++ Core Guidelines 中文版 GSL
  • 同时显示上下两层凸包特征的可视化程序
  • Django框架之模型层(二)
  • C++之list
  • C语言日记——调试篇
  • 【python】Django——templates模板、静态文件、django模板语法、请求和响应
  • Android设计模式--观察者模式
  • 【Linux】Ubuntu16.04下安装python高版本--源码安装