📄 05151858102.java
字号:
filename = null;
}
if (filename != null)
{
writeFile(filename);
this.updates();
s = textarea.getText();// 保存后内容为当前内容
}
else
{
filename = ss;
}
}
else if (EventS == exititem)
{// 退出文件
ifsave = this.tosave();
if (ifsave == 1)
{// 如果改变
saveasfiledialog.setVisible(true);
filename = saveasfiledialog.getDirectory() + saveasfiledialog.getFile();
// 没有选择文件
if ((savefiledialog.getDirectory() == null) && (savefiledialog.getFile() == null))
{
filename = null;
}
if (filename != null)
{
writeFile(filename);
System.exit(0);
}
}
else if (ifsave == -1)
{
System.exit(0);
}
else if (ifsave == 0)
{
}
}
else if (EventS == redoitem || EventS == predoitem)
{
undoManger.redo();
}
else if (EventS == undoitem || EventS == pundoitem)
{
undoManger.undo();
}
else if (EventS == selectitem | EventS == pselectitem)
{// 全选
textarea.selectAll();
}
else if (EventS == cutitem | EventS == pcutitem)
{// 剪切
String text = textarea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection, null);
textarea.replaceRange("", textarea.getSelectionStart(), textarea.getSelectionEnd());
}
else if (EventS == copyitem | EventS == pcopyitem)
{// 复制
String text = textarea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection, null);
}
else if (EventS == pasteitem | EventS == ppasteitem)
{// 粘贴
Transferable contents = clipboard.getContents(this);
if (contents == null)
return;
String text = "";
try
{
text = (String) contents.getTransferData(DataFlavor.stringFlavor);
}
catch (Exception ex)
{
}
textarea.replaceRange(text, textarea.getSelectionStart(), textarea.getSelectionEnd());
}
else if (EventS == deleteitem | EventS == pdeleteitem)
{// 删除
// 以下两种方法选一种
// textarea.replaceSelection("");
textarea.replaceRange("", textarea.getSelectionStart(), textarea.getSelectionEnd());
}
else if (EventS == finditem)
{// 查找
JDialog jfind = new FindJDialog(textarea, this);
jfind.setVisible(true);
}
else if (EventS == findnextitem)
{// 查找下一个
String text1 = findnextstr;// 要查找的内容
String findstr = textarea.getText();// 进行大小写判断用的,并重新获得当前内容
if (!findnextboolean)
{// 不区分大小写
findstr = findstr.toLowerCase();
}
if (!text1.equals(""))
{
if (!findnextboolean)
{// 不区分大小写
text1 = text1.toLowerCase();// 转换成小写
}
int index, i = textarea.getSelectionStart(), t = textarea.getSelectionEnd();
// 向上查找
if (findnextupdown.equals("up"))
{
String ftp = findstr.substring(0, i);
index = ftp.lastIndexOf(text1);
if (index != -1)
{
i = index - 1;
textarea.select(index, index + text1.length());
}
else
{
JOptionPane.showMessageDialog(this, "找不到 " + findnextstr, "提示",
JOptionPane.INFORMATION_MESSAGE);
}
}
// 向下查找
else
{
index = findstr.indexOf(text1, t);
if (index != -1)
{
t = index + text1.length();
textarea.select(index, index + text1.length());
}
else
{
JOptionPane.showMessageDialog(this, "找不到 " + findnextstr, "提示",
JOptionPane.INFORMATION_MESSAGE);
}
}
this.repaint();
}
else
{
JDialog jfind = new FindJDialog(textarea, this);
jfind.setVisible(true);
}
}
else if (EventS == replaceitem)
{// 替换
ReplaceJDialog rf = new ReplaceJDialog(textarea, this);
rf.setVisible(true);
}
else if (EventS == autorowitem)
{// 自动换行
if (autorowitem.getState())
{
textarea.setLineWrap(true);
}
else
{
textarea.setLineWrap(false);
}
}
else if (EventS == fontitem)
{// 字体设置
FontJDialog fj = new FontJDialog(textarea, this);
fj.setVisible(true);
}
else if (EventS == coloritem)
{// 字体颜色
fontColor(e);// 以e作参数调用自定义函数fontColor()
}
else if (EventS == aboutitem)
{// 关于对话框
JOptionPane.showMessageDialog(this, "傻刚刚噶...", "关于 JAVA 记事本",
JOptionPane.INFORMATION_MESSAGE);
}
}
// 字体颜色方法
public void fontColor(ActionEvent e)
{
Color fcolor = textarea.getForeground();// 将textarea.getForeground();赋值给Color对象fcolor
jColorChooser.setColor(fcolor);// 调用JColorChooser类中的方法setColor()
// jColorChooser.showDialog()调出颜色选择窗口...
textarea.setForeground(JColorChooser.showDialog(textarea, "选择颜色", fcolor));// 直接调用静态方法
}
// 读文件
public void readFile(String filename)
{
try
{
File file = new File(filename);
FileReader readin = new FileReader(file);
int size = (int) file.length();
int charsread = 0;
char[] content = new char[size];
while (readin.ready())
charsread += readin.read(content, charsread, size - charsread);
readin.close();
textarea.setText(new String(content, 0, charsread));
}
catch (IOException e)
{
System.out.println("Error Opening file");
return;
}
}
// 写文件
public void writeFile(String filename)
{
try
{
File file;
// 判断保存的文件名是否有(.txt)扩展名
if (filename.substring(filename.length() - 4, filename.length()).equals(".txt"))
{
file = new File(filename);
}
else
{
filename = filename + ".txt";
file = new File(filename);
}
FileWriter writeout = new FileWriter(file);
writeout.write(textarea.getText());
writeout.close();
}
catch (IOException e)
{
System.out.println("Error writing file");
return;
}
}
int tosave()
{// 判断是否要保存文件
// **********************
// 判断文件内容有没有改变
stemp = textarea.getText();
if (!(s.equals(stemp)))
{
ifsave = 1;
}
else
{
ifsave = -1;
}
// **********************
if (ifsave == 1)
{
int orsave = JOptionPane.showConfirmDialog(this, "是否保存文件" + filename, "记事本",
JOptionPane.YES_NO_CANCEL_OPTION);
switch (orsave)
{
case JOptionPane.YES_OPTION:
return 1;
case JOptionPane.NO_OPTION:
return -1;
case JOptionPane.CANCEL_OPTION:
return 0;
default:
return 0;
}
}
else
{
return -1;
}
}
// 更新文件
public void updates()
{
// 判断保存的文件名是否有(.txt)扩展名
if (!filename.substring(filename.length() - 4, filename.length()).equals(".txt"))
{
filename = filename + ".txt";
}
String strtemp = "";
if (filename == null)
{
strtemp = "无标题";
}
else
{
strtemp = filename;
}
this.setTitle(strtemp);
this.repaint();
}
public void mouseReleased(MouseEvent e)
{
if (e.isPopupTrigger())
jpm.show(this.textarea, e.getX(), e.getY());
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
}
// **************************************************************************
// 字体类
class FontJDialog extends JDialog implements ActionListener, KeyListener
{ // ,ItemListener
private final static long serialVersionUID = 1L;
JTextArea textarea = new JTextArea();
Panel contentPane = new Panel();
JLabel jLabel_ziti = new JLabel();
String[] data1 =
{
"Arial", "Arial Black", "Arial Narrow", "Basemic", "Book Antiqua", "Bookman Old Style",
"Century Gothic", "Comic Sans MS", "Fixedsys", "Garamond", "Monotype Corsiva", "Roman",
"方正舒体", "方正姚体", "仿宋_GB2312", "黑体", "华文彩云", "华文仿宋", "华文细黑", "华文新魏", "华文行楷", "华文中宋",
"楷体_GB2312", "隶书", "宋体", "宋体-方正超大字符集", "新宋体", "幼圆"
};
JList jList1 = new JList(data1);
JLabel jLabel_zixing = new JLabel();
String[] data2 =
{
"常规", "斜体", "粗体"
}; // ,"粗斜体"
JList jList2 = new JList(data2);
JLabel jLabel_daxiao = new JLabel();
String[] data3 =
{
"7", "8", "9", "10", "11", "12", "13", "14", "16", "18", "20", "22", "24", "26", "28",
"36", "48", "60", "72"
};
JList jList3 = new JList(data3);
JButton jButton1 = new JButton(); // 确定按钮
JComboBox jComboBox1 = new JComboBox();
JLabel jLabel_charset = new JLabel();
JTextField jTextField_preview = new JTextField("Preview Font");
JTextField jTextField_font = new JTextField("宋体");
JTextField jTextField_grapheme = new JTextField("常规");
JTextField jTextField_fontsize = new JTextField("12");
JButton jButton2 = new JButton(); // 取消按钮
int index;
String ziti = "宋体";
int daxiao = 18;
int zixing = 0;// 0为常规即Font.PLAIN;
public FontJDialog(JTextArea textarea, JFrame mainDialog)
{
super(mainDialog, "字体", true);
this.textarea = textarea;
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
Init(); // 执行初始化
}
catch (Exception e)
{
e.printStackTrace();
}
}
private void Init() throws Exception
{
// setLayout();
contentPane.setLayout(null);
this.getContentPane().setBackground(Color.lightGray);
this.setSize(new Dimension(440, 335));
// this.setTitle("字体");
this.setModal(true);
jLabel_ziti.setText("字体");
jList1.setToolTipText("提示");
jList1.setSelectedIndex(24);// 第24是"宋体"
jLabel_zixing.setText("字型");
jList2.setSelectedIndex(0);// 第0是"常规"
jLabel_daxiao.setText("大小");
jList3.setSelectedIndex(9);// 第8是"18"
jButton1.setText("确定");
jButton2.setText("取消");
jLabel_charset.setText("字符集");
// 把文本框的文本设置为中部对齐,两种方法
// jTextField_preview.setHorizontalAlignment(jTextField_preview.CENTER);
jTextField_preview.setHorizontalAlignment(0);// 2为左对齐 4为右对齐 0为中部对齐
jTextField_preview.setEnabled(false); // 不可编辑
jTextField_preview.setBackground(Color.lightGray);// 背景色
jTextField_preview.setForeground(Color.WHITE);// 前景色
jTextField_preview.setDisabledTextColor(Color.BLACK);// 字体色
jTextField_preview.setSize(20, 10);
JScrollPane scrollPane1 = new JScrollPane(jList1);
JScrollPane scrollPane2 = new JScrollPane(jList2);
JScrollPane scrollPane3 = new JScrollPane(jList3);
// Or in two steps:
// JScrollPane scrollPane = new JScrollPane();
// scrollPane.getViewport().setView(dataList);
jLabel_ziti.setBounds(new Rectangle(10, 8, 65, 23));
jLabel_zixing.setBounds(new Rectangle(163, 8, 65, 23));
jLabel_daxiao.setBounds(new Rectangle(274, 8, 65, 23));
jTextField_font.setEnabled(false);
jTextField_font.setDisabledTextColor(Color.BLACK);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -