菜单,监听器事件,适配器事件的使用

[size=medium][color=blue][color=blue]import java.awt.FileDialog;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;

public class EditText {
static String filepath;
static JFrame frame = new JFrame("文本编辑器");
static TextArea tf = new TextArea("",30,40,TextArea.SCROLLBARS_BOTH);
//TextArea(String text, int rows,int columns, int scrollbars)scrollbars
//参数值提供的常量: SCROLLBARS_BOTH, SCROLLBARS_VERTICAL_ONLY, SCROLLBARS_HORIZONTAL_ONLY, SCROLLBARS_NONE.


static JMenuBar menubar=new JMenuBar();

static JMenu menuFile=new JMenu("文件");
JMenu menuEdit=new JMenu("编辑");
JMenu menuLook=new JMenu("查看");
JMenu menuTool=new JMenu("工具");
JMenu menuDoc=new JMenu("文档");

static JMenuItem newFile=new JMenuItem("新建 Ctrl+N");
static JMenuItem Open=new JMenuItem("打开 Ctrl+O");
JMenuItem Save=new JMenuItem("保存 Ctrl+S");
JMenuItem Lsave=new JMenuItem("另存为 Shift+Ctrl+S");

JMenuItem Cut=new JMenuItem("剪切 Ctrl+X");
JMenuItem Copy=new JMenuItem("复制 Ctrl+C");
JMenuItem Paste=new JMenuItem("粘贴 Ctrl+V");
JMenuItem delete=new JMenuItem("删除 Ctrl+D");

JMenuItem ToolItem=new JMenuItem("工具栏 ");
JMenuItem zt=new JMenuItem("状态栏 ");
JMenuItem cb=new JMenuItem("测边栏 ");
JMenuItem tc=new JMenuItem("突出显示模式 ");

JMenuItem Doctongji=new JMenuItem("文档统计 ");

JMenuItem Docsave=new JMenuItem("文档保存 ");
public EditText(){
frame.setSize(600,500);
frame.setVisible(true);
frame.add(tf);
frame.setJMenuBar(menubar);

menubar.add(menuFile);
menubar.add(menuEdit);
menubar.add(menuLook);
menubar.add(menuTool);
menubar.add(menuDoc);

menuFile.add(newFile);
menuFile.add(Open);
menuFile.add(Save);
menuFile.add(Lsave);

menuEdit.add(Cut);
menuEdit.add(Copy);
menuEdit.add(Paste);
menuEdit.add(delete);

menuLook.add(ToolItem);
menuLook.add(zt);
menuLook.add(cb);
menuLook.add(tc);

menuTool.add(Doctongji);
menuDoc.add(Docsave);
}

public static void main(String[] args) {
Open.addActionListener(new ActionListener(){//打开一个文件

public void actionPerformed(ActionEvent e) {
BufferedReader br=null;
FileDialog dialog=new FileDialog(new JFrame(),"打开 指定文件 ",FileDialog.LOAD);
dialog.setVisible(true);
filepath=dialog.getDirectory()+dialog.getFile();
File file=new File(filepath);
try {
br=new BufferedReader(new FileReader((file)));
String content;
StringBuffer sb=new StringBuffer();
while((content=br.readLine())!=null){
sb.append(content+"\n");
}
tf.setText(sb.toString());
} catch (Exception e1) {
e1.printStackTrace();
}
finally{
try {
if(br!=null){
br.close(); }

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}});

new EditText();

}

}


//鼠标事件:
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;

public class MouseTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame("窗口关闭");
frame.setSize(300, 200);
frame.setVisible(true);
frame.addMouseListener(new MouseListener(){

public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
int x=e.getX();
int y=e.getY();
System.out.println("x坐标"+x);
System.out.println("y坐标"+y);
}

public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}

public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}

} );
}

}


import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class MouseTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame("窗口关闭");
frame.setSize(300, 200);
frame.setVisible(true);

final JTextField tf = new JTextField();
frame.add(tf,BorderLayout.NORTH);
frame.pack();
frame.addMouseMotionListener(new MouseMotionListener(){
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
int x=e.getX();
int y=e.getY();
tf.setText("x="+x+" "+"y="+y);
}

public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub

}


} );
}

}


import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;


public class KeyTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame("窗口关闭");
frame.setSize(300, 200);
frame.setVisible(true);
frame.addKeyListener(new KeyListener(){

public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("按下键盘");
}

public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println( "keyReleased(");
}

public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("keyTyped");
}

} );
}

}


import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class MouseTest extends MouseMotionAdapter{
static JFrame frame = new JFrame("窗口关闭");

static JTextField tf = new JTextField();

public MouseTest() {
frame.setSize(300, 200);
frame.setVisible(true);
frame.add(tf, BorderLayout.NORTH);
frame.addMouseMotionListener(this);
frame.pack();
}

public static void main(String[] args) {
// TODO Auto-generated method stub
new MouseTest();
}

public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
int x = e.getX();
int y = e.getY();
tf.setText("x=" + x + " " + "y=" + y);
}

}[/color][/color][/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值