JAVA学习日志之画图板重绘

画板类的代码:

package 画图板重绘;

import java.awt.FlowLayout;
import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JFrame;

//画板类
public class Huaban {
Graphics g;
public static void main(String[] args){
Huaban hb = new Huaban();
hb.show();
}
private void show() {
JFrame jf = new JFrame();
jf.setTitle("画图板重绘");
jf.setSize(800, 800);
//设置居中显示
jf.setLocationRelativeTo(null);

jf.setLayout(new FlowLayout());
jf.setDefaultCloseOperation(3);
//在画布上创建鼠标监听器
Mouse m = new Mouse();
jf.addMouseListener(m);

//添加按钮
String[] text = {"直线","矩形"};
for(int i=0;i<text.length;i++)
{
JButton btn = new JButton(text[i]);
jf.add(btn);
btn.addActionListener(m);//给按钮添加动作监听器
}
jf.setVisible(true);
//获取画笔
Graphics g = jf.getGraphics();
//将画笔传到监听器去
m.g = g;
}
}

---------------------

posted @ 2019-08-12 21:10  水至清明  阅读(177)  评论(0编辑  收藏  举报