当前位置:网站首页>Swing组件之单选与多选按钮
Swing组件之单选与多选按钮
2022-07-25 09:27:00 【看小虫子】
单选按钮
复习图片图标加到按钮上
package Swing;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
public static void main(String[] args) {
new JButtonDemo01();
}
public JButtonDemo01() {
Container container=getContentPane();
//将图片变成图标
URL url =JButtonDemo01.class.getResource("fj.jpg");
Icon icon=new ImageIcon(url);
JButton jButton=new JButton();
//将图标添加到按钮上
jButton.setIcon(icon);
jButton.setToolTipText("图片按钮");
container.setLayout(new GridLayout(2,1));
container.add(jButton);
//单选框
JRadioButton jRadioButton01 = new JRadioButton("JRadioButton01");
JRadioButton jRadioButton02 = new JRadioButton("JRadioButton01");
JRadioButton jRadioButton03 = new JRadioButton("JRadioButton01");
//由于单选框只能选一个,分组(一个组中只能选一个)
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton01);
buttonGroup.add(jRadioButton02);
buttonGroup.add(jRadioButton03);
container.add(jRadioButton01,BorderLayout.CENTER);
container.add(jRadioButton02,BorderLayout.NORTH);
container.add(jRadioButton03,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,200,300,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
多选框
package Swing;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDeno02 extends JFrame{
public static void main(String[] args) {
new JButtonDeno02();
}
public JButtonDeno02() {
Container container=getContentPane();
//将图片变成图标
URL url =JButtonDemo01.class.getResource("fj.jpg");
Icon icon=new ImageIcon(url);
//多选框
JCheckBox CheckBox01 = new JCheckBox("CheckBox01");
JCheckBox CheckBox02 = new JCheckBox("CheckBox02");
JCheckBox CheckBox03 = new JCheckBox("CheckBox03");
Container container1=getContentPane();
container1.add(CheckBox01,BorderLayout.SOUTH);
container1.add(CheckBox02,BorderLayout.NORTH);
container1.add(CheckBox03,BorderLayout.CENTER);
this.setVisible(true);
this.setBounds(100,200,300,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
下拉选框
package Swing;
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo01 extends JFrame{
public static void main(String[] args) {
new TestComboboxDemo01();
}
public TestComboboxDemo01() {
Container container=getContentPane();
JComboBox status=new JComboBox();
status.addItem(null);
status.addItem("正在上映");
status.addItem("已经下架");
status.addItem("不好看");
container.add(status);
//这两个要写在点击事件
//返回项数
System.out.println(status.getSelectedIndex());
//返回内容
System.out.println(status.getSelectedItem());
this.setVisible(true);
this.setBounds(100,200,300,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
列表框
package Swing;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboboxDemo01 extends JFrame{
public static void main(String[] args) {
new TestComboboxDemo01();
}
public TestComboboxDemo01() {
Container container=getContentPane();
//生成列表的内容 稀疏数组
// String[]contents={"1","2","3"};
Vector contents=new Vector();
JList jlist = new JList(contents);
container.add(jlist);
contents.add("1");
contents.add("3");
contents.add("1");
this.setVisible(true);
this.setBounds(100,200,300,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
文本框,文本域,密码框
package Swing;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestTextDemo01 extends JFrame{
public static void main(String[] args) {
new TestTextDemo01();
}
public TestTextDemo01() {
Container container=getContentPane();
JTextField textField=new JTextField("hello");
JTextArea jTextArea=new JTextArea(20,30);
JPasswordField jPasswordField=new JPasswordField();
jPasswordField.setEchoChar('*');
container.add(jPasswordField,BorderLayout.SOUTH);
container.add(textField,BorderLayout.CENTER);
container.add(jTextArea,BorderLayout.NORTH);
this.setVisible(true);
this.setBounds(100,200,300,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
边栏推荐
- Detailed explanation of JDBC operation database
- nodejs链接mysql报错:ER_NOT_SUPPORTED_AUTH_MODEError: ER_NOT_SUPPORTED_AUTH_MODE
- UE4 框架介绍
- Use of dictionary tree
- TensorFlow raw_ RNN - implement the seq2seq mode to take the output of the previous time as the input of the next time
- Copy the old project into a web project
- 概率论与数理统计 4 Continuous Random Variables and Probability Distributions(连续随机变量与概率分布)(上篇)
- Detailed explanation of MySQL database
- [necessary for growth] Why do I recommend you to write a blog? May you be what you want to be in years to come.
- Introduction to Verdi Foundation
猜你喜欢

Advanced introduction to digital IC Design SOC

CentOs安装redis
![腾讯云之错误[100007] this env is not enable anonymous login](/img/a2/a209a0d94e3fbf607242c28d87e2dd.png)
腾讯云之错误[100007] this env is not enable anonymous login

概率论与数理统计 4 Continuous Random Variables and Probability Distributions(连续随机变量与概率分布)(上篇)

Probability theory and mathematical statistics 4 continuous random variables and probability distributions (Part 1)

Subtotal of rospy odometry sinkhole
![[machine translation] scones -- machine translation with multi tag tasks](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[machine translation] scones -- machine translation with multi tag tasks

js加密参数定位

UE4 LoadingScreen动态加载启动动画

Debug篇快捷键入门
随机推荐
DHCP的配置(以华为eNSP为例)
安装 oh my zsh
多数相合问题总结
Dataset 和 Dataloader数据加载
基础背包问题
See how a junior student of double non-2 (0 Internship) can get an offer from Alibaba and Tencent
VCs common commands
CCF 201509-2 date calculation
Introduction to armv8 architecture
关闭brew执行命令时的自动更新
Eco introduction
第五阶段第一周
UE4 碰撞(Collsion)
message from server: “Host ‘xxx.xxx.xxx.xxx‘ is not allowed to connect to this MySQL server“
史上最全面的UE4 文件操作,打开,读、写,增、删、改、查
线程池的设计和原理
字符串切片的用法
Redis和MongoDB的区别
rospy Odometry天坑小计
Introduction to arm GIC