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

第七次小结(java编写简单画板)

2013-08-01 
第七次总结(java编写简单画板)##################################分割线###############################

第七次总结(java编写简单画板)

##################################分割线##################################

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.JFrame;
import javax.swing.JPanel;

public class DrawingBoard extends JFrame{

?/**
? * @param args
? */
?public static void main(String[] args) {
?
??DrawingBoard db = new DrawingBoard();
??db.initUI();

?}
?//画板类的属性值
?private String shape="line";
?//构造方法
?public DrawingBoard() {
??
?}
?//获得按钮所指示的形状的方法
?public String getShapes(){
??return shape;
?}
?//初始化界面的方法
?public void initUI() {
??this.setTitle("简单画板");
??this.setSize(600, 500);
??this.setDefaultCloseOperation(3);
??this.setLocationRelativeTo(rootPane);
??
??JPanel northPanel =new JPanel();
??
??//创建按钮组件并设置大小
??JButton line =new JButton("直线");
??line.setActionCommand("line");
??JButton rect =new JButton("矩形");
??rect.setActionCommand("rect");
??JButton oval =new JButton("圆形");
??oval.setActionCommand("oval");
??JButton roundrect =new JButton("圆角矩形");
??roundrect.setActionCommand("roundrect");
??JButton triangle =new JButton("三角形");
??triangle.setActionCommand("triangle");
??line.setPreferredSize(new Dimension(90,40));
??rect.setPreferredSize(new Dimension(90,40));
??oval.setPreferredSize(new Dimension(90,40));
??roundrect.setPreferredSize(new Dimension(90,40));
??triangle.setPreferredSize(new Dimension(90,40));
??
??
??//匿名内部类直接“实例化”一个接口
??ActionListener al = new ActionListener(){
???//事件处理方法
???public void actionPerformed(ActionEvent e) {
????//获取按钮的动作命令
????shape = e.getActionCommand();
????System.out.println("shape = "+shape);
???}
???
??};
??//为各个按钮组件添加监听器方法并绑定事件处理类对象
??line.addActionListener(al);
??oval.addActionListener(al);
??rect.addActionListener(al);
??roundrect.addActionListener(al);
??triangle.addActionListener(al);
??//将各个按钮组件添加到南部面板上
??northPanel.add(line);
??northPanel.add(rect);
??northPanel.add(oval);
??northPanel.add(roundrect);
??northPanel.add(triangle);
??//将南部面板添加到窗体的南部
??this.add(northPanel,BorderLayout.NORTH);
??
??//创建中部面板
??JPanel centerPanel = new JPanel();
??//设置背景色为白色
??centerPanel.setBackground(Color.WHITE);
??//将中部面板添加到窗体的中部
??this.add(centerPanel,BorderLayout.CENTER);
??
??this.setVisible(true);
??//画图形必须在setVisible之后
??
??
??//
??Graphics g=centerPanel.getGraphics();
??//创建一个鼠标事件处理类对象
??DrawingListener dl = new DrawingListener(this,g);
??//为中部面板添加监听器方法并绑定处理类对象
??centerPanel.addMouseListener(dl);
??
?}

?
}

热点排行