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

Java快速上手之实验五

1. 编写JButtonDemo.java。

(1)使用水平布局放置2个按钮,其中按钮2为图标按钮(使用ImageIcon)。

(2)当按下按钮时弹出相应的对话框。

2. 编写JRadioButtonDemo.java。根据所选按钮改变背景色。

3. 编写JCheckBoxDemo.java。根据所选按钮改变标签的效果。

4. 编写JComboBoxDemo.java。在标签中显示所选列表项内容。

5.编写ColorDialogDemo.java,点击按钮弹出颜色对话框,并用选中的颜色作为窗口的背景色。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class JButtonDemo {public static void main(String[] args) {// 创建主窗口JFrame frame = new JFrame("测试按钮类");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(300, 150);frame.setLayout(new FlowLayout()); // 水平布局// 创建按钮1JButton button1 = new JButton("按钮1");button1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 弹出对话框JOptionPane.showMessageDialog(frame, "你按了:按钮1", "消息", JOptionPane.INFORMATION_MESSAGE);}});// 创建按钮2(图标按钮)ImageIcon icon = new ImageIcon("java-icon.png"); JButton button2 = new JButton("按钮2", icon);button2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 弹出对话框JOptionPane.showMessageDialog(frame, "你按了:按钮2", "消息", JOptionPane.INFORMATION_MESSAGE);}});// 将按钮添加到窗口frame.add(button1);frame.add(button2);// 设置窗口可见frame.setVisible(true);}
}2.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class JRadioButtonDemo {public static void main(String[] args) {// 创建主窗口JFrame frame = new JFrame("单选框");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(300, 200);frame.setLayout(new BorderLayout());// 创建面板,用于显示背景颜色JPanel colorPanel = new JPanel();colorPanel.setBackground(Color.RED); // 默认背景颜色为红色frame.add(colorPanel, BorderLayout.CENTER);// 创建单选按钮JRadioButton redButton = new JRadioButton("红色", true);  // 默认选中JRadioButton greenButton = new JRadioButton("绿色");JRadioButton blueButton = new JRadioButton("蓝色");// 将单选按钮添加到按钮组,确保只能单选ButtonGroup group = new ButtonGroup();group.add(redButton);group.add(greenButton);group.add(blueButton);// 创建面板用于放置单选按钮JPanel buttonPanel = new JPanel();buttonPanel.setLayout(new FlowLayout());buttonPanel.add(redButton);buttonPanel.add(greenButton);buttonPanel.add(blueButton);frame.add(buttonPanel, BorderLayout.SOUTH);// 添加事件监听器,实现背景颜色更改redButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {colorPanel.setBackground(Color.RED);}});greenButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {colorPanel.setBackground(Color.GREEN);}});blueButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {colorPanel.setBackground(Color.BLUE);}});// 设置窗口可见frame.setVisible(true);}
}3.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;public class JCheckBoxDemo {public static void main(String[] args) {// 创建主窗口JFrame frame = new JFrame("复选框");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(400, 200);frame.setLayout(new BorderLayout());// 创建标签JLabel label = new JLabel("请注意观察字体的变化", JLabel.CENTER);label.setFont(new Font("宋体", Font.PLAIN, 20)); // 初始字体label.setOpaque(true); // 设置背景颜色可见label.setBackground(Color.YELLOW); // 设置背景颜色frame.add(label, BorderLayout.CENTER);// 创建复选框JCheckBox boldCheckBox = new JCheckBox("粗体");JCheckBox italicCheckBox = new JCheckBox("斜体", true); // 默认选中// 创建面板用于放置复选框JPanel checkBoxPanel = new JPanel();checkBoxPanel.add(boldCheckBox);checkBoxPanel.add(italicCheckBox);frame.add(checkBoxPanel, BorderLayout.SOUTH);// 添加复选框的监听器ItemListener listener = new ItemListener() {@Overridepublic void itemStateChanged(ItemEvent e) {// 初始化字体样式int style = Font.PLAIN;if (boldCheckBox.isSelected()) {style += Font.BOLD;}if (italicCheckBox.isSelected()) {style += Font.ITALIC;}// 更新标签字体样式label.setFont(new Font("宋体", style, 20));}};// 为复选框添加监听器boldCheckBox.addItemListener(listener);italicCheckBox.addItemListener(listener);// 设置窗口可见frame.setVisible(true);}
}4.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class JComboBoxDemo {public static void main(String[] args) {// 创建主窗口JFrame frame = new JFrame("下拉列表");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(300, 150);frame.setLayout(new FlowLayout()); // 使用流式布局// 创建标签,用于显示选择结果JLabel label = new JLabel("你选择了:上海");label.setOpaque(true);label.setBackground(Color.YELLOW); // 设置背景颜色frame.add(label);// 创建下拉列表String[] cities = {"上海", "北京", "广州", "深圳"};JComboBox<String> comboBox = new JComboBox<>(cities);frame.add(comboBox);// 为下拉列表添加事件监听器comboBox.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 获取用户选择的项String selectedCity = (String) comboBox.getSelectedItem();// 更新标签内容label.setText("你选择了:" + selectedCity);}});// 设置窗口可见frame.setVisible(true);}
}5.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class ColorDialogDemo {public static void main(String[] args) {// 创建主窗口JFrame frame = new JFrame("带颜色对话框的窗口");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(400, 300);frame.setLayout(new BorderLayout());// 设置初始背景颜色frame.getContentPane().setBackground(Color.BLUE);// 创建按钮JButton button = new JButton("打开颜色对话框");button.setFont(new Font("宋体", Font.PLAIN, 16));frame.add(button, BorderLayout.CENTER);// 添加按钮的事件监听button.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 弹出颜色选择对话框Color selectedColor = JColorChooser.showDialog(frame, "选择颜色", frame.getContentPane().getBackground());if (selectedColor != null) {// 设置选中颜色为背景色frame.getContentPane().setBackground(selectedColor);}}});// 设置窗口可见frame.setVisible(true);}
}

http://www.xdnf.cn/news/2549.html

相关文章:

  • 若依脱敏功能升级:接口返回想脱就脱,想不脱就不脱(实现灵活可控制的数据脱敏)
  • 手撕——贪吃蛇小游戏(下)
  • 【quantity】1 创建 crates.io 账号并上传 Rust 库
  • 数据库查询艺术:从单表操作到多表联查的全面指南
  • Rollup、Webpack、Esbuild 和 Vite 前端打包工具
  • Redis01-基础-入门
  • 华为仓颉编程语言的实际用法与使用领域详解
  • OpenCV实验室工具的使用
  • 【银河麒麟高级服务器操作系统】在VMware虚拟机情况下出现软锁处理过程
  • C/C++死锁和活锁
  • k8s学习记录(五):Pod亲和性详解
  • 解决两个技术问题后小有感触-QZ Tray使用经验小总结
  • 分布式GPU上计算长向量模的方法
  • 数据一致性问题剖析与实践(四)——竞态条件竞争导致的一致性问题
  • 制作一款打飞机游戏26:精灵编辑器
  • streamlit实现非原生的按钮触发效果 + flask实现带信息的按钮触发
  • Pikachu靶场-PHP反序列化漏洞
  • 2024ICPC网络赛第二场题解
  • DeepSeek:重构人类文明的智能引擎
  • JVM——运行时数据区
  • NLP预处理:如何 处理表情符号
  • 基于物理信息的神经网络在异常检测Anomaly Detection中的应用:实践指南
  • 解决Cline的Shell Integration Unavailable问题
  • 软考:软件设计师考试数据结构知识点详解
  • 引领印尼 Web3 变革:Mandala Chain 如何助力 1 亿用户迈向数字未来?
  • .class文件是字节码吗还是二进制文件
  • 【首款Armv9开源芯片“星睿“O6测评】SVE2指令集介绍与测试
  • Android调试那些事儿
  • uniapp-商城-42-shop 后台管理 分包
  • 多视觉编码器协同与高低分辨率特征融合技术综述