< 今日知识点 >
- 简易计算器
- 组合+内部类回顾复习
- 画笔paint
- 鼠标监听事件、模拟画图工具
·
·
·
·
—01 简易计算器
代码实现: (优化代码前)
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//简易计算器
public class TestCalc {
public static void main(String[] args) {
Frame Calculator = new Calculator();
WindowClose(Calculator);
}
private static void WindowClose (Frame frame){ //关闭窗口的方法
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//计算器类
class Calculator extends Frame{
public Calculator(){
//3个文本框
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(num1,num2,num3));
//1个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout()); //流式布局
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalculatorListener implements ActionListener{
//获取三个变量
private TextField num1,num2,num3;
public MyCalculatorListener(TextField num1,TextField num2,TextField num3){
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
public void actionPerformed(ActionEvent e){
//1.获得加数和被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2、将这个值加法运算后,放到第三个框
num3.setText(""+(n1+n2)); //需要的是一个字符串,所以前面加“”
//3、清除前两个框中的数字
num1.setText("");
num2.setText("");
}
}
运行结果:
·
·
·
—02 画笔paint
代码实现:
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPaint {
public static void main(String[] args) {
Mypaint mypaint = new Mypaint();
mypaint.loadFrame();
WindowClose(mypaint);
}
private static void WindowClose (Frame frame){ //关闭窗口的方法
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class Mypaint extends Frame {
public void loadFrame(){
setBounds(200,200,600,500);
setVisible(true);
}
//画笔
public void paint(Graphics g) {
//画笔需要有颜色,可以画画
g.setColor(Color.pink);
g.drawOval(100,100,100,100);
g.fillOval(100,100,100,100); //实心的园
g.setColor(Color.orange);
g.drawRect(200,200,200,200);
g.fillRect(200,200,200,200);
//养成一个习惯,画笔用完将他还原到最初的颜色
}
}
运行结果:
·
·
·
—03 鼠标监听事件、模拟画图工具
目的:想要实现鼠标画画
代码实现:
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;
public class TestMouseListener {
public static void main(String[] args) {
MyFrame myframe = new MyFrame("画图");
WindowClose(myframe);
}
private static void WindowClose (Frame frame){ //关闭窗口的方法
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//自己的类
class MyFrame extends Frame {
//画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
ArrayList points; //集合
public MyFrame(String title){
super(title); //继承父类标题
setBounds(200,200,400,300); //画板
//存鼠标点击的点到集合中
points = new ArrayList<>();
setVisible(true);
//鼠标监听器,针对这个窗口:this
this.addMouseListener(new MyMouseListener());
}
public void paint(Graphics g){
//画图、监听鼠标的事件
Iterator iterator = points.iterator(); //迭代器
while (iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.orange);
g.fillOval(point.x,point.y,10,10); //画点:其实是画圆形
}
}
//添加一个点到界面上
public void addPaint(Point point){
points.add(point); //把鼠标新点的point添加到集合中
}
//适配器模式
private class MyMouseListener extends MouseAdapter {
//鼠标按下,弹起,按住不放
public void mousePressed(MouseEvent e){
MyFrame frame = (MyFrame) e.getSource();
//这个我们点击的时候就会在界面上产生一个点!!画
//这个点就是鼠标的点
frame.addPaint(new Point(e.getX(),e.getY())); //addPaint 是自己写的一种方法
//每次点击鼠标都需要重新画一遍
frame.repaint();//刷新一个页面
}
}
}
思路图:
运行结果:
-
窗口刚打开时
-
用鼠标随意画点