[java]代码库
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.WindowConstants;
public class NotePadTest extends JFrame {
private static final long serialVersionUID = 7733222986650734523L;
JMenuBar jmb = new JMenuBar();
// 创建子菜单
JMenu jm1 = new JMenu("文件");
JMenu jm2 = new JMenu("编辑");
JMenu jm3 = new JMenu("格式");
JMenu jm5 = new JMenu("帮助");
// 创建菜单项(文件)
JMenuItem jme1 = new JMenuItem("新建");
JMenuItem jme2 = new JMenuItem("打开");
JMenuItem jme3 = new JMenuItem("保存");
JMenuItem jme4 = new JMenuItem("另存为...");
JMenuItem jme5 = new JMenuItem("退出");
// 创建菜单项(编辑)
JMenuItem jme6 = new JMenuItem("全选");
JMenuItem jme7 = new JMenuItem("剪切");
JMenuItem jme8 = new JMenuItem("复制");
JMenuItem jme9 = new JMenuItem("粘贴");
JMenuItem jme10 = new JMenuItem("删除");
// 创建菜单项(格式)
JCheckBoxMenuItem jme11 = new JCheckBoxMenuItem("自动换行", false);
// 创建菜单项(帮助)
JMenuItem jme14 = new JMenuItem("查看帮助");
JMenuItem jme15 = new JMenuItem("关于记事本");
// 实例化文本域
JTextArea jtext = new JTextArea();
JTextPane pane = new JTextPane();// 文本窗格
JScrollPane jscroll = new JScrollPane(jtext);
JTextPane jtshow = new JTextPane();
public NotePadTest() {
setTitle("记事本");
setSize(500, 460);
setLocationRelativeTo(null);
pane.setLayout(new BorderLayout());
// 设置快捷键
jme1.setMnemonic('N');
jme1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.CTRL_MASK));
jme2.setMnemonic('O');
jme2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_MASK));
jme3.setMnemonic('S');
jme3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK));
jme6.setMnemonic('A');
jme6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK));
jme7.setMnemonic('X');
jme7.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_MASK));
jme8.setMnemonic('C');
jme8.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK));
jme9.setMnemonic('V');
jme9.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.CTRL_MASK));
jme10.setMnemonic('L');
jme10.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, KeyEvent.CTRL_MASK));
// 添加菜单项到子菜单(文件)
jm1.add(jme1);
jm1.addSeparator();
jm1.add(jme2);
jm1.addSeparator();
jm1.add(jme3);
jm1.addSeparator();
jm1.add(jme4);
jm1.addSeparator();
jm1.add(jme5);
jmb.add(jm1);
// 添加菜单项到子菜单(编辑)
jm2.add(jme6);
jm2.addSeparator();
jm2.add(jme7);
jm2.addSeparator();
jm2.add(jme8);
jm2.addSeparator();
jm2.add(jme9);
jm2.addSeparator();
jm2.add(jme10);
jmb.add(jm2);
// 添加菜单项到子菜单(格式)
// 设置快捷键
jme11.setMnemonic('w');
jme11.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, KeyEvent.CTRL_MASK));
jm3.add(jme11);
jmb.add(jm3);
// 添加菜单项到子菜单(帮助)
jm5.add(jme14);
jm5.addSeparator();
jm5.add(jme15);
jmb.add(jm5);
// 将菜单栏添加到窗体顶部
setJMenuBar(jmb);
// container.add(jmb,BorderLayout.NORTH);
// 添加文本域
pane.add(jscroll, BorderLayout.CENTER);
add(pane);
// **************************添加事件监听器(全选部分)*******************************
jme6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jtext.selectAll();// 全选
}
});
// **************************添加事件监听器(剪切部分)*******************************
jme7.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jtext.cut();// 剪切
}
});
// **************************添加事件监听器(复制部分)*******************************
jme8.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jtext.copy();// 复制
}
});
// **************************添加事件监听器(粘贴部分)*******************************
jme9.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jtext.paste();// 粘贴
}
});
// **************************添加事件监听器(删除部分)*******************************
jme10.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jtext.replaceSelection(null);// 删除
}
});
// **************************添加事件监听器(新建部分)********************************
jme1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (jtext.getText().equals("")) {
jtext.setText("");
} else {
int result = JOptionPane.showConfirmDialog(NotePadTest.this, "是否将更改保存到无标题?", "记事本",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if (result == 0) {
save();
}
}
}
});
// **************************添加事件监听器(打开部分)********************************
jme2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser choose = new JFileChooser();
choose.setDialogTitle("打开");
File file;
int result = choose.showOpenDialog(NotePadTest.this);
if (result == JFileChooser.APPROVE_OPTION) {
try { // 获取当前选择的文件
// 创建文件输出流
file = choose.getSelectedFile();
BufferedReader br = new BufferedReader(new FileReader(file));
String str = null;
while ((str = br.readLine()) != null) {
jtext.append(str + "\r\n");
}
// 设置记事本标题
setTitle(file.getName() + " 记事本");
br.close();
} catch (Exception ee) {
ee.printStackTrace();
}
}
}
});
// **************************添加事件监听器(保存部分)********************************
jme3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (jtext.getText() != null && !jtext.getText().equals("")) {
save();
}
}
});
// **************************添加事件监听器(另存为部分)********************************
jme4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (jtext.getText() != null && !jtext.getText().equals("")) {
save();
}
}
});
// **************************添加事件监听器(退出部分)********************************
jme5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (jtext.getText().equals("")) {
NotePadTest.this.dispose();
} else {
int result = JOptionPane.showConfirmDialog(NotePadTest.this, "是否将更改保存到无标题?", "记事本",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if (result == 0) {
if (jtext.getText() != null && !jtext.getText().equals("")) {
save();
}
} else if (result == 1) {
NotePadTest.this.dispose();
}
}
}
});
// **************************添加事件监听器(自动换行部分)********************************
jme11.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (jme11.isSelected()) {
jtext.setLineWrap(true);
} else {
jtext.setLineWrap(false);
}
}
});
// **************************添加事件监听器(查看帮助部分)***********************************
jme14.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(NotePadTest.this, "输入文字即可!");
}
});
// **************************添加事件监听器(关于记事本部分)***********************************
jme15.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(NotePadTest.this, "版本:Windows旗舰版");
}
});
// 设置窗体关闭
if (jtext.getText().equals("")) {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} else if (jtext.getText() != null) {
int result = JOptionPane.showConfirmDialog(NotePadTest.this, "是否将更改保存到无标题?", "记事本",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if (result == 0) {
} else if (result == 1) {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
// 设置窗体是否可见
setVisible(true);
}
public void save() {
JFileChooser choose = new JFileChooser();
int result = choose.showSaveDialog(NotePadTest.this);// NotePad.this父容器
if (result == JFileChooser.APPROVE_OPTION) {
File file;
try {
file = choose.getSelectedFile();
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(jtext.getText());
bw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public static void main(String[] args) {
new NotePadTest();
}
}
中级程序员
by: 东城 发表于:2016-05-21 22:19:29 顶(1) | 踩(0) 回复
回复评论