关于JAVA的问题。求教达人

关于JAVA的问题。求教达人,第1张

你这个还没有注册监听之类的吧...所以按了都没有反应

我这里有个我写的记事本程序你可以看看...不懂的在交流..我QQ718596512

/**

* 记事本程序

* 打开,保存支持的文件

* 可以使用快捷键 *** 作(JMenu项是ALT+字母)

* 可以修改背景色,设置字体大小,种类

* 为JMenuItem添加快捷键JmenuItem jmi=new JmenuItem("打开",'O')

* 键盘加速即在不打开菜单的情况下自行打开相应菜单项的功能

* jmi1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK))

* 实现格式即字体窗格(JColorChooser,JSlider)

* 使用到

* JMenuBar JMenu JMenuItem JTextArea JScrollPane

* UIManager JFileChooser FileInputStream FileOutPutStream

* FileFilter(javax.swing.filechooser.FileFilter 另外是java.io中的一个接口)

* 实现d出菜单JPopupMenu使用鼠标适配器 类实现(MouseAdapter)

* 没有实现查找--虽然查找很简单但是要将它在jta中定位貌似不好搞

*/

package com.happy

import javax.swing.*

import javax.swing.border.TitledBorder

//注意这个包是必须的

import javax.swing.event.ChangeEvent

import javax.swing.event.ChangeListener

import javax.swing.filechooser.FileFilter

import java.awt.*

import java.awt.event.*

import java.io.*

import java.awt.event.MouseAdapter

public class NotePadWin extends JFrame implements ActionListener{

/**

*

*/

private static final long serialVersionUID = 1L

/**

* @param args

*/

//数据成员

JMenuBar jmb=null

JMenu jm1=null//文件

JMenu jm2=null//编辑

JMenu jm3=null//格式

JMenu jm4=null//帮助

JPopupMenu jpm=null

JMenuItem jmi1=null

JMenuItem jmi2=null

JMenuItem jmi3=null

JMenuItem jmi33=null

JMenuItem jmi4=null

JMenuItem jmi44=null

JMenuItem jmi5=null

JMenuItem jmi6=null

JMenuItem jmi7=null

JMenuItem jmi8=null

JMenuItem jmi9=null

JMenuItem jmi99=null

JMenuItem jmi10=null

static JTextArea jta=null

JScrollPane jsp=null

JFileChooser jfc=null

//记录当前文本内容

StringBuffer con=null

FontWin fw=null

public NotePadWin(){

//实例化

jmb=new JMenuBar()

jm1=new JMenu("文件(F)")

jm2=new JMenu("编辑(Z)")

jm3=new JMenu("格式(G)")

jm4=new JMenu("帮助(H)")

//添加快捷键

jm1.setMnemonic('F')

jm2.setMnemonic('Z')

jm3.setMnemonic('G')

jm4.setMnemonic('H')

//这样的快捷键必须打开菜单才有效

jmi1=new JMenuItem("新建(N)",'N')

jmi2=new JMenuItem("打开(O)",'O')

jmi3=new JMenuItem("保存(S)",'S')

jmi33=new JMenuItem("保存(S)")

jmi4=new JMenuItem("退出(E)",'E')

jmi44=new JMenuItem("退出(E)")

//设置键盘加速

jmi1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK))

jmi2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK))

jmi3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK))

jmi4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK))

jm1.add(jmi1)

jm1.add(jmi2)

jm1.add(jmi3)

// 添加分隔线

jm1.addSeparator()

jm1.add(jmi4)

jmi5=new JMenuItem("查找")

jmi6=new JMenuItem("全选")

jm2.add(jmi5)

jm2.add(jmi6)

jmi7=new JMenuItem("帮助主题")

jmi7.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,InputEvent.CTRL_MASK))

jmi8=new JMenuItem("关于")

jm4.add(jmi7)

jm4.add(jmi8)

jmi9=new JMenuItem("字体")

jmi99=new JMenuItem("字体")

jm3.add(jmi9)

jmb.add(jm1)

jmb.add(jm2)

jmb.add(jm3)

jmb.add(jm4)

jpm=new JPopupMenu()

jpm.add(jmi33)

jpm.add(jmi99)

jpm.add(jmi44)

jta=new JTextArea()

jta.setBorder(new TitledBorder("文本域"))

//添加鼠标适配器

jta.addMouseListener(new MouseAdapter(){

public void mouseReleased(MouseEvent e){

if(e.getButton()==MouseEvent.BUTTON3){

System.out.println("ff")

jpm.show(e.getComponent(), e.getX(), e.getY())

}

}

})

jsp=new JScrollPane(jta,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED)

jfc=new JFileChooser()

//添加文件过滤器

jfc.addChoosableFileFilter(new MyFileFilter("java"))

jfc.addChoosableFileFilter(new MyFileFilter("txt"))

jfc.addChoosableFileFilter(new MyFileFilter("c"))

jfc.addChoosableFileFilter(new MyFileFilter("cpp"))

jfc.addChoosableFileFilter(new MyFileFilter("lrc"))

jfc.addChoosableFileFilter(new MyFileFilter("h"))

//注册监听器

jmi1.addActionListener(this)

jmi2.addActionListener(this)

jmi3.addActionListener(this)

jmi33.addActionListener(this)

jmi4.addActionListener(this)

jmi44.addActionListener(this)

jmi5.addActionListener(this)

jmi6.addActionListener(this)

jmi7.addActionListener(this)

jmi8.addActionListener(this)

jmi9.addActionListener(this)

jmi99.addActionListener(this)

//添加菜单栏和文本域

this.setJMenuBar(jmb)

this.add(jsp)

}

//主函数

public static void main(String[] args) {

// TODO Auto-generated method stub

//设置当前程序的UI

try {

UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName())

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace()

} catch (InstantiationException e) {

// TODO Auto-generated catch block

e.printStackTrace()

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace()

} catch (UnsupportedLookAndFeelException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

NotePadWin notepad=new NotePadWin()

notepad.setSize(400,300)

notepad.setTitle("我的记事本")

notepad.setVisible(true)

notepad.setDefaultCloseOperation(EXIT_ON_CLOSE)

}

//事件处理函数

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

JMenuItem t=(JMenuItem)e.getSource()

//处理新建

if(t==jmi1){

newFile()

}

//打开

if(t==jmi2){

openFile()

}

//保存文件

if(t==jmi3||t==jmi33){

saveFile()

}

//退出

if(t==jmi4||t==jmi44){

myExit()

}

//帮助

if(t==jmi7){

JOptionPane.showMessageDialog(null, "这个记事本可以打开基本的文本文件\n"

+"包括*.java,*.class,*.cpp,*.h,*.lrc,*.txt\n"

+"为了避免与系统热键冲突对于全选.复制.粘贴等没有实现")

}

//关于

if(t==jmi8){

JOptionPane.showMessageDialog(null, "一个简单的记事本\n"

+"仅供学习研究之用\n"+"作者:高兴亮 \n"+"2010-11-30")

}

//字体

if(t==jmi9||t==jmi99){

fontSet()

}

}

public void saveFile(){

File file=null

int flag=jfc.showSaveDialog(this)

jfc.setApproveButtonText("保存")

jfc.setDialogTitle("保存文件.....")

FileOutputStream fos=null

if(flag==JFileChooser.APPROVE_OPTION){

file=jfc.getSelectedFile()

try {

fos=new FileOutputStream(file)

try {

fos.write(jta.getText().getBytes())

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace()

}

} catch (FileNotFoundException e1) {

// TODO Auto-generated catch block

e1.printStackTrace()

}finally{

if(fos!=null){

try {

fos.close()

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace()

}

}

}

}

}

public void openFile(){

String str=null

File file=null

jfc.setApproveButtonText("确定")

jfc.setDialogTitle("打开文件....")

FileInputStream fis=null

//打开窗口.返回值

int flag=jfc.showOpenDialog(this)

//打开文件

if(flag==JFileChooser.APPROVE_OPTION){

file=jfc.getSelectedFile()

String name=file.getName().substring(file.getName().lastIndexOf('.')+1).toLowerCase()

//约束打开文件类型

if(name.equals("java")||name.equals("txt")||name.equals("h")

||name.equals("lrc")||name.equals("cpp")||name.equals("class")){

//得到文件内容

try {

fis=new FileInputStream(file)

byte buf[]=new byte[1024]

int n=0

try {

while((n=fis.read(buf))!=-1){

str+=new String(buf,0,n)

}

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace()

}

jta.setText(str)

} catch (FileNotFoundException e1) {

// TODO Auto-generated catch block

e1.printStackTrace()

}finally{

if(fis!=null){

try {

fis.close()

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace()

}

}

}

}

}else{

//test

System.out.println("未打开文件")

}

}

public void fontSet(){

fw=new FontWin()

}

public void myExit(){

//文本域有内容

if(jta.getText().length()!=0){

int ii=JOptionPane.showConfirmDialog(null, "是否保存文件")

//用户选择是

if(ii==0){

saveFile()

System.exit(0)

}else if(ii==1){

System.exit(0)

}else{

System.exit(0)

}

}else{

System.exit(0)

}

}

public void newFile(){

//文本域有内容

if(jta.getText().length()!=0){

int ii=JOptionPane.showConfirmDialog(null, "是否保存文件")

//用户选择是

if(ii==0){

saveFile()

}else if(ii==1){

jta.setText("")

}else{

System.out.println("error")

}

}

}

}

//我的文件过滤器实现对打开文件的限制

class MyFileFilter extends FileFilter{

//文件后缀

String str

public MyFileFilter(String str) {

super()

this.str = str

}

//当目录里面的文件与要求的相同时返回true,并将该文件显示在文件对话框中

@Override

public boolean accept(File file) {

// TODO Auto-generated method stub

//如果文件是目录

if(file.isDirectory()){

return true

}

String fileName=file.getName()

if(fileName.substring(fileName.lastIndexOf('.')+1).equalsIgnoreCase(str)){

return true

}

return false

}

//返回描述文件对应的描述

public String getDescription(){

String des=new String()

if(str.equalsIgnoreCase("java")){

des="java file"

}else if(str.equalsIgnoreCase("class")){

des="java class file"

}else if(str.equalsIgnoreCase("txt")){

des="text file"

}else if(str.equalsIgnoreCase("cpp")){

des="c++ file"

}else if(str.equalsIgnoreCase("h")){

des="c/c++ header file"

}else if(str.equalsIgnoreCase("lrc")){

des="lrc file"

}else

des=""

return des

}

}

//字体控制窗口

class FontWin extends JFrame implements ActionListener,ChangeListener{

//字体种类

JComboBox jcb=null

//颜色(文本域的颜色)

JColorChooser jcc=null

//字体大小

JSlider js=null

//示例

JTextArea jta=null

JScrollPane jsp=null

//控制按钮

JButton jbt1=null

JButton jbt2=null

JButton jbt3=null

String[] fonts=null

//定义此次 *** 作是否有效

Font curFont=null

Color color=new Color(0)

public FontWin(){

//初始化字体种类

GraphicsEnvironment e=GraphicsEnvironment.getLocalGraphicsEnvironment()

fonts=e.getAvailableFontFamilyNames()

jcb=new JComboBox(fonts)

jcb.addActionListener(this)

//初始化Jcc

//jcc=new JColorChooser(Color.white)

//color=jcc.showDialog(jf, "颜色选择器", Color.white)

jbt1=new JButton("确定")

jbt2=new JButton("取消")

jbt3=new JButton("颜色选择器")

jbt1.addActionListener(this)

jbt2.addActionListener(this)

jbt3.addActionListener(this)

jta=new JTextArea(30,30)

jta.setText("我的记事本\n"+"my notepad")

jta.setEditable(true)

jta.setBorder(new TitledBorder("示例"))

jsp=new JScrollPane(jta,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED)

//字体大小在10-50

js=new JSlider(10,50)

js.setMajorTickSpacing(10)

js.setMinorTickSpacing(1)

js.setPaintTicks(true)

js.setPaintLabels(true)

//滑到整数附近

js.setSnapToTicks(true)

js.addChangeListener(this)

JPanel jpa1=new JPanel()

jpa1.add(jcb)

jpa1.add(js)

JPanel jpa2=new JPanel()

jpa2.add(jbt3)

jpa2.add(jbt1)

jpa2.add(jbt2)

JPanel jpa3=new JPanel()

jpa3.add(jsp,BorderLayout.CENTER)

this.add(jpa1,BorderLayout.NORTH)

this.add(jpa3,BorderLayout.CENTER)

this.add(jpa2,BorderLayout.SOUTH)

this.setSize(400, 200)

this.setLocation(200, 200)

this.setVisible(true)

this.setResizable(false)

this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)

this.setTitle("字体选择器")

}

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(e.getSource()==jcb){

String curFontName=(String)jcb.getSelectedItem()

int curFontSize=js.getValue()

Font f=new Font(curFontName,Font.PLAIN,curFontSize)

jta.setFont(f)

}

