📄 新建 文本文档.txt
字号:
import java.awt.image.*;
import java.awt.Frame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.applet.*;
//============================================================================== 主函数
public class EditorTh
{
public static void main(String args[])
{
Myframe mf=new Myframe(); //Myframe是自己设计的继承 Frame 的类
mf.setSize(new Dimension(800,600)); //设定显示的范围
mf.setVisible(true); //设定 自定义的 Myframe为可视
}
}
//============================================================================
class Myframe extends Frame implements ActionListener,MouseListener,MouseMotionListener //继承 几个屏幕响应的接口
{
private Label label1 = new Label("选择图形"); //屏幕的元素
private Choice choice1 = new Choice();
private Checkbox checkbox1 = new Checkbox();
private Button button1 = new Button("选择颜色");
private Button button2 = new Button("清除");
private Button button3 = new Button("文本操作");
FileDialog fd; //对话框选项,下面用来显示文本编辑器
Dialog d;
Image img; // 构造一个 image类用来引入图形
Graphics g; //构造一个 Graphics类对象 g用来画图
Point p=new Point(40,100); //点类,用来定位坐标
Myframe() // Myframe的构造函数
{
super("图形编辑器"); //标题
setLayout(new FlowLayout() ); //定义默认的 flowlayout
choice1.setForeground(Color.blue); //选择画点的框中的前景色为蓝
checkbox1.setLabel("填充");
//--------------------------------------------------------------------------=
img=getToolkit().getImage("fox.gif"); // 用 getToolkit().getImage( )方法得到本地硬盘的一个图象
setBackground(Color.white); // myframe的默认的 背景色
choice1.add("点");
choice1.add("直线");
choice1.add("曲线");
choice1.add("矩形");
choice1.add("圆");
button1.addActionListener(this); //注册监听
button2.addActionListener(this);
button3.addActionListener(this);
//button6.addActionListener(this);
addMouseListener(this);
addMouseMotionListener(this);
addWindowListener(new closeWin()); //注意,这是关闭窗口的函数
add(label1); //注册 myframe的元素对象
add(choice1);
add(checkbox1);
add(button1);
add(button3);
// add(button6);
add(button2);
}
//---------------------------------下面的类建立一个非模式的对话框,用来编辑文本
public class GroupD extends Dialog implements ActionListener //extends Dialog----说明是Dialog 的一个继承
{
TextArea t;
Button saveT;
Button openD;
Button quitD;
GroupD(Frame frame)
{
super(frame,"您正在进行文本的操作,您 的文本将会被保存在当前目录下text子目录的test.txt里",
false); //建立一个非模式的对话框
setLayout(new FlowLayout());
setResizable(true); //这个对话框可以缩放大小
saveT = new Button("save");
openD = new Button("open");
t = new TextArea(30,100);
add(saveT);
add(openD);
add(t);
addWindowListener(new closeDialog()); //注意,关闭对话框的函数,closeDialog()说明引发关闭窗口的是对话框
saveT.addActionListener(this);
openD.addActionListener(this);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) //重载了的 消息响应函数
{
if(e.getSource() == saveT) //点击保存文件按钮时候的操作
{
File myfile =new File("text","test.txt"); //构造一个文件对象,为默认的 txt类型写入
try{
FileOutputStream fout =new FileOutputStream(myfile); //输出流
DataOutputStream dout = new DataOutputStream(fout);
dout.write(t.getText().getBytes()); //以字节写入
dout.close(); //关闭输入流
}catch(IOException ed)
{
setVisible(false);
}
}
if(e.getSource() == openD) //点击打开文件按钮时候的操作
{
try{
File mf = new File ( "text","test.txt");
RandomAccessFile raf = new RandomAccessFile(mf,"r");
while(raf.getFilePointer() < raf.length())
{
t.append(raf.readLine() + "\n"); //在文件尾追加
}
}catch (IOException ioe)
{
System.err.println(ioe.toString());
}
}
}
}
//--------------------------------------------------------------------------------------------------对话框类完毕
Color c=new Color(0,0,0); //构造一个 color类
Point p1,p2;
Point prep1;
public void paint1(Graphics g) //重新写系统的 paint 函数
{
g.drawImage(img,p.x,p.y,this); //画图
}
public void mousePressed(MouseEvent em) //下面是一系列的鼠标感应函数
{
p1=new Point(em.getPoint());
p2=new Point(em.getPoint());
}
public void mouseMoved(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
// g=this.getGraphics();
p.x=e.getX();p.y=e.getY();
String s1=String.valueOf(p.x);
String s2=String.valueOf(p.y);
super.setTitle("画图板 ");// 指定单击鼠标时标题栏的显示
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseDragged(MouseEvent ed) //拖动鼠标时候,画各种的图元,(不包括多边形)
{
g=this.getGraphics();
g.setColor(this.getBackground());
if(!checkbox1.getState()){
switch(choice1.getSelectedIndex()){
case 0: //g.drawRect(p1.x,p1.y,Math.abs(p2.x-p1.x),Math.abs(p2.y-p1.y));
g.setColor(c);
g.drawLine(p1.x,p1.y,p1.x+1,p1.y+1);
break;
case 1: g.drawLine(p1.x,p1.y,p2.x,p2.y);
g.setColor(c);
g.drawLine(p1.x,p1.y,ed.getX(),ed.getY());
break;
case 2:
g.setColor(c);
g.drawLine(p2.x,p2.y,ed.getX(),ed.getY());
break;
case 3: g.drawRect(p1.x,p1.y,Math.abs(p2.x-p1.x),Math.abs(p2.y-p1.y));
g.setColor(c);
g.drawRect(p1.x,p1.y,Math.abs(ed.getX()-p1.x),Math.abs(ed.getY()-p1.y));
break;
case 4: g.drawOval(p1.x,p1.y,Math.abs(p2.x-p1.x),Math.abs(p2.y-p1.y));
g.setColor(c);
g.drawOval(p1.x,p1.y,Math.abs(ed.getX()-p1.x),Math.abs(ed.getY()-p1.y));
break;
}
p2=ed.getPoint();
}
else
{
switch(choice1.getSelectedIndex()){ //选择了填充就用这个函数
case 0: //g.drawRect(p1.x,p1.y,Math.abs(p2.x-p1.x),Math.abs(p2.y-p1.y));
g.setColor(c);
g.drawLine(p1.x,p1.y,p1.x+1,p1.y+1);
break;
case 1: g.drawLine(p1.x,p1.y,p2.x,p2.y);
g.setColor(c);
g.drawLine(p1.x,p1.y,ed.getX(),ed.getY());
break;
case 2:
g.setColor(c);
g.drawLine(p2.x,p2.y,ed.getX(),ed.getY());
break;
case 3: g.fillRect(p1.x,p1.y,Math.abs(p2.x-p1.x),Math.abs(p2.y-p1.y));
g.setColor(c);
g.fillRect(p1.x,p1.y,Math.abs(ed.getX()-p1.x),Math.abs(ed.getY()-p1.y));
break;
case 4: g.fillOval(p1.x,p1.y,Math.abs(p2.x-p1.x),Math.abs(p2.y-p1.y));
g.setColor(c);
g.fillOval(p1.x,p1.y,Math.abs(ed.getX()-p1.x),Math.abs(ed.getY()-p1.y));
break;
}
p2=ed.getPoint();
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == button1){ // myframe 自身的消息处理函数
JColorChooser jColorChooser1 =new JColorChooser(); //选择颜色 的按纽
c=jColorChooser1.showDialog(this,"chose the color",button1.getForeground());
button1.setForeground(c);
}
if(e.getSource() == button2){ //清除 容器图象的按纽
g=this.getGraphics();
g.clearRect(0,10,1024,768);
}
if(e.getSource() == button3) //生成图形编辑器的按纽
{
d =new GroupD(this);
}
}
}
class closeWin extends WindowAdapter //-关闭窗口
{
public void windowClosing (WindowEvent e)
{
Frame frm=(Frame)(e.getSource());
frm.dispose();
System.exit(0);
}
}
class closeDialog extends WindowAdapter //关闭对话框的函数
{
public void windowClosing (WindowEvent e)
{
Dialog dlg=(Dialog)(e.getSource());
dlg.dispose();
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -