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个方位常量来确定组件所在的 ...
随机推荐
- Microsoft的考验――查找第二大的数
#include<stdio.h> int main() { int n,m,t,max,max1; scanf("%d",&n); while(n--) { ...
- java之struts2之ServletAPI
在之前的学习中struts2已经可以处理大部分问题了.但是如果要将用户登录数据存入session中,可以有两种方式开存入ServletAPI. 一种解耦合方式,一种耦合方式. 1. 解耦合方式 解耦合 ...
- webapi session
webapi中使用session 修改global.cs里面的内容 using System; using System.Web; using System.Web.Routing; using Sy ...
- Java File类与IO流
File 类 java.io.File 文件和目录路径名的抽象表示形式, 对文件或目录进行操作 构造方法: File(File parent, String child) : 根据 parent 抽象 ...
- Sqlite常用命令及基本知识
sqlite菜鸟教程:http://www.runoob.com/sqlite/sqlite-tutorial.html 常用命令: .sqlite3 --从dos命令模式进入sqlite命令 ...
- python实现进制转换(二、八、十六进制;十进制)
python实现进制转换(二.八.十六进制:十进制) (一)十进制整数转为二.八.十六进制 1.format实现转换>>> format(2,"b") # (10 ...
- 挂载nfs提示:mount.nfs: access denied by server while mounting...
出现此类错误原因大致为: 权限问题 防火墙机制问题 共享配置文件问题 搭建好nfs服务后,在client端进行挂载时,提示: [root@web1 media]# mount -t nfs 192.1 ...
- 【大数据技术能力提升_4】logistic学习
logistic学习 标签(空格分隔): logistic sigmod函数 逻辑回归 分类 前言: 整体逻辑回归比线性回归难理解点,其还需要<概率论与数理统计>中"二项分布 ...
- php 执行大量sql语句 MySQL server has gone away
php 设置超时时间单位秒 set_time_limit(3600); php 设置内存限制ini_set('memory_limit', '1024M'); mysql服务端接收到的包的大小 ...
- Linux在丢失的情况下重置密码
1.开机菜单是 移动光标到第一行 --敲击e 2.找到UTF-8,加上空格rd.break,敲击ctrl+x 3.输入以下命令 mount -o remount,rw /sysroot chroot ...