标签:scroll 文件 dstat mysq exception 退出 exists 接口 rac
package jishiben;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.*;
import javax.swing.undo.UndoManager;
public class Jishiben implements ActionListener{
JFrame frame;
JTextArea text;
JScrollPane sp;
JMenuItem newItem,windomItem,openItem,saveItem,othersaveItem,pagesetupItem,printItem,closeItem;
JMenuBar bar;
JMenu fileMenu,editMenu,formatMenu,checkMenu,helpMenu;
JFileChooser choose;
File file;
JTable table;
JMenuItem blankItem;
UndoManager undoManager;
public void Wenjian() {
frame=new JFrame("记事本");
text=new JTextArea();
sp=new JScrollPane(text);
bar=new JMenuBar();
fileMenu=new JMenu("文件(F)");
editMenu=new JMenu("编辑(E)");
formatMenu=new JMenu("格式(O)");
checkMenu=new JMenu("查看(V)");
helpMenu=new JMenu("帮助(H)");
//关于文件的选项
newItem=new JMenuItem("新建(N)");
windomItem=new JMenuItem("新窗口(W)");
openItem=new JMenuItem("打开(O)");
saveItem=new JMenuItem("保存(S)");
othersaveItem=new JMenuItem("另存为(A)");
pagesetupItem=new JMenuItem("页面设置(U)");
printItem=new JMenuItem("打印(P)");
closeItem=new JMenuItem("退出(X)");
//关于文件助记符的设置
newItem.setMnemonic('N');
windomItem.setMnemonic('W');
openItem.setMnemonic('O');
saveItem.setMnemonic('S');
othersaveItem.setMnemonic('A');
pagesetupItem.setMnemonic('U');
printItem.setMnemonic('P');
closeItem.setMnemonic('X');
//关于文件快捷键的设置
newItem.setAccelerator(KeyStroke.getKeyStroke('N',KeyEvent.CTRL_DOWN_MASK));
windomItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_DOWN_MASK|InputEvent.SHIFT_DOWN_MASK));
openItem.setAccelerator(KeyStroke.getKeyStroke('O',KeyEvent.CTRL_DOWN_MASK));
saveItem.setAccelerator(KeyStroke.getKeyStroke('S',KeyEvent.CTRL_DOWN_MASK));
othersaveItem.setAccelerator(KeyStroke.getKeyStroke('S',KeyEvent.CTRL_DOWN_MASK|KeyEvent.SHIFT_DOWN_MASK));
printItem.setAccelerator(KeyStroke.getKeyStroke('P',KeyEvent.CTRL_DOWN_MASK));
//关于文件菜单的动作监听
newItem.addActionListener(this);
windomItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
othersaveItem.addActionListener(this);
pagesetupItem.addActionListener(this);
printItem.addActionListener(this);
closeItem.addActionListener(this);
fileMenu.add(newItem);
fileMenu.add(windomItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(othersaveItem);
fileMenu.add(pagesetupItem);
fileMenu.add(closeItem);
//向菜单栏添加
bar.add(fileMenu);
bar.add(editMenu);
bar.add(formatMenu);
bar.add(checkMenu);
bar.add(helpMenu);
frame.add(sp);
frame.setJMenuBar(bar);
frame.setSize(400,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Jishiben shi=new Jishiben();
shi.Wenjian();
}
public void actionPerformed(ActionEvent e){
//关于文件下面选项的监听
if(e.getSource()==newItem) {
text.setText("");
}else if(e.getSource()==windomItem) {
Jishiben ben =new Jishiben();
ben.Wenjian();
}else if(e.getSource()==openItem) {
choose=new JFileChooser();
choose.showOpenDialog(null);
file=choose.getSelectedFile();
try {
FileInputStream input=new FileInputStream(file);
byte[] b=new byte[input.available()];
input.read(b);
text.append(new String(b));
input.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}else if(e.getSource()==othersaveItem) {
choose=new JFileChooser();
choose.showOpenDialog(null);
file=choose.getSelectedFile();
try {
if(!file.exists()) {
file.createNewFile();
}
FileOutputStream output=new FileOutputStream(file);
byte[] b=text.getText().getBytes();
output.write(b);
output.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}else if(e.getSource()==saveItem) {
choose=new JFileChooser();
choose.showOpenDialog(null);
file=choose.getSelectedFile();
try {
FileOutputStream output=new FileOutputStream(file);
byte[] b=text.getText().getBytes();
output.write(b);
output.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}else if(e.getSource()==pagesetupItem) {
}else if(e.getSource()==printItem) {
}else if(e.getSource()==closeItem) {
System.exit(1);
}
}
}
标签:scroll 文件 dstat mysq exception 退出 exists 接口 rac
原文地址:https://www.cnblogs.com/xia-unun/p/11960856.html