Java画图板程序设计报告(4)
helpMenu.add(aboutItem); bar.add(helpMenu);
items=new ImageIcon[names.length];
4. 工具栏图形按钮功能实现
创建基本图形按钮、导入图标、加入动作侦听器、字体风格选择、字体选择、画图板等功能的实现。 代码如下:
//创建各种基本图形的按钮
drawingArea=new DrawPanel(); choices=new JButton[names.length];
buttonPanel = new JToolBar( JToolBar.VERTICAL ) ; buttonPanel = new JToolBar( JToolBar.HORIZONTAL) ; ButtonHandler handler=new ButtonHandler(); ButtonHandler1 handler1=new ButtonHandler1();
//导入图形图标,这些图标都存放在与源文件相同的目录下面 for(int i=0;i items[i]=new ImageIcon(names[i] + \ choices[i]=new JButton(\ choices[i].setToolTipText(tipText[i]); buttonPanel.add(choices[i]); } //将动作侦听器加入按钮里面 for(int i=3;i choices[i].addActionListener(handler); } choices[0].addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { newFile(); } } ); choices[1].addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { 16 / 31 loadFile(); } } ); choices[2].addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { saveFile(); } } ); choices[choices.length-3].addActionListener(handler1); choices[choices.length-2].addActionListener(handler1); choices[choices.length-1].addActionListener(handler1); //字体风格选择 styles=new JComboBox(styleNames); styles.setMaximumRowCount(8); styles.addItemListener( new ItemListener(){ public void itemStateChanged(ItemEvent e) { style1=styleNames[styles.getSelectedIndex()]; } } ); //字体选择 bold=new JCheckBox(\ italic=new JCheckBox(\ checkBoxHandler cHandler=new checkBoxHandler(); bold.addItemListener(cHandler); italic.addItemListener(cHandler); JPanel wordPanel=new JPanel(); buttonPanel.add(bold); buttonPanel.add(italic); buttonPanel.add(styles); styles.setMinimumSize( new Dimension ( 50, 20 ) ); styles.setMaximumSize(new Dimension ( 100, 20 ) ); Container c=getContentPane(); super.setJMenuBar( bar ); 17 / 31 c.add(buttonPanel,BorderLayout.NORTH); c.add(drawingArea,BorderLayout.CENTER); statusBar=new JLabel(); c.add(statusBar,BorderLayout.SOUTH); statusBar.setText(\ createNewItem(); setSize(width,height); show(); } //按钮侦听器ButtonHanler类,内部类,用来侦听基本按钮的操作 public class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { for(int j=3;j if(e.getSource()==choices[j]) {currentChoice=j; createNewItem(); repaint();} } } } //按钮侦听器ButtonHanler1类,用来侦听颜色选择、画笔粗细设置、文字输入按钮的操作 public class ButtonHandler1 implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource()==choices[choices.length-3]) {chooseColor();} if(e.getSource()==choices[choices.length-2]) {setStroke();} if(e.getSource()==choices[choices.length-1]) {JOptionPane.showMessageDialog(null, \hit the drawing pad to choose the word input position\ \INFORMATION_MESSAGE ); currentChoice=14; createNewItem(); repaint(); } 18 / 31 } } //鼠标事件mouseA类,继承了MouseAdapter,用来完成鼠标相应事件操作 class mouseA extends MouseAdapter { public void mousePressed(MouseEvent e) {statusBar.setText(\ \设置状态提示 itemList[index].x1=itemList[index].x2=e.getX(); itemList[index].y1=itemList[index].y2=e.getY(); //如果当前选择的图形是随笔画或者橡皮擦,则进行下面的操作 if(currentChoice==3||currentChoice==13) { itemList[index].x1=itemList[index].x2=e.getX(); itemList[index].y1=itemList[index].y2=e.getY(); index++; createNewItem(); } //如果当前选择的图形式文字输入,则进行下面操作 if(currentChoice==14) { itemList[index].x1=e.getX(); itemList[index].y1=e.getY(); String input; input=JOptionPane.showInputDialog( \ itemList[index].s1=input; itemList[index].x2=f1; itemList[index].y2=f2; itemList[index].s2=style1; index++; currentChoice=14; createNewItem(); drawingArea.repaint(); } } public void mouseReleased(MouseEvent e) 19 / 31 {statusBar.setText(\ \ if(currentChoice==3||currentChoice==13) { itemList[index].x1=e.getX(); itemList[index].y1=e.getY(); } itemList[index].x2=e.getX(); itemList[index].y2=e.getY(); repaint(); index++; createNewItem(); } public void mouseEntered(MouseEvent e) { statusBar.setText(\ \ } public void mouseExited(MouseEvent e) { statusBar.setText(
…… 此处隐藏:2388字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [资格考试]机械振动与噪声学部分答案
- [资格考试]空调工程课后思考题部分整合版
- [资格考试]电信登高模拟试题
- [资格考试]2018年上海市徐汇区中考物理二模试卷(
- [资格考试]坐标转换及方里网的相关问题(椭球体、
- [资格考试]语文教研组活动记录表
- [资格考试]广东省2006年高应变考试试题
- [资格考试]LTE学习总结—后台操作-数据配置步骤很
- [资格考试]北京市医疗美容主诊医师和外籍整形外科
- [资格考试]中学生广播稿400字3篇
- [资格考试]CL800双模站点CDMA主分集RSSI差异过大
- [资格考试]泵与泵站考试复习题
- [资格考试]4个万能和弦搞定尤克里里即兴弹唱(入
- [资格考试]咽喉与经络的关系
- [资格考试]《云南省国家通用语言文字条例》学习心
- [资格考试]标准化第三范式
- [资格考试]GB-50016-2014-建筑设计防火规范2018修
- [资格考试]五年级上册品社复习资料(第二单元)
- [资格考试]2.对XX公司领导班子和班子成员意见建议
- [资格考试]关于市区违法建设情况的调研报告
- 二0一五年下半年经营管理目标考核方案
- 2014年春八年级英语下第三次月考
- 北师大版语文二年级上册第十五单元《松
- 2016国网江苏省电力公司招聘高校毕业生
- 多渠道促家长督导家长共育和谐 - 图文
- 2018 - 2019学年高中数学第2章圆锥曲线
- 竞争比合作更重要( - 辩论准备稿)课
- “案例积淀式”校本研训的实践与探索
- 新闻必须客观vs新闻不必客观一辩稿
- 福师大作业 比较视野下的外国文学
- 新编大学英语第二册1-7单元课文翻译及
- 年产13万吨天然气蛋白项目可行性研究报
- 河南省洛阳市2018届高三第二次统一考试
- 地下车库建筑设计探讨
- 南京大学应用学科教授研究方向汇编
- 2018年八年级物理全册 第6章 第4节 来
- 毕业论文-浅析余华小说的悲悯性 - 以《
- 2019年整理乡镇城乡环境综合治理工作总
- 广西民族大学留学生招生简章越南语版本
- 故宫旧称紫禁城简介




