1.求Java记事本源代码
2.exceptionåerrorçåºå«
求Java记事本源代码
自己写的源码,能实现基本功能:
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
/*因为根据个人的电脑路径可能有所偏差,没有源路径的源码automapper源码情况下,设置默认保存路径为D盘根目录下
* 若要选择保存其他地方,源码监测指标源码免费查询可以选择 另存为*/
public class TestDemo extends JFrame {
private static final long serialVersionUID = -L;
private String url = null;//文件路径
private String str=null;//复制或剪切 的源码研判系统源码怎么找字符串
private StringSelection stringSelection=null;
private Clipboard clipboard=new Clipboard(str);
private Transferable transferable=null;
private DataFlavor flavor=null;
public TestDemo() {
init();
}
private void init() {
setTitle("我的记事本");
setSize(, );
setContentPane(createContentPane());//添加主面板
}
/*创建主面板*/
private JPanel createContentPane() {
JPanel pane = new JPanel(new BorderLayout());
pane.add(BorderLayout.NORTH, createChocePane());//添加菜单栏
pane.add(createAreaPane());//添加文本编辑区域
return pane;
}
/*创建菜单栏,以及实现功能*/
private JPanel createChocePane() {
JPanel pane = new JPanel();
JMenuBar menuBar1 = new JMenuBar();
JMenu menu = new JMenu("文件");
menuBar1.add(menu);
JMenuItem menuIt1 = new JMenuItem("新建");
JMenuItem menuIt2 = new JMenuItem("打开");
JMenuItem menuIt3 = new JMenuItem("保存");
JMenuItem menuIt4 = new JMenuItem("另存为");
menu.add(menuIt1);
menu.add(menuIt2);
menu.add(menuIt3);
menu.add(menuIt4);
JMenuBar menuBar2 = new JMenuBar();
JMenu menu2 = new JMenu("编辑");
menuBar2.add(menu2);
JMenuItem menuIt5 = new JMenuItem("复制");
JMenuItem menuIt6 = new JMenuItem("剪切");
JMenuItem menuIt7 = new JMenuItem("粘帖");
menu2.add(menuIt5);
menu2.add(menuIt6);
menu2.add(menuIt7);
JMenuBar menuBar3 = new JMenuBar();
JMenu menu3 = new JMenu("帮助");
menuBar3.add(menu3);
JMenuItem menuIt8 = new JMenuItem("关于记事本");
menu3.add(menuIt8);
pane.add(menuBar1);
pane.add(menuBar2);
pane.add(menuBar3);
menuIt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
testArea.setText(null);
}
});
menuIt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
final FileDialog fd = new FileDialog(new JFrame(),源码超级赛道公式源码在哪 "查找文件",
FileDialog.LOAD);
fd.setVisible(true);
if (fd.getDirectory() != null && fd.getFile() != null) {
testArea.setText(null);
url = fd.getDirectory() + fd.getFile();
try {
BufferedReader in = new BufferedReader(new FileReader(
url));
for (int i = 0;; i++) {
testArea.append(in.readLine());
if (in.read() == -1) {
break;
} else
continue;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
menuIt3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (url==null) {
url="D:\\新建 文本文档.txt";
}
File f = new File(url);
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter(url));
f.createNewFile();
out.append(testArea.getText());
out.flush();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
menuIt4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
FileDialog fd = new FileDialog(new JFrame(), "保存文本",
FileDialog.SAVE);
fd.setVisible(true);
if (url!=null) {
File f = new File(url);
BufferedWriter out = null;
try {
f.createNewFile();
out = new BufferedWriter(new FileWriter(url));
out.append(testArea.getText());
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
menuIt5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str=testArea.getSelectedText();
stringSelection=new StringSelection(str);
clipboard.setContents(stringSelection, null);
}
});
menuIt6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
str=testArea.getSelectedText();
stringSelection=new StringSelection(str);
clipboard.setContents(stringSelection, null);
int start=testArea.getSelectionStart();
int end=testArea.getSelectionEnd();
testArea.replaceRange( null,start,end);
}
});
menuIt7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
transferable=clipboard.getContents(this);
flavor=DataFlavor.stringFlavor;
if (transferable.isDataFlavorSupported(flavor)) {
int start=testArea.getSelectionStart();
int end=testArea.getSelectionEnd();
testArea.replaceRange( null,start,end);
int pos=testArea.getCaretPosition();
try {
str=(String)transferable.getTransferData(flavor);
testArea.insert(str, pos);
} catch (UnsupportedFlavorException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
menuIt8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"功能简单,绝对原创 ");
}
});
return pane;
}
JTextArea testArea;
private JScrollPane createAreaPane() {
JScrollPane pane = new JScrollPane();
pane.setBorder(new TitledBorder("编辑区域"));
testArea = new JTextArea();
testArea.setFont(new Font("宋体",源码云蹦迪开源码下载 Font.BOLD, ));
testArea.setLineWrap(true);
pane.getViewport().add(testArea);
return pane;
}
public static void main(String[] args) {
TestDemo td = new TestDemo();
td.setVisible(true);
}
}
exceptionåerrorçåºå«
1.å¦å¾Exception å Erroré½ç»§æ¿èªThrowableç±»ï¼ç±jdk apiææ¡£ä¸å¯¹è¯¥ç±»çæè¿°å¯ä»¥ç¥éå®æ¯å¼å¸¸å¤çæºå¶çåºæ¬ç»æç±»åãå¯ä»¥ç±èææºæåºæè ç¼ç è èªè¡æåº(throw)jdk8ä¸æåç¿»è¯Throwableç±»çæè¿°ï¼Throwableç±»æ¯Javaè¯è¨ä¸ææé误åå¼å¸¸çThrowableç±»ã åªæä½ä¸ºæ¤ç±»ï¼æå ¶ä¸ä¸ªåç±»ï¼çå®ä¾ç对象ç±Javaèææºæåºï¼æè å¯ä»¥ç±Java throwè¯å¥æåºã 类似å°ï¼åªæè¿ä¸ªç±»æå ¶åç±»å¯ä»¥æ¯catchåå¥ä¸çåæ°ç±»åã
2.Exception å Error ä½ç°äº Java å¹³å°è®¾è®¡è 对ä¸åå¼å¸¸æ åµçåç±»ãException æ¯ç¨åºæ£å¸¸è¿è¡ä¸ï¼å¯ä»¥é¢æçæå¤æ åµï¼å¯è½å¹¶ä¸åºè¯¥è¢«æè·ï¼è¿è¡ç¸åºå¤çã
3.Error æ¯æå¨æ£å¸¸æ åµä¸ï¼ä¸å¤§å¯è½åºç°çæ åµï¼ç»å¤§é¨åç Error é½ä¼å¯¼è´ç¨åºï¼æ¯å¦ JVM èªèº«ï¼å¤äºéæ£å¸¸çãä¸å¯æ¢å¤ç¶æãæ¢ç¶æ¯éæ£å¸¸æ åµï¼æ以ä¸ä¾¿äºä¹ä¸éè¦æè·ï¼å¸¸è§çæ¯å¦ OutOfMemoryError ä¹ç±»ï¼é½æ¯ Error çåç±»ã
4.Exception åå为å¯æ£æ¥ï¼checkedï¼å¼å¸¸åä¸æ£æ¥ï¼uncheckedï¼å¼å¸¸ï¼å¯æ£æ¥å¼å¸¸å¨æºä»£ç éå¿ é¡»æ¾å¼å°è¿è¡æè·å¤çï¼è¿æ¯ç¼è¯ææ£æ¥çä¸é¨åãåé¢æä»ç»çä¸å¯æ¥ç Errorï¼æ¯ Throwable ä¸æ¯ Exceptionã
å¦ä½å¤çè¿äºå¼å¸¸ï¼
å¨ç°å®ç¼ç¨ä¸æ们ä¸è¬ä½¿ç¨ try-with-resources å multiple catchæ¥è¿è¡ä¸äºå¼å¸¸å¤ç(便å©çç¹æ§)ï¼å¨ç¼è¯æ¶æï¼ä¼èªå¨çæç¸åºçå¤çé»è¾ï¼æ¯å¦ï¼èªå¨æç §çº¦å®ä¿æ close é£äºæ©å±äº AutoCloseable æè Closeable ç对象ã
try-with-resources æ¯ä¸ç§å¤çCloseableå®ç°ç±»å ³éèµæºçä¸ç§åæ³ï¼ç®ååäºä¸ä¸ªä¾åå¯ä»¥åç°å ¶å®è¿å°±æ¯ä¸ä¸è¯æ³ï¼è¿ç§è¯æ³å¯ä»¥èªå¨ç¼è¯å¸®å¿ç¼è¯æåå ³éæµçæä½ï¼
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("a"));
BufferedWriter writer = new BufferedWriter(new FileWriter("a"))) {
// Try-with-resources
} catch (IOException e) { // Multiple catch
// Handle it
}
}
ç¼è¯åclass
public static void main(String[] args) { try {
BufferedReader br = new BufferedReader(new FileReader("a"));
Throwable var2 = null; try {
BufferedWriter writer = new BufferedWriter(new FileWriter("a"));
Object var4 = null; if (writer != null) { if (var4 != null) { try {
writer.close();
} catch (Throwable var) {
((Throwable)var4).addSuppressed(var);
}
} else {
writer.close();
}
}
} catch (Throwable var) {
var2 = var; throw var;
} finally { if (br != null) { if (var2 != null) { try {
br.close();
} catch (Throwable var) {
var2.addSuppressed(var);
}
} else {
br.close();
}
}
}
} catch (IOException var) {
}
}
å¼å¸¸å¤çç两个åºæ¬åå
å°½éä¸è¦æè·ç±»ä¼¼ Exception è¿æ ·çéç¨å¼å¸¸ï¼èæ¯åºè¯¥æè·ç¹å®å¼å¸¸
è¿æ¯å 为å¨æ¥å¸¸çå¼åååä½ä¸ï¼æ们读代ç çæºä¼å¾å¾è¶ è¿å代ç ï¼è½¯ä»¶å·¥ç¨æ¯é¨åä½çèºæ¯ï¼æ以æ们æä¹å¡è®©èªå·±ç代ç è½å¤ç´è§å°ä½ç°åºå°½éå¤çä¿¡æ¯ï¼èæ³æ³ç Exception ä¹ç±»ï¼æ°æ°éèäºæ们çç®çãå¦å¤ï¼æ们ä¹è¦ä¿è¯ç¨åºä¸ä¼æè·å°æ们ä¸å¸ææè·çå¼å¸¸ãæ¯å¦ï¼ä½ å¯è½æ´å¸æ RuntimeException 被æ©æ£åºæ¥ï¼èä¸æ¯è¢«æè·ã
ä¸è¦çåï¼swallowï¼å¼å¸¸ãè¿æ¯å¼å¸¸å¤çä¸è¦ç¹å«æ³¨æçäºæ ï¼å 为å¾å¯è½ä¼å¯¼è´é常é¾ä»¥è¯æç诡å¼æ åµã
å¦ææ们ä¸æå¼å¸¸æåºæ¥ï¼æè ä¹æ²¡æè¾åºå°æ¥å¿ï¼Loggerï¼ä¹ç±»ï¼ç¨åºå¯è½å¨åç»ä»£ç 以ä¸å¯æ§çæ¹å¼ç»æã没人è½å¤è½»æå¤æ究ç«æ¯åªéæåºäºå¼å¸¸ï¼ä»¥åæ¯ä»ä¹åå 产çäºå¼å¸¸ã
å¨åç¨åºæ¶å¯ä»¥éè¿Objectsç±»(jdkæä¾),æè æè¨çæåå¤æé®é¢ï¼å¦ç©ºæéå¼å¸¸çä¸äºå¼çå¤çObjects. requireNonNull(filename);èä¸æ¯ä½¿ç¨ä¹åæé®é¢åç³»ç»æåºå¼å¸¸ï¼è¿å¯è½å°±ä¼ä¸ç´è§çæ¾å°é®é¢æå¨ãå³:Throw early, catch late åå
å¸æ对æ¨ææ帮å©ï¼~