A(){ B(); } B(){ 代码段 } 1运行时异常 class AgeIsIllegal extends RuntimeException{ public AgeIsIllegal(String message){ super(message); } } 函数内部代码段throw new AgeIsIllegal(….),A函数的B部分再用try-catch围住
2异常 public class NameNullException extends Exception{ public NameNullException(String message) { super(message); } } 函数内部代码段throw new NameNullException(….),然后B函数把异常抛出去(throws NameNullException),A函数的B部分再用try-catch围住
public class App3 extends JFrame{ List<Student>list=new ArrayList<>(); public App3(){ super("1819A卷"); this.setSize(500,400); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); init(); }
private void init() { JPanel jp=new JPanel(); jp.setLayout(new FlowLayout()); JLabel nameLabel = new JLabel("姓名:"); JTextField name = new JTextField(5); // 调整文本框宽度 JLabel ageLabel = new JLabel("年龄:"); JTextField age = new JTextField(5); // 调整文本框宽度 jp.add(nameLabel); jp.add(name); jp.add(ageLabel); jp.add(age); JLabel msgLa = new JLabel(); // 用于显示信息 jp.add(msgLa); // 将消息标签添加到面板 JButton jb1=new JButton("添加"); JButton jb2=new JButton("取消"); JButton jb3=new JButton("显示"); this.add(jp); jp.add(jb1); jp.add(jb2); jp.add(jb3); public static void main(String[] args) { App3 app3=new App3(); app3.setVisible(true); } }
常见例题 一、程序运行初始界面如下左图,界面如下图所示。每次点击“计数”按钮,均会在相 关标签中给出计数提示;点击退出按钮,则会结束程序。请补充完成此程序。 a.)程序运行的初始界面 b.)点击“计数”按钮 9 次后的界面 _(1) /* 请补充需要导入的包 / class MyGUI extends JFrame (2){ private JButton b_count,b_exit; private JLabel cLa; private int count=0; public MyGUI() { setLayout(new FlowLayout()); b_count = new JButton(“计数”); b_exit= new JButton(“退出”); cLa=new Jlabel(“0”); add(cLa); add(b_count); add(b_exit); setVisible(true); _(3)/ 剩余部分,请补充完整 / } _(4)/ 其它方法,请补充完整 */ public static void main(String[] args) { new MyGUI(); } }
public App1(int maxStudentNum) { st = new Student1[maxStudentNum]; len = 0; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500, 100); setBackground(Color.lightGray); setLocation(300, 240); setLayout(new FlowLayout());
name = new JTextField(10); // 调整文本框宽度 age = new JTextField(5);
public class T implements Runnable{ private int data; @Override public void run() { //synchronized (this) { int newdata=data; System.out.print(Thread.currentThread().getName() + ":"); for (int i = 1; i <= 10; i++) { System.out.print(newdata + " "); newdata=data+newdata; } System.out.println(); //} } public T(int data){ this.data=data; } } package FinalTest.Thread.Moni;
public class Print_Server { private int x; } public class T implements Runnable{ private Thread t; private Print_Server data; private String name; private String[] say; public void start(){ t.start(); } public T(Print_Server d,String n, String []s){ data=d; name=n; say=s; t=new Thread(this); }
@Override // public void run() { // System.out.print(name+"说: "); // for (String s : say) { // System.out.print(s+" "); // } // System.out.println(); // } public void run(){ synchronized(data){ System.out.print(name+"说:"); for(int i=0; i<say.length; i++) System.out.print(say[i]); System.out.println (); } } } public class App2 { public static void main(String[] args) throws InterruptedException { Print_Server d=new Print_Server();
字节的编码和解码形式必须统一 编码 string->byte byte[ ]bytes=” …”.getBytes(“GBK”); 解码 byte->string String s=new String(bytes,”GBK”); File I/O流 字节流适合数据传送,字符流适合文本内容 InputStream is = new FileInputStream(“file-io-app\src\itheima03.txt”); byte[] buffer = new byte[3]; int len; // 记住每次读取了多少个字节。 abc 66 while ((len = is.read(buffer)) != -1){ // 注意:读取多少,倒出多少。 String rs = new String(buffer, 0 , len); System.out.print(rs); }
//覆盖管道:覆盖之前的数据 // OutputStream os = new FileOutputStream(“file-io-app/src/itheima04out.txt”); // 追加数据的管道 OutputStream os = new FileOutputStream(“file-io-app/src/itheima04out.txt”, true); byte[] bytes = “我爱你中国abc”.getBytes(); os.write(bytes);
1.复制文本文件 public static void main(String[] args) throws Exception { // 需求:复制照片。 // 1、创建一个字节输入流管道与源文件接通 InputStream is = new FileInputStream("file-io-app\\src\\itheima03.txt"); // 2、创建一个字节输出流管道与目标文件接通。 OutputStream os = new FileOutputStream("file-io-app\\src\\itheima03copy.txt");
System.out.println(10 / 0); // 3、创建一个字节数组,负责转移字节数据。 byte[] buffer = new byte[1024]; // 1KB. // 4、从字节输入流中读取字节数据,写出去到字节输出流中。读多少写出去多少。 int len; // 记住每次读取了多少个字节。 while ((len = is.read(buffer)) != -1){ os.write(buffer, 0, len); }
public class BufferedInputStreamTest1 { public static void main(String[] args) { try ( InputStream is = new FileInputStream("io-app2/src/itheima01.txt"); // 1、定义一个字节缓冲输入流包装原始的字节输入流 InputStream bis = new BufferedInputStream(is);
OutputStream os = new FileOutputStream("io-app2/src/itheima01_bak.txt"); // 2、定义一个字节缓冲输出流包装原始的字节输出流 OutputStream bos = new BufferedOutputStream(os); ){
byte[] buffer = new byte[1024]; int len; while ((len = bis.read(buffer)) != -1){ bos.write(buffer, 0, len); } System.out.println("复制完成!!");
public String getLoginName() { return loginName; }
public void setLoginName(String loginName) { this.loginName = loginName; }
public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public String getPassWord() { return passWord; }
public void setPassWord(String passWord) { this.passWord = passWord; }
@Override public String toString() { return "User{" + "loginName='" + loginName + '\'' + ", userName='" + userName + '\'' + ", age=" + age + ", passWord='" + passWord + '\'' + '}'; } } public class Test1ObjectOutputStream { public static void main(String[] args) { try (
// 2、创建一个对象字节输出流包装原始的字节 输出流。 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E://fxxk.txt")); ){ // 1、创建一个Java对象。 User u = new User("admin", "张三", 32, "666888xyz");