Java随堂小记09
异常处理机制
检查性异常:程序员无法预见的。如打开一个不存在文件时异常发生。编译时不能被简单地忽略
运行时异常:运行时异常是可能被程序员避免的异常。编译时可以被忽略
错误:错误不是异常,而是脱离程序员控制的问题。通常在代码中被忽略
-
快捷键:Ctrl + Alt + T 自动补全异常
throw:主动抛出异常,在方法体内使用
throws:方法中处理不了这个异常,方法上抛出异常(像极了甩锅的你)
finally:善后工作
e.printStackTrace():打印错误的栈信息 -
捕捉多个异常要从小到大:Error e < Exception e < Throwable t
package www.Dongyi.oop.Demo12;
public class Test {
public static void main(String[] args) {
int a=1;
int b=0;
try {
System.out.println(a/b);
} catch (Exception e) {
System.out.println("异常,除数不能为0");
e.printStackTrace();//打印错误的栈信息
} finally {
System.out.println("finally");
}
}
//方法中处理不了这个异常,方法上抛出异常
public void test(int a,int b) throws ArithmeticException{
//throw主动抛出异常
if(b==0){
throw new ArithmeticException();//一般用于方法中
}
}
}
自定义异常
package www.Dongyi.oop.Demo13;
public class MyException extends Exception{
private int detail;
public MyException(int a) {
this.detail = a;
}
//toString:异常的打印信息
@Override
public String toString() {
return "MyException{" +
"detail=" + detail +
'}';
}
}
package www.Dongyi.oop.Demo13;
public class Test {
//写一个存在异常的方法
public static void test(int a) throws MyException {
System.out.println("传递的参数为:"+ a);
if(a>10){
throw new MyException(a);//可抛出也可捕获
}
System.out.println("ok");
}
public static void main(String[] args) {
try {
test(11);
} catch (MyException e) {
//增加一些处理异常的代码
System.out.println("MyException=>"+ e);
}
}
}
总结:
- 处理运行时异常时,采用逻辑去合理规避同时辅助try-catch处理
- 在多重catch块后面,可以加一个catch(Exception)来处理可能会被遗漏的异常
- 对于不确定的代码,也可以加上try-cath,处理潜在异常
- 尽量去处理异常,切记只是简单地调用printStackTrace()去打印异常
- 根据业务需求和异常类型去处理异常
- 尽量添加finally语句块去释放占用的资源
GUI编程
GUI的核心技术: Swing 、AWT
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件(web会有)
- 鼠标
- 键盘事件
Frame窗口
不要忘了psvm
package www.Dongyi.GUI;
import java.awt.*;
public class FrameTest {
public static void main(String[] args) {
Frame frame = new Frame("我的Frame窗口");
//设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//背景颜色 Color
Color color = new Color(0x9F3FCE);
frame.setBackground(color);
//弹出的初始位置(距离屏幕最左上角)
frame.setLocation(200,200);
//设置大小固定(setResizable:设置大小不固定,所以要设置成false)
frame.setResizable(false);
}
}
回顾封装:
**package www.Dongyi.GUI;
import java.awt.*;
public class MyFrameTest {
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.BLUE);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.YELLOW);
MyFrame myFrame3 = new MyFrame(500, 100, 200, 200, Color.RED);
MyFrame myFrame4 = new MyFrame(700, 100, 200, 200, Color.BLACK);
}
}
class MyFrame extends Frame {
//除了Frame方法以外,还有自己的方法
static int id = 0;//可能存在多个窗口,我们需要一个计数器
//构造器封装一下
public MyFrame(int x, int y, int w, int h, Color color) {
super("MyFrame+" + (++id));
setBackground(color);
setVisible(true);
//设置长宽和起始位置坐标
setBounds(x, y, w, h);
setResizable(false);
}
}**
Panel 面板
Panel:面板,不能单独存在
明白什么是适配器模式new一个 WindowAdapter(),选择需要的功能
add()添加功能
package www.Dongyi.GUI;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//Panel 可以看成一个空间,但是不能单独存在,得放在Frame上
public class PanelTest {
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
frame.setLayout(null);//设置布局选项为默认
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(147, 72, 222));
//panel 设置坐标,相对于Frame的坐标
panel.setBounds(150,150,200,200);
panel.setBackground(new Color(208, 208, 123));
//frame.add(panel)frame添加面板
frame.add(panel);//Panel经过三层继承,最终继承了Component
frame.setVisible(true);
//监听时间,监听窗口关闭事件 System.exit(0)
//适配器模式: new一个子类 选择 new WindowAdapter()
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
}
});
}
}
三种布局管理器
System.exit(0) :用于结束程序
流式布局 FlowLayout:从左到右
package www.Dongyi.GUI;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Botton {
public static void main(String[] args) {
Frame frame = new Frame();
//组件-按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//设置为流式布局
//frame.setLayout(new FlowLayout());默认是中
//frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
frame.setSize(200,200);
frame.setVisible(true);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
东南西北中 BorderLayout
package www.Dongyi.GUI;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Botton1 {
public static void main(String[] args) {
Frame frame = new Frame("添加按钮");
Button east = new Button("East");
Button west = new Button("West");
Button south = new Button("South");
Button north = new Button("North");
Button center = new Button("Center");
frame.add(east,BorderLayout.EAST);
frame.add(west ,BorderLayout.WEST);
frame.add(south ,BorderLayout.SOUTH);
frame.add(north ,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);
frame.setBounds(200,200,300,300);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
表格布局 GridLayout
frame.pack();可以让布局变得好看
package www.Dongyi.GUI;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Botton2 {
public static void main(String[] args) {
Frame frame = new Frame("表格布局");
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
Button button6 = new Button("button6");
frame.setLayout(new GridLayout(3,2));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.pack();//让布局变得好看
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
课堂练习
先构思再写:
首先Frame分成两行frame1中分BorderLayout“东西中”三块,在“中”添加表格布局GridLayout两行一列
frame2也分为“东西中”三块,在“中”添加表格布局GridLayout两行两列
package www.Dongyi.GUI;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class BottonTest {
public static void main(String[] args) {
Frame frame = new Frame("课堂练习");
frame.setVisible(true);
frame.setBounds(300,300,300,300);
frame.setLayout(new GridLayout(2,1));
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2,1));
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2,2));
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
p1.add(button1,BorderLayout.EAST);
p1.add(button2,BorderLayout.WEST);
p1.add(p2,BorderLayout.CENTER);
p2.add(button3);
p2.add(button4);
Button button5 = new Button("button5");
Button button6 = new Button("button6");
Button button7 = new Button("button7");
Button button8 = new Button("button8");
Button button9 = new Button("button9");
Button button10 = new Button("button10");
p3.add(button5,BorderLayout.EAST);
p3.add(button6,BorderLayout.WEST);
p3.add(p4,BorderLayout.CENTER);
p4.add(button7);
p4.add(button8);
p4.add(button9);
p4.add(button10);
frame.add(p1);
frame.add(p3);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}