package ming;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent; import javax.swing.*; public class TestFrame { JFrame f = new JFrame("testing"); // 定义一个按钮,并为它设置图标
Icon okIcon = new ImageIcon("/Icon/anydo.png"); JButton bnt_ok = new JButton("yes", okIcon); // 定义一个单选按钮
JRadioButton male = new JRadioButton("male"); JRadioButton female = new JRadioButton("female"); // 将单选按钮组合一起
ButtonGroup bg = new ButtonGroup(); // 定义复选按钮框
JCheckBox married = new JCheckBox("have been married?", false); String[] colors = new String[] { "Red", "Green", "Blue" }; // 定义一个下拉选择框
JComboBox<String> colorChooser = new JComboBox<String>(colors); // 定义一个列表选择框
JList<String> colorList = new JList<String>(colors); // 定义一个8行 20列 多行文本域
JTextArea ta = new JTextArea(8, 20); // 定义一个40列的文本域
JTextField name = new JTextField(40); JMenuBar mb = new JMenuBar();
JMenu file = new JMenu("file");
JMenu edit = new JMenu("edit"); // 创建“新建” 菜单项
Icon newIcon = new ImageIcon("/Icon/soundhound.png");
JMenuItem newItem = new JMenuItem("NEW", newIcon); // 创建保存 菜单项
Icon saveIcon = new ImageIcon("/Icon/messenger.png");
JMenuItem saveItem = new JMenuItem("SAVE", saveIcon); // 创建退出菜单项
Icon exitIcon = new ImageIcon("/Icon/contact.png");
JMenuItem exitItem = new JMenuItem("EXIT", exitIcon); // 创建自动换行
JCheckBoxMenuItem autoWrap = new JCheckBoxMenuItem("auto wrap"); // 创建复制 菜单项
JMenuItem copyItem = new JMenuItem("COPY", new ImageIcon(
"/Icon/playstore.png"));
// 创建黏贴 菜单项
JMenuItem pasteItem = new JMenuItem("COPY", new ImageIcon(
"/Icon/playstore.png")); JMenu format = new JMenu("format"); JMenuItem commentItem = new JMenuItem("COMMENT", new ImageIcon(
"/Icon/playstore.png"));
JMenuItem cancelItem = new JMenuItem("CANCEL COMMENT", new ImageIcon(
"/Icon/playstore.png")); // 定义一个右键菜单,设置程序风格
JPopupMenu pop = new JPopupMenu();
ButtonGroup flavorGroup = new ButtonGroup();
// 5个单选框用于设置风格
JRadioButtonMenuItem metailItem = new JRadioButtonMenuItem("metail", true);
JRadioButtonMenuItem nimbusItem = new JRadioButtonMenuItem("nimbus");
JRadioButtonMenuItem windowsItem = new JRadioButtonMenuItem("windows");
JRadioButtonMenuItem classicItem = new JRadioButtonMenuItem("classic");
JRadioButtonMenuItem motifItem = new JRadioButtonMenuItem("motif"); public void init() {
// 创建装载文本框的Panel
JPanel buttom = new JPanel();
buttom.add(name);
buttom.add(bnt_ok);
f.add(buttom, BorderLayout.NORTH);
// 装载下拉选择框,3个JCheckBox的JPanel
JPanel checkPanel = new JPanel();
checkPanel.add(colorChooser);
bg.add(male);
bg.add(female);
checkPanel.add(male);
checkPanel.add(female);
checkPanel.add(married); // 创建一个垂直排列组建的BOX 装JPanel
Box topLeft = Box.createVerticalBox();
// 使用JScrollPan 作为普通组建的JViewPort
JScrollPane taJsp = new JScrollPane(ta);
topLeft.add(taJsp);
topLeft.add(checkPanel); //创建水平排了组件的BOX,装topLeft, colorList
Box top = Box.createHorizontalBox();
top.add(topLeft);
top.add(colorList);
//top加载到窗口中间
f.add(top); //组合菜单,并添加监听器
//newItem设置快捷键事要用大写字母
newItem.setAccelerator(KeyStroke.getKeyStroke('N',InputEvent.CTRL_MASK));
newItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
ta.append("user clicked new item\n");
} });
//为file菜单添加菜单项
file.add(newItem);
file.add(saveItem);
file.add(exitItem);
//为edit菜单添加菜单项
edit.add(autoWrap);
//添加分割线
edit.addSeparator();
edit.add(copyItem);
edit.add(pasteItem); //为commentIteam添加提示信息
commentItem.setToolTipText("comment the code");
//为format添加菜单项目
format.add(commentItem);
format.add(cancelItem); //使用 new JMenuItem("-") 不能添加菜单分隔符
edit.add(new JMenuItem("-"));
//format添加到edit,形成二级菜单
edit.add(format);
//add file,edit item to Menu
mb.add(file);
mb.add(edit);
//为f设置菜单条
f.setJMenuBar(mb);
//右键组合菜单,并安装右键菜单
flavorGroup.add(metailItem);
flavorGroup.add(nimbusItem);
flavorGroup.add(windowsItem);
flavorGroup.add(classicItem);
flavorGroup.add(motifItem);
pop.add(metailItem);
pop.add(nimbusItem);
pop.add(windowsItem);
pop.add(classicItem);
pop.add(motifItem);
ActionListener flavorListener = new ActionListener(){ @Override
public void actionPerformed(ActionEvent e) {
try{
switch(e.getActionCommand()){
case "metail":
changeFlavor(1);
break;
case "nimbusItem":
changeFlavor(2);
break;
case "windows":
changeFlavor(3);
break;
case "classic":
changeFlavor(4);
break;
case "motifItem":
changeFlavor(5);
break; }
}catch(Exception ee){
ee.printStackTrace();
}
} private void changeFlavor(int i) { } }; //设置5个风格的监控事件
metailItem.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); } public static void main(String[] args) {
// TODO Auto-generated method stub
//默认风格
JFrame.setDefaultLookAndFeelDecorated(true); new TestFrame().init();
} }

Java_swing控件实例的更多相关文章

  1. 计数器控件实例 NumericStepper

    计数器控件实例 书:158 <?xml version="1.0" encoding="utf-8"?> <s:Application xml ...

  2. 树结构控件实例 TreeControl

    树结构控件实例 书:157 <?xml version="1.0" encoding="utf-8"?> <s:Application xml ...

  3. 下拉列表控件实例 ComboBoxControl

    下拉列表控件实例 书:151页 <?xml version="1.0" encoding="utf-8"?> <s:Application x ...

  4. wpf datagrid 如何自定义行的控件实例,(textbox 并选中则全选)

    主要是为了用户输入方便 按回车,选中下一列,text自动获取焦点,输入状态 获取控件实例  https://blog.csdn.net/m15188153014/article/details/486 ...

  5. .NET组件控件实例编程系列——5.DataGridView数值列和日期列

    在使用DataGridView编辑数据的时候,编辑的单元格一般会显示为文本框,逻辑值和图片会自动显示对应类型的列.当然我们自己可以手工选择列的类型,例如ComboBox列.Button列.Link列. ...

  6. Appium依据xpath获取控件实例随笔

    如文章<Appium基于安卓的各种FindElement的控件定位方法实践>所述,Appium拥有众多获取控件的方法.当中一种就是依据控件所在页面的XPATH来定位控件. 本文就是尝试通过 ...

  7. 【转】Appium根据xpath获取控件实例随笔

    原文地址:http://blog.csdn.net/zhubaitian/article/details/39754233 如文章<Appium基于安卓的各种FindElement的控件定位方法 ...

  8. Appium根据xpath获取控件实例随笔

    如文章<Appium基于安卓的各种FindElement的控件定位方法实践>所述,Appium拥有众多获取控件的方法.其中一种就是根据控件所在页面的XPATH来定位控件. 本文就是尝试通过 ...

  9. webform FileUpload控件实例应用 上传图片

    首先在根目录下建一个"images"文件: HTML: <form id="form1" runat="server"> < ...

随机推荐

  1. 嵌入式Linux启动过程中的问题积累

    嵌入式Linux启动过程中的问题积累 Dongas 07-12-19 1.Bad Magic Number ## Booting image at 33000000 ... Bad Magic Num ...

  2. J2EE的若干问题

    1.问题:jsp中out.println页面显示不出换行效果.例如: out.println("唱歌"); out.println("跳舞"); 以上代码的结果 ...

  3. Travel(HDU 4284状压dp)

    题意:给n个城市m条路的网图,pp在城市1有一定的钱,想游览这n个城市(包括1),到达一个城市要一定的花费,可以在城市工作赚钱,但前提有工作证(得到有一定的花费),没工作证不能在该城市工作,但可以走, ...

  4. PHP 实现短域名互转

    /** * 短域名生成&解析类 */ class Build_URL { private $mem; private $base_url = 'http://xxx.com/'; public ...

  5. 寒假训练第九场 Brocard Point of a Triangle

    题意:求布洛卡点坐标 思路:直接利用布洛卡点的性质.http://pan.baidu.com/s/1eQiP76E #include<cstdio> #include<cstring ...

  6. uvalive 3218 Find the Border

    题意:一条封闭折线将平面分成了若干个区域,按顺序给出折线各点的坐标,要求输出封闭折线的轮廓. 题解:用类似卷包裹的算法,先确定一个一定会被选中的点(x坐标最小,y坐标最小)作为起点,然后把可能是下一个 ...

  7. Harris 角点检测

    一 .Motivation 对于做图像处理的人来说,Harris角点检测肯定听过,1988年发表的文章"A combined corner and edge detector"描述 ...

  8. C++ 我想这样用(一)

    虽然还是菜鸟,但我是一个地地道道的c程序员,甚至一度很讨厌C++(虽然现在也是). 为了在不用C++的情况下学习和使用面向对象而长期奔走,曾经用过一年的Python,后终放弃.之后很长一段时间里摆弄O ...

  9. 50道经典的JAVA编程题(目录)

    这份题从2013做到2014啊...哈哈,整理个目录吧.为了好查阅,也为了监督自己好好的去做完这50道题.当然,有些题实在做得没意思就跳过了,或者自己改题了.题目来源于:http://blog.sin ...

  10. FZU 2176 easy problem (DFS序+树状数组)

    对于一颗树,dfs遍历为每个节点标号,在进入一个树是标号和在遍历完这个树的子树后标号,那么子树所有的标号都在这两个数之间,是一个连续的区间.(好神奇~~~) 这样每次操作一个结点的子树时,在每个点的开 ...