已阅读5页,还剩18页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
异常处理:import java.util.Scanner;public class Exception /* * param args */public static void main(String args) / TODO Auto-generated method stubint a=0;int b=0;char c; int result = 0; try a = Integer.parseInt(args0); b = Integer.parseInt(args2); c = args1.charAt(0); switch(c) case + :result=a+b;break; case - :result=a-b;break; case # :result=a*b;break; case / :result=a/b;break; default: throw new java.lang.Exception(); System.out.println(result=+result); catch(java.lang.ArrayIndexOutOfBoundsException e1) System.out.println(您的参数不够,请增加到3个); catch(java.lang.NumberFormatException e2) System.out.println(转换有问题,无法转换成数字); catch(java.lang.ArithmeticException e3) System.out.println(捕获到数字类异常,除数不能为0); System.out.println(请重新输入除数:); Scanner keyin = new Scanner(System.in); b = keyin.nextInt(); result = a/b; System.out.println(result=+result); catch(java.lang.Exception e4) System.out.println(出现异常,请自行查找何种异常); finally System.out.println(不管是否有异常,总是要执行); 从文件中提取一行,并加行号,输出到另外一个文件:import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;public class ReadFileTest /* * param args */public static void main(String args) / TODO Auto-generated method stubFile file = new File(a.txt);File file2 = new File(b.txt);tryFileWriter in1= new FileWriter(file);BufferedWriter in2= new BufferedWriter(in1);FileReader out1 = new FileReader(file2);BufferedReader out2 = new BufferedReader(out1);String str= null;int i =1;while(str=out2.readLine()!=null)in2.write(i+:+str);in2.newLine();i+;in2.flush();in1.close();in2.close();out1.close();out2.close(); catch(Exception e)System.out.println(IOError!); GUI界面,输入姓名,显示欢迎:import java.awt.Button;import java.awt.Frame;import java.awt.Label;import java.awt.Panel;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class ButtonStringTest extends Frame private Label lb;private TextField tf;private Button btn1,btn2;public ButtonStringTest(String s) / TODO Auto-generated constructor stubsuper(s);lb = new Label(姓名:);tf = new TextField(55);btn1 = new Button(确定);btn2 = new Button(清除);btn1.addActionListener(new ActionListener()Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(tf.getText().equals() | tf.getText().equals(请你输入姓名)tf.setText(请你输入姓名);elsetf.setText(你好, + tf.getText() + ,欢迎你使用java编程!););btn2.addActionListener(new ActionListener()Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubtf.setText(););Panel p = new Panel();p.add(lb);p.add(tf);p.add(btn1);p.add(btn2);this.add(p);this.setVisible(true);this.setSize(500, 100);this.setLocation(500, 500);this.addWindowListener(new WindowAdapter()Overridepublic void windowClosing(WindowEvent e) / TODO Auto-generated method stubsuper.windowClosing(e);System.exit(0););/* * param args */public static void main(String args) / TODO Auto-generated method stubnew ButtonStringTest(文本框Action事件测试);简单的文本剪辑器:public class MyNotepad extends JFrame implements ActionListener private JTextArea editText;File f = null;JFileChooser fc = new JFileChooser();String menu_str = 文件, 编辑, 文本颜色, 帮助;String file_str = 打开, 保存, 另存为, 退出;String edit_str = 复制, 剪切, 粘贴;String color_str = 红色,绿色, 蓝色, 自定义;String help_str = 帮助 ;JMenu menu = new JMenumenu_str.length;JMenuItem file_item = new JMenuItemfile_str.length;JMenuItem edit_item = new JMenuItemedit_str.length;JMenuItem color_item = new JMenuItemcolor_str.length;JMenuItem help_item = new JMenuItemhelp_str.length;public MyNotepad(String string) / TODO Auto-generated constructor stubsuper(string);editText = new JTextArea();this.add(new JScrollPane(editText);JMenuBar mb = new JMenuBar();for(int i=0; imenu.length; i+) menui = new JMenu(menu_stri);mb.add(menui);for(int i=0; ifile_item.length; i+) file_itemi = new JMenuItem(file_stri);file_itemi.addActionListener(this);menu0.add(file_itemi);for(int i=0; iedit_item.length; i+) edit_itemi = new JMenuItem(edit_stri);edit_itemi.addActionListener(this);menu1.add(edit_itemi);for(int i=0; icolor_item.length; i+) color_itemi = new JMenuItem(color_stri);color_itemi.addActionListener(this);menu2.add(color_itemi);for(int i=0; ihelp_item.length; i+) help_itemi = new JMenuItem(help_stri);help_itemi.addActionListener(this);menu3.add(help_itemi);this.setJMenuBar(mb);this.setBounds(500,300, 600,400);this.setVisible(true);/* * param args */public static void main(String args) / TODO Auto-generated method stubnew MyNotepad(我的简单记事本);Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(e.getSource() = file_item0) int n = fc.showOpenDialog(this);if(n = JFileChooser.APPROVE_OPTION) f = fc.getSelectedFile();try FileReader fr = new FileReader(f);BufferedReader br = new BufferedReader(fr);String str = null;while( (str = br.readLine() != null)editText.append(str+n);fr.close();br.close(); catch (FileNotFoundException e1) / TODO Auto-generated catch blocke1.printStackTrace(); catch (IOException e1) / TODO Auto-generated catch blocke1.printStackTrace(); else if(e.getSource() = file_item1 ) if(f = null) int n = fc.showSaveDialog(this);if(n = JFileChooser.APPROVE_OPTION) f = fc.getSelectedFile();try FileWriter fw = new FileWriter(f);BufferedWriter bw = new BufferedWriter(fw);bw.write(editText.getText();bw.flush();fw.close();bw.close(); catch (IOException e1) / TODO Auto-generated catch blocke1.printStackTrace(); else if(e.getSource() = file_item2) int n = fc.showSaveDialog(this);if(n = JFileChooser.APPROVE_OPTION) f = fc.getSelectedFile();try FileWriter fw = new FileWriter(f);BufferedWriter bw = new BufferedWriter(fw);bw.write(editText.getText();bw.flush();bw.close();fw.close(); catch (IOException e1) / TODO Auto-generated catch blocke1.printStackTrace(); else if(e.getSource() = file_item3) System.exit(0); else if(e.getSource() = edit_item0) editText.copy(); else if(e.getSource() = edit_item1) editText.cut(); else if(e.getSource() = edit_item2) editText.paste(); else if(e.getSource() = color_item0) editText.setForeground(Color.RED); else if(e.getSource() = color_item1) editText.setForeground(Color.GREEN); else if(e.getSource() = color_item2) editText.setForeground(Color.BLUE); else if(e.getSource() = color_item3) Color newColor = JColorChooser.showDialog(this, 调色板, editText.getForeground();editText.setForeground(newColor); else if(e.getSource() = help_item0) JOptionPane.showMessageDialog(this, 我的简单记事本 V1.0);简单图像查看器:public class PicLookTest extends JFrame implements ActionListener JButton btn1,btn2;int index = 0;JLabel lb;public PicLookTest(String string) / TODO Auto-generated constructor stubsuper(string);JPanel p1 = new JPanel();lb = new JLabel();lb.setIcon(new ImageIcon(pic+(index+1)+.jpg);p1.add(lb);JPanel p2 = new JPanel();btn1 = new JButton(上一张);btn2 = new JButton(下一张);btn1.addActionListener(this);btn2.addActionListener(this);p2.add(btn1); p2.add(btn2);this.add(p1);this.add(p2,BorderLayout.SOUTH);this.setVisible(true);this.setBounds(500, 500, 500, 300);Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(e.getSource() = btn1) if(index!=0)index = index - 1;elseindex = 6;System.out.println(index);lb.setIcon(new ImageIcon(pic+(index+1)+.jpg); else if(e.getSource() = btn2) if(index!=6)index = index + 1;elseindex = 0;System.out.println(index);lb.setIcon(new ImageIcon(pic+(index+1)+.jpg);/* * param args */public static void main(String args) / TODO Auto-generated method stubnew PicLookTest(picture look test);图片幻灯片播放:import java.awt.Graphics;import java.awt.Image;import java.awt.Toolkit;import javax.swing.JFrame;import javax.swing.JOptionPane;public class AnimationTest extends JFrame implements Runnable public static final int PN = 4;Image im;Thread th;int index = 0;int x = 0, y = 0; public AnimationTest(String string) / TODO Auto-generated constructor stubsuper(string);Toolkit toolkit = Toolkit.getDefaultToolkit();im = new ImagePN;for(int i=0; iPN; i+) imi = toolkit.getImage(u + (i+1) + .jpg);th = new Thread(this);th.start();this.setVisible(true);this.setBounds(500, 500, 400, 400);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Overridepublic void run() / TODO Auto-generated method stubwhile(x=400 & y=400) repaint();index+;if(index = PN) index = 0;x = x + 10;y = y + 10;try Thread.sleep(500); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();JOptionPane.showMessageDialog(this, Animation closed);Overridepublic void paint(Graphics g) / TODO Auto-generated method stubsuper.paint(g);g.drawImage(imindex, x, y, this);g.drawLine(0, y, x, y); / 横向g.drawLine(x, 0, x, y); / 竖线/* * param args */public static void main(String args) / TODO Auto-generated method stubnew AnimationTest(animation test);图片动画:import java.awt.Graphics;import java.awt.Image;import java.awt.Toolkit;import javax.swing.JFrame;import javax.swing.JOptionPane;public class AnimationTestAnother extends JFrame implements Runnable public static final int PN = 5;Image im;Thread th;int index = 0;int x = 0, y = 0; public AnimationTestAnother(String string) / TODO Auto-generated constructor stubsuper(string);Toolkit toolkit = Toolkit.getDefaultToolkit();im = new ImagePN;for(int i=0; iPN; i+) imi = toolkit.getImage(bird + i + .gif);th = new Thread(this);th.start();this.setVisible(true);this.setBounds(500, 500, 400, 400);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Overridepublic void run() / TODO Auto-generated method stubwhile(x=400 & y=400) repaint();index+;if(index = PN) index = 0;x = x + 10;y = y + 10;try Thread.sleep(100); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();JOptionPane.showMessageDialog(this, Animation closed);Overridepublic void paint(Graphics g) / TODO Auto-generated method stubsuper.paint(g);g.drawImage(imindex, x, y, this);g.drawLine(0, y, x, y); / 横向g.drawLine(x, 0, x, y); / 竖线/* * param args */public static void main(String args) / TODO Auto-generated method stubnew AnimationTestAnother(animation test);计算三角形面积:package ch01test1;public class TriangelTest /* * param args */public static void main(String args) / TODO Auto-generated method stubTriangle tri = new Triangle(3,4,5); / 创建对象double a = tri.geta(); / 获得3条边参数double b = tri.getb();double c = tri.getc();double area;double p =
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 电梯安全管理培训标准
- 2026届河北承德强基联盟高三下学期一模历史试题含答案
- 焊工技术就业指南
- 预应力混凝土结构灌浆记录
- (正式版)DB32∕T 5390-2026 口袋公园设计标准
- 2026上半年广西梧州市苍梧县引进急需紧缺专业人才11人考试参考题库及答案解析
- 2026年济宁市任城区事业单位公开招聘工作人员(教育类)(237人)考试模拟试题及答案解析
- 2026重庆广播新闻中心政务服务团队人员招聘4人考试备考题库及答案解析
- 2026四川南充阆中市第二批招募青年就业见习人员67人笔试备考试题及答案解析
- 四川天府新区兴隆街道2026年公开招聘专职网格员(14人)考试备考试题及答案解析
- 2026年安徽省慈善总会公开招聘工作人员2名笔试备考试题及答案解析
- 2026年4月广东深圳市光明区教育局招聘区属公办幼儿园财务人员13人考试参考试题及答案解析
- 陈振明公共管理学2版知识点
- GB/T 17431.2-2010轻集料及其试验方法第2部分:轻集料试验方法
- 物质的聚集状态与晶体常识【知识精讲+备课精研+高效课堂】 高二化学 课件(人教版2019选择性必修二)
- GA/T 642-2020道路交通事故车辆安全技术检验鉴定
- QC培训教学讲解课件
- 一次调频原理与作用课件
- 虚虚实实的网络世界 课件 全一册心理健康-华中师大版
- 中国石油天然气集团公司工程建设承包商管理办法
- 《西方音乐史》课件肖邦
评论
0/150
提交评论