//颜色选择器

if(e.getSource()==jbt3){

jcc=new JColorChooser(Color.white)

color=JColorChooser.showDialog(this, "颜色选择器", Color.white)

jta.setBackground(color)

}

if(e.getSource()==jbt1){

String curFontName=(String)jcb.getSelectedItem()

int curFontSize=js.getValue()

curFont=new Font(curFontName,Font.PLAIN,curFontSize)

//color=color

NotePadWin.jta.setFont(curFont)

NotePadWin.jta.setBackground(color)

this.dispose()

}

if(e.getSource()==jbt2){

this.dispose()

}

}

//js

@Override

public void stateChanged(ChangeEvent e) {

// TODO Auto-generated method stub

if(e.getSource()==js){

String curFontName=(String)jcb.getSelectedItem()

int curFontSize=js.getValue()

Font f=new Font(curFontName,Font.PLAIN,curFontSize)

jta.setFont(f)

}

}

public Font getCurFont() {

return curFont

}

public Color getColor() {

return color

}

}

楼主写一个html,很容易把下面代码嵌入到applet,可以google一下实现,

还有copy自己不知道算不算复制。。。-_-!

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

楼主给你一个我的,直接保存成pb.java编译运行,就是你要的画图功能 ,可以参考一下

____________________________________________________________________

import java.applet.*

import java.awt.*

import java.awt.event.*

import java.util.*

import javax.swing.*

import java.awt.geom.*

import java.io.*

class Point implements Serializable

{

int x,y

Color col

int tool

int boarder

Point(int x, int y, Color col, int tool, int boarder)

{

this.x = x

this.y = y

this.col = col

this.tool = tool

this.boarder = boarder

}

}

class paintboard extends Frame implements ActionListener,MouseMotionListener,MouseListener,ItemListener

{

int x = -1, y = -1

int con = 1//画笔大小

int Econ = 5//橡皮大小

int toolFlag = 0//toolFlag:工具标记

//toolFlag工具对应表:

//(0--画笔);(1--橡皮);(2--清除);

//(3--直线);(4--圆);(5--矩形);

Color c = new Color(0,0,0)//画笔颜色

BasicStroke size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL)//画笔粗细

Point cutflag = new Point(-1, -1, c, 6, con)//截断标志

Vector paintInfo = null//点信息向量组

int n = 1

FileInputStream picIn = null

FileOutputStream picOut = null

ObjectInputStream VIn = null

ObjectOutputStream VOut = null

// *工具面板--画笔,直线,圆,矩形,多边形,橡皮,清除*/

Panel toolPanel

Button eraser, drLine,drCircle,drRect

Button clear ,pen

Choice ColChoice,SizeChoice,EraserChoice

Button colchooser

Label 颜色,大小B,大小E

//保存功能

Button openPic,savePic

FileDialog openPicture,savePicture

paintboard(String s)

{

super(s)

addMouseMotionListener(this)

addMouseListener(this)

paintInfo = new Vector()

/*各工具按钮及选择项*/

//颜色选择

ColChoice = new Choice()

ColChoice.add("black")

ColChoice.add("red")

ColChoice.add("blue")

ColChoice.add("green")

ColChoice.addItemListener(this)

//画笔大小选择

SizeChoice = new Choice()

SizeChoice.add("1")

SizeChoice.add("3")

SizeChoice.add("5")

SizeChoice.add("7")

SizeChoice.add("9")

SizeChoice.addItemListener(this)

//橡皮大小选择

EraserChoice = new Choice()

EraserChoice.add("5")

EraserChoice.add("9")

EraserChoice.add("13")

EraserChoice.add("17")

EraserChoice.addItemListener(this)

////////////////////////////////////////////////////

toolPanel = new Panel()

clear = new Button("清除")

eraser = new Button("橡皮")

pen = new Button("画笔")

drLine = new Button("画直线")

drCircle = new Button("画圆形")

drRect = new Button("画矩形")

openPic = new Button("打开图画")

savePic = new Button("保存图画")

colchooser = new Button("显示调色板")

//各组件事件监听

clear.addActionListener(this)

eraser.addActionListener(this)

pen.addActionListener(this)

drLine.addActionListener(this)

drCircle.addActionListener(this)

drRect.addActionListener(this)

openPic.addActionListener(this)

savePic.addActionListener(this)

colchooser.addActionListener(this)

颜色 = new Label("画笔颜色",Label.CENTER)

大小B = new Label("画笔大小",Label.CENTER)

大小E = new Label("橡皮大小",Label.CENTER)

//面板添加组件

toolPanel.add(openPic)

toolPanel.add(savePic)

toolPanel.add(pen)

toolPanel.add(drLine)

toolPanel.add(drCircle)

toolPanel.add(drRect)

toolPanel.add(颜色)toolPanel.add(ColChoice)

toolPanel.add(大小B)toolPanel.add(SizeChoice)

toolPanel.add(colchooser)

toolPanel.add(eraser)

toolPanel.add(大小E)toolPanel.add(EraserChoice)

toolPanel.add(clear)

//工具面板到APPLET面板

add(toolPanel,BorderLayout.NORTH)

setBounds(60,60,900,600)setVisible(true)

validate()

//dialog for save and load

openPicture = new FileDialog(this,"打开图画",FileDialog.LOAD)

openPicture.setVisible(false)

savePicture = new FileDialog(this,"保存图画",FileDialog.SAVE)

savePicture.setVisible(false)

openPicture.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{ openPicture.setVisible(false)}

})

savePicture.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{ savePicture.setVisible(false)}

})

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{ System.exit(0)}

})

}

public void paint(Graphics g)

{

Graphics2D g2d = (Graphics2D)g

Point p1,p2

n = paintInfo.size()

if(toolFlag==2)

g.clearRect(0,0,getSize().width,getSize().height)//清除

for(int i=0i<n i++){

p1 = (Point)paintInfo.elementAt(i)

p2 = (Point)paintInfo.elementAt(i+1)

size = new BasicStroke(p1.boarder,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL)

g2d.setColor(p1.col)

g2d.setStroke(size)

if(p1.tool==p2.tool)

{

switch(p1.tool)

{

case 0://画笔

Line2D line1 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y)

g2d.draw(line1)

break

case 1://橡皮

g.clearRect(p1.x, p1.y, p1.boarder, p1.boarder)

break

case 3://画直线

Line2D line2 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y)

g2d.draw(line2)

break

case 4://画圆

Ellipse2D ellipse = new Ellipse2D.Double(p1.x, p1.y, Math.abs(p2.x-p1.x) , Math.abs(p2.y-p1.y))

g2d.draw(ellipse)

break

case 5://画矩形

Rectangle2D rect = new Rectangle2D.Double(p1.x, p1.y, Math.abs(p2.x-p1.x) , Math.abs(p2.y-p1.y))

g2d.draw(rect)

break

case 6://截断,跳过

i=i+1

break

default :

}//end switch

}//end if

}//end for

}

public void itemStateChanged(ItemEvent e)

{

if(e.getSource()==ColChoice)//预选颜色

{

String name = ColChoice.getSelectedItem()

if(name=="black")

{c = new Color(0,0,0)}

else if(name=="red")

{c = new Color(255,0,0)}

else if(name=="green")

{c = new Color(0,255,0)}

else if(name=="blue")

{c = new Color(0,0,255)}

}

else if(e.getSource()==SizeChoice)//画笔大小

{

String selected = SizeChoice.getSelectedItem()

if(selected=="1")

{

con = 1

size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL)

}

else if(selected=="3")

{

con = 3

size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL)

}

else if(selected=="5")

{con = 5

size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL)

}

else if(selected=="7")

{con = 7

size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL)

}

else if(selected=="9")

{con = 9

size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL)

}

}

else if(e.getSource()==EraserChoice)//橡皮大小

