- 创建一个类并将其命名
- 写出他的基本构架
package com.xzy0904;
public class Shape {
public void showUI(){
}
public static void main(String[] args) {
}
}
- 编译方法
3.1创建窗口并将其命名为“画板”
3.2对窗口的规格,位置,关闭,可视化(放在最后)进行设置
import javax.swing.*;
public class Shape {
public void showUI(){
JFrame jf =new JFrame("画板");
jf.setSize(800,600);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void main(String[] args) {
}
}
4.在程序入口创建使用对象
package com.xzy0904;
import javax.swing.*;
public class Shape {
public void showUI(){
//创建窗口
JFrame jf =new JFrame("画板");
jf.setSize(800,600);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void main(String[] args) {
//创建对象
Shape sp=new shape();
//对象名.方法名
sp.showUI();
}
}
5.创建一个新的类充当监听器,将这个类命名
6.引入鼠标监听器(画直线要用鼠标操作)
package com.xzy0904;
import java.awt.event.MouseListener;
public class Shape1Listener implements MouseListener{}
7.编写监听器的方法(按住Ctrl鼠标左键单机MouseListener)
8.将五个方法全部添加(点击,按住,释放,进入,离开)并将分号改为花括号
package com.xzy0904;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Shape1Listener implements MouseListener{
public void mouseClicked(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
}
9.为每一个方法添加操作名称
10.在各方法前添加坐标
11.在“按下”方法中获取起始坐标(x1.y1)
12.在“释放”方法中获取终止坐标(x2.y2)
package com.xzy0904;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Shape1Listener implements MouseListener{
int x1,y1,x2,y2;
public void mouseClicked(MouseEvent e){
System.out.println("点击");
}
public void mousePressed(MouseEvent e){
x1=e.getX();
y1=e.getY();
System.out.println("按住");
}
public void mouseReleased(MouseEvent e){
System.out.println("释放");
x2=e.getX();
y2=e.getY();
System.out.println("x:"+x1+"y:"+y1);
System.out.println("x:"+x2+"y:"+y2);
}
public void mouseEntered(MouseEvent e){
System.out.println("进入");
}
public void mouseExited(MouseEvent e){
System.out.println("离开");
}
}
13.在第一个类中添加监听器
13.1.创建监听对象【类名.对象名=new类名()】
13.2添加鼠标监听
package com.xzy0904;
import javax.swing.*;
public class Shape {
public void showUI(){
//创建窗口
JFrame jf =new JFrame("画板");
jf.setSize(800,600);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
Shape1Listener sp1=new Shape1Listener();
jf.addMouseListener(sp1);
}
public static void main(String[] args) {
//创建对象
Shape sp=new Shape();
//对象名.方法名
sp.showUI();
}
}
14.获取画笔
public class Shape {
public void showUI(){
//创建窗口
JFrame jf =new JFrame("画板");
jf.setSize(800,600);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
//创建监听对象
Shape1Listener sp1=new Shape1Listener();
//添加鼠标监听
jf.addMouseListener(sp1);
//获取画笔
Graphics g1=jf.getGraphics();
sp1 .g2=g1;
}
public class Shape1Listener implements MouseListener{
//创建画笔
Graphics g2;
int x1,y1,x2,y2;
15.画笔画出直线
public void mouseReleased(MouseEvent e){
System.out.println("释放");
x2=e.getX();
y2=e.getY();
System.out.println("x:"+x1+"y:"+y1);
System.out.println("x:"+x2+"y:"+y2);
g2.drawLine(x1,y1,x2,y2);
16.点击运行