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();
} }

最新文章

  1. DHtmlx组件获取选中行的某一列对应的值
  2. Activity intent经常使用的 FLAG
  3. Gevent的长轮询实现方法详解
  4. vim 语法高亮
  5. php的一些小笔记--时间函数
  6. openssl windows编译 32位&amp;64位
  7. NoSql研究报告
  8. IP命令
  9. Android之RecyclerView入门
  10. PyCharm 2017 免费 破解 注册 激活 教程(附 License Server 地址)(Python 编辑器 IDE 推荐)
  11. ELK安装(ubuntu)
  12. gym 101873
  13. 设计模式之观察者模式(Observer)(4)
  14. 关于datatable的数据绑定问题
  15. php fputcsv 读取不到中文文件、数据
  16. sql查询字段中的值长度最大的记录
  17. mysql根据时间查询日期的优化
  18. 《Git学习指南》学习笔记(一)
  19. python数据类型二(列表和元组)
  20. 20条IPTables防火墙规则用法! [转]

热门文章

  1. 真机无法接收到android.provider.Telephony.SMS_RECEIVED的问题
  2. EF Code First 学习笔记:约定配置
  3. 我的WCF之旅(3):在WCF中实现双工通信
  4. ollydbg z
  5. 【Python】Python-用大写字母打印你的名字
  6. 4种activity的启动模式
  7. python 网络编程(三)---TCP 服务器端客户端实现
  8. 【翻译】运行于x86机器上的FreeBSD的PCI中断
  9. uva 2572 Viva Confetti
  10. [读书笔记]了不起的node.js(三)