learning java swing 基本组件用法
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent; public class SwingComponent {
JFrame f = new JFrame("test");
Icon okIcon = new ImageIcon("ico/ok.png"); JButton ok = new JButton("确认", okIcon); JRadioButton male = new JRadioButton("男", true);
JRadioButton female = new JRadioButton("女", false); ButtonGroup bg = new ButtonGroup(); JCheckBox married = new JCheckBox("是否已婚", false); String[] colors = new String[]{"红色","绿色","蓝色"}; JComboBox<String> colorChooser = new JComboBox<>(colors); JList<String> colorList = new JList<>(colors); JTextArea ta = new JTextArea(, ); JTextField name = new JTextField(); JMenuBar mb = new JMenuBar(); JMenu file = new JMenu("文件"); JMenu edit = new JMenu("编辑"); Icon newIcon = new ImageIcon("ico/new.png");
JMenuItem newItem = new JMenuItem("新建", newIcon); Icon saveIcon = new ImageIcon("ico/save.png");
JMenuItem saveItem = new JMenuItem("保存", saveIcon); Icon exitIcon = new ImageIcon("ico/exit.png");
JMenuItem exitItem = new JMenuItem("退出",exitIcon); JCheckBoxMenuItem autoWrap = new JCheckBoxMenuItem("自动换行"); JMenuItem copyItem = new JMenuItem("复制", new ImageIcon("ico/copy.png")); JMenuItem pasteItem = new JMenuItem("粘贴", new ImageIcon("ico/paste.png")); JMenu format = new JMenu("格式"); JMenuItem commentItem = new JMenuItem("注释");
JMenuItem cancelItem = new JMenuItem("取消注释"); JPopupMenu pop = new JPopupMenu(); ButtonGroup flavorGroup = new ButtonGroup(); JRadioButtonMenuItem metalItem = new JRadioButtonMenuItem("Metal风格", true);
JRadioButtonMenuItem nimbusItem = new JRadioButtonMenuItem("Nimbus风格");
JRadioButtonMenuItem windowsItem = new JRadioButtonMenuItem("Windows风格");
JRadioButtonMenuItem classicItem = new JRadioButtonMenuItem("Windows经典风格");
JRadioButtonMenuItem motifItem = new JRadioButtonMenuItem("Motif风格"); public void init(){
var botton = new JPanel();
botton.add(name);
botton.add(ok);
f.add(botton, BorderLayout.SOUTH); var checkPannel = new JPanel();
checkPannel.add(colorChooser);
checkPannel.add(male);
checkPannel.add(female);
checkPannel.add(married);
bg.add(male);
bg.add(female); var topLeft = Box.createVerticalBox();
var taJsp = new JScrollPane(ta);
topLeft.add(taJsp);
topLeft.add(checkPannel); var top = Box.createHorizontalBox(); top.add(topLeft);
top.add(colorList);
f.add(top); newItem.setAccelerator(KeyStroke.getKeyStroke('N', InputEvent.CTRL_DOWN_MASK));
newItem.addActionListener(e -> ta.append("用户单击了 新建 菜单")); file.add(newItem);
file.add(saveItem);
file.add(exitItem); edit.add(autoWrap);
edit.addSeparator();
edit.add(copyItem);
edit.add(pasteItem); commentItem.setToolTipText("将程序代码注释起来"); format.add(commentItem);
format.add(cancelItem); edit.add(new JMenuItem("-"));
edit.add(format); mb.add(file);
mb.add(edit); f.setJMenuBar(mb); flavorGroup.add(metalItem);
flavorGroup.add(nimbusItem);
flavorGroup.add(windowsItem);
flavorGroup.add(classicItem);
flavorGroup.add(motifItem); pop.add(metalItem);
pop.add(nimbusItem);
pop.add(windowsItem);
pop.add(classicItem);
pop.add(motifItem); ActionListener flavorListener = e -> {
try {
switch (e.getActionCommand()){
case "Metal风格":
changeFlavor();
break;
case "Nimbus风格":
changeFlavor();
break;
case "Windows风格":
changeFlavor();
break;
case "Windows经典风格":
changeFlavor();
break;
case "Motif风格":
changeFlavor();
break; }
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}; metalItem.addActionListener(flavorListener);
nimbusItem.addActionListener(flavorListener);
windowsItem.addActionListener(flavorListener);
classicItem.addActionListener(flavorListener);
motifItem.addActionListener(flavorListener);
ta.setComponentPopupMenu(pop);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack();
f.setVisible(true);
} private void changeFlavor(int flavor) throws ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException { switch (flavor){
case :
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
break;
case :
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
break;
case :
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
break;
case :
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
break;
case :
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
break;
} SwingUtilities.updateComponentTreeUI(f.getContentPane());
SwingUtilities.updateComponentTreeUI(mb);
SwingUtilities.updateComponentTreeUI(pop);
} public static void main(String[] args) {
new SwingComponent().init();
} }
output:

learning java swing 基本组件用法的更多相关文章
- leaning java swing 为组件设置边框
import javax.swing.*; import javax.swing.border.BevelBorder; import javax.swing.border.Border; impor ...
- learning java swing 双缓冲和键盘驱动
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.In ...
- learning java AWT 常见组件
import javax.swing.*; import java.awt.*; public class CommonComponent { Frame f = new Frame("te ...
- learning java 处理流的用法
有点重定向使用的味道 import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.Pri ...
- Java Swing Graphics Graphics2D的一般用法
Java Swing Graphics Graphics2D的一般用法: 贝塞尔曲线参考:http://www.zhangxinxu.com/wordpress/2014/06/deep-unders ...
- Java核心技术卷一基础知识-第9章-Swing用户界面组件-读书笔记
第9章 Swing用户界面组件 本章内容: * Swing与模型-视图-控制器设计模式 * 布局管理概述 * 文本输入 * 选择组件 * 菜单 * 复杂的布局管理 * 对话框 本章将介绍构造功能更加齐 ...
- java Swing组件和事件处理(二)
1.BoxLayout类可以创建一个布局对象,成为盒式布局,BoxLayout在javax.Swing border 包中,java.swing 包提供一个Box类,该类也是一个类,创建的容器称作一 ...
- Java中日志组件详解
avalon-logkit Java中日志组件详解 lanhy 发布于 2020-9-1 11:35 224浏览 0收藏 作为开发人员,我相信您对日志记录工具并不陌生. Java还具有功能强大且功能强 ...
- Java Swing 第03记 布局管理器
几种Swing常用的布局管理器 BorderLaout 它将容器分为5个部分,即东.南.西.北.中,每一个区域可以容纳一个组件,使用的时候也是通过BorderLayout中5个方位常量来确定组件所在的 ...
随机推荐
- node-red File读取好保存
File节点是操作文件的节点 file文件的保存 拖拽 注入节点inject file节点(writes msg.payload to a file)和 debug节点到工作区,并连线 设置file ...
- jquery easyui datagrid 在翻页以后仍能记录被选中的行及刷新设置选中行数据
//easyUI的datagrid在复选框多选时,如何在翻页以后仍能记录被选中的行://注意datagrid中需要配置idField属性,一般为数据的主键 $.ajax({ type: 'GET', ...
- Oracle 11g 服务器结构
Oracle 服务器主要又实例.数据库.程序全局区和前台进程组成. 实例可以进一步划分为系统全局区(SGA)和后台进程(PMON.SMON等)两部分,其中,SGA 使用操作系统的内存资源,而后台进程需 ...
- 增强学习--TRPO算法
理论部分参考 推导 数学上的分析 代码
- el-pagination分页优化
表格分页优化: <template> <el-pagination small background @size-change="handleSizeChange" ...
- Django异常 - ImportError: No module named django.core.management
Django错误 - ImportError: No module named django.core.management问题描述:在命令行输入 manage.py runserver,提示找不到d ...
- Java8 stream用法-备忘录
1. 如何使用匹配模式 List<String> strs = Arrays.asList("a", "a", "a", &qu ...
- 虚拟机NAS存储的安装
1.下载一款免费的NAS系统fressNAS 下载网址;https://www.freenas.org/download/ 2.选择配置好虚拟机(内存设置8G最好,选择桥接) 不要做任何操作等待进入安 ...
- [ipsec][strongswan] strongswan源码分析-- (三) xfrm与strongswan内核接口分析
目录 strongwan sa分析(三) xfrm与strongswan内核接口分析 1. strongswan的实现 2. 交互机制 4. xfrm的消息通信的实现 strongwan sa分析(三 ...
- 使用Cloudera Manager搭建MapReduce集群及MapReduce HA
使用Cloudera Manager搭建MapReduce集群及MapReduce HA 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.通过CM部署MapReduce On ...