{

String Esize = EraserChoice.getSelectedItem()

if(Esize=="5")

{ Econ = 5*2}

else if(Esize=="9")

{ Econ = 9*2}

else if(Esize=="13")

{ Econ = 13*2}

else if(Esize=="17")

{ Econ = 17*3}

}

}

public void mouseDragged(MouseEvent e)

{

Point p1

switch(toolFlag){

case 0://画笔

x = (int)e.getX()

y = (int)e.getY()

p1 = new Point(x, y, c, toolFlag, con)

paintInfo.addElement(p1)

repaint()

break

case 1://橡皮

x = (int)e.getX()

y = (int)e.getY()

p1 = new Point(x, y, null, toolFlag, Econ)

paintInfo.addElement(p1)

repaint()

break

default :

}

}

public void mouseMoved(MouseEvent e) {}

public void update(Graphics g)

{

paint(g)

}

public void mousePressed(MouseEvent e)

{

Point p2

switch(toolFlag){

case 3://直线

x = (int)e.getX()

y = (int)e.getY()

p2 = new Point(x, y, c, toolFlag, con)

paintInfo.addElement(p2)

break

case 4: //圆

x = (int)e.getX()

y = (int)e.getY()

p2 = new Point(x, y, c, toolFlag, con)

paintInfo.addElement(p2)

break

case 5: //矩形

x = (int)e.getX()

y = (int)e.getY()

p2 = new Point(x, y, c, toolFlag, con)

paintInfo.addElement(p2)

break

default :

}

}

public void mouseReleased(MouseEvent e)

{

Point p3

switch(toolFlag){

case 0://画笔

paintInfo.addElement(cutflag)

break

case 1: //eraser

paintInfo.addElement(cutflag)

break

case 3://直线

x = (int)e.getX()

y = (int)e.getY()

p3 = new Point(x, y, c, toolFlag, con)

paintInfo.addElement(p3)

paintInfo.addElement(cutflag)

repaint()

break

case 4: //圆

x = (int)e.getX()

y = (int)e.getY()

p3 = new Point(x, y, c, toolFlag, con)

paintInfo.addElement(p3)

paintInfo.addElement(cutflag)

repaint()

break

case 5: //矩形

x = (int)e.getX()

y = (int)e.getY()

p3 = new Point(x, y, c, toolFlag, con)

paintInfo.addElement(p3)

paintInfo.addElement(cutflag)

repaint()

break

default:

}

}

public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

public void mouseClicked(MouseEvent e){}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==pen)//画笔

{toolFlag = 0}

if(e.getSource()==eraser)//橡皮

{toolFlag = 1}

if(e.getSource()==clear)//清除

{

toolFlag = 2

paintInfo.removeAllElements()

repaint()

}

if(e.getSource()==drLine)//画线

{toolFlag = 3}

if(e.getSource()==drCircle)//画圆

{toolFlag = 4}

if(e.getSource()==drRect)//画矩形

{toolFlag = 5}

if(e.getSource()==colchooser)//调色板

{

Color newColor = JColorChooser.showDialog(this,"调色板",c)

c = newColor

}

if(e.getSource()==openPic)//打开图画

{

openPicture.setVisible(true)

if(openPicture.getFile()!=null)

{

int tempflag

tempflag = toolFlag

toolFlag = 2

repaint()

try{

paintInfo.removeAllElements()

File filein = new File(openPicture.getDirectory(),openPicture.getFile())

picIn = new FileInputStream(filein)

VIn = new ObjectInputStream(picIn)

paintInfo = (Vector)VIn.readObject()

VIn.close()

repaint()

toolFlag = tempflag

}

catch(ClassNotFoundException IOe2)

{

repaint()

toolFlag = tempflag

System.out.println("can not read object")

}

catch(IOException IOe)

{

repaint()

toolFlag = tempflag

System.out.println("can not read file")

}

}

}

if(e.getSource()==savePic)//保存图画

{

savePicture.setVisible(true)

try{

File fileout = new File(savePicture.getDirectory(),savePicture.getFile())

picOut = new FileOutputStream(fileout)

VOut = new ObjectOutputStream(picOut)

VOut.writeObject(paintInfo)

VOut.close()

}

catch(IOException IOe)

{

System.out.println("can not write object")

}

}

}

}//end paintboard

public class pb

{

public static void main(String args[])

{ new paintboard("画图程序")}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11875570.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存