首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

画图板的总结

2013-12-21 
画图板的小结import java.awt.BorderLayoutimport java.awt.Colorimport java.awt.Dimensionimport jav

画图板的小结
import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JColorChooser;import javax.swing.JFrame;import javax.swing.JPanel;/** * 定义一个画板界面类,该类继承自JFrame * * @author xxj * */public class DrawingBorder extends JFrame {/** * 主函数 */public static void main(String[] args) {DrawingBorder db = new DrawingBorder();// 实例化对象db.initUI();// 调用初始化界面的方法}/** * 初始化界面的方法 */public void initUI() {// 设置窗体的属性值this.setTitle("简单画板");this.setSize(new Dimension(600, 500));this.setDefaultCloseOperation(3);this.setLocationRelativeTo(null);// 调用创建北边面板的方法JPanel north = createNorthPane();this.add(north, BorderLayout.NORTH);// 添加到窗体的北边// 调用创建中间面板的方法JPanel center = createCenterPane();this.add(center, BorderLayout.CENTER);// 添加到窗体的中间this.setVisible(true);//获取事件源上的画布对象 如果窗体未可见,那么就无法获取到画布对象。Graphics g = center.getGraphics();//实例化事件处理类的对象,并且带入对应的参数DrawingListener dl = new DrawingListener(g,this);//给事件源添加鼠标监听器方法,绑定事件处理类的对象dlcenter.addMouseListener(dl);center.addMouseMotionListener(dl);}/** * 事件处理类的对象 * 使用匿名内部类来实现ActionListener接口的具体实现 */private ActionListener al = new ActionListener(){//事件处理方法public void actionPerformed(ActionEvent e) {str = e.getActionCommand();//获取事件源的动作命令值。System.out.println("str = "+str);}};//定义字符串属性,用来存储图形private String str = "Line";protected Color color;/** * 获取图形属性值的方法 */public String getStr(){return str;}/** * 创建北边面板的方法 * @param 返回创建好的JPanel对象 */public JPanel createNorthPane() {JPanel jp = new JPanel();// 实例化面板对象/** * 接口不能用来实例化对象;但是在实例化接口对象时,带上一对大括号,就表示在实例化对象时,该接口有了具体的实现 * {}就是一个类,但是没有类名,而且是在另一个类中。 匿名内部类。 */jp.setPreferredSize(new Dimension(30, 80));// 设置大小ActionListener al = new ActionListener() {// 事件处理方法public void actionPerformed(ActionEvent e) {// 判断点击的是否是Color按钮if (e.getActionCommand().equals("Color")) {//弹出一个颜色选择界面,供用户选择颜色,选择好的颜色会返回给colorcolor = JColorChooser.showDialog(null, "颜色选择器",Color.BLACK);System.out.println("color= "+color);} else {// 获取按钮上的文本内容str = e.getActionCommand();System.out.println("str = " + str);}}};// 定义数组,存储按钮上显示的文字String[] array = { "Line", "Rect", "Oval", "RoundRect","Polygon","Pencil", "Color", "Easer","Brush"};// 遍历数组,循环创建JButton对象for (int j= 0; j < array.length; j++) {// 创建按钮,按钮上显示的内容是数组中的元素JButton jbu = new JButton(array[j]);// 给事件源添加动作监听器方法,绑定事件处理类的对象aljbu.addActionListener(al); jp.add(jbu);}return jp;}/** * 创建中间面板的方法 * @param 返回创建好的JPanel对象 */public JPanel createCenterPane() {JPanel panel = new JPanel();// 实例化面板对象panel.setBackground(Color.WHITE);// 设置面板的背景色return panel;}}

?

import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JColorChooser;import javax.swing.JFrame;import javax.swing.JPanel;/** * 定义一个画板界面类,该类继承自JFrame *  * @author xxj *  */public class DrawingBorder extends JFrame {/** * 主函数 */public static void main(String[] args) {DrawingBorder db = new DrawingBorder();// 实例化对象db.initUI();// 调用初始化界面的方法}/** * 初始化界面的方法 */public void initUI() {// 设置窗体的属性值this.setTitle("简单画板");this.setSize(new Dimension(600, 500));this.setDefaultCloseOperation(3);this.setLocationRelativeTo(null);// 调用创建北边面板的方法JPanel north = createNorthPane();this.add(north, BorderLayout.NORTH);// 添加到窗体的北边// 调用创建中间面板的方法JPanel center = createCenterPane();this.add(center, BorderLayout.CENTER);// 添加到窗体的中间this.setVisible(true);//获取事件源上的画布对象   如果窗体未可见,那么就无法获取到画布对象。Graphics g = center.getGraphics();//实例化事件处理类的对象,并且带入对应的参数DrawingListener dl = new DrawingListener(g,this);//给事件源添加鼠标监听器方法,绑定事件处理类的对象dlcenter.addMouseListener(dl);center.addMouseMotionListener(dl);}/** * 事件处理类的对象 * 使用匿名内部类来实现ActionListener接口的具体实现 */private ActionListener al = new ActionListener(){//事件处理方法public void actionPerformed(ActionEvent e) {str = e.getActionCommand();//获取事件源的动作命令值。System.out.println("str = "+str);}};//定义字符串属性,用来存储图形private String str = "Line";protected Color color;/** * 获取图形属性值的方法 */public String getStr(){return str;}/** * 创建北边面板的方法 * @param 返回创建好的JPanel对象 */public JPanel createNorthPane() {JPanel jp = new JPanel();// 实例化面板对象/** * 接口不能用来实例化对象;但是在实例化接口对象时,带上一对大括号,就表示在实例化对象时,该接口有了具体的实现 * {}就是一个类,但是没有类名,而且是在另一个类中。 匿名内部类。 */jp.setPreferredSize(new Dimension(30, 80));// 设置大小ActionListener al = new ActionListener() {// 事件处理方法public void actionPerformed(ActionEvent e) {// 判断点击的是否是Color按钮if (e.getActionCommand().equals("Color")) {//弹出一个颜色选择界面,供用户选择颜色,选择好的颜色会返回给colorcolor = JColorChooser.showDialog(null, "颜色选择器",Color.BLACK);System.out.println("color= "+color);} else {// 获取按钮上的文本内容str = e.getActionCommand();System.out.println("str = " + str);}}};// 定义数组,存储按钮上显示的文字String[] array = { "Line", "Rect", "Oval", "RoundRect","Polygon","Pencil", "Color", "Easer","Brush"};// 遍历数组,循环创建JButton对象for (int j= 0; j < array.length; j++) {// 创建按钮,按钮上显示的内容是数组中的元素JButton jbu = new JButton(array[j]);// 给事件源添加动作监听器方法,绑定事件处理类的对象aljbu.addActionListener(al);               jp.add(jbu);}return jp;}/** * 创建中间面板的方法 * @param 返回创建好的JPanel对象 */public JPanel createCenterPane() {JPanel panel = new JPanel();// 实例化面板对象panel.setBackground(Color.WHITE);// 设置面板的背景色return panel;}}

?

热点排行