📄 book.java
字号:
private void TextCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_TextCaretUpdate Text.setLineWrap(true);//自动换行 try{ int pos = Text.getCaretPosition();//获取行数 int lineOfC = Text.getLineOfOffset(pos) + 1; //获取列数 int col = pos - Text.getLineStartOffset(lineOfC - 1) + 1; Label.setText("当前光标位置 " + lineOfC + " 行 , " + col + " 列 "); } catch(Exception ex){ Label.setText(" 无法获得当前光标位置"); } // TODO add your handling code here: }//GEN-LAST:event_TextCaretUpdate private void NewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NewActionPerformed int value=-1; if(hasChange()){ String[] buttonTexts={"保存","不保存","取消"}; value=JOptionPane.showOptionDialog(this, "源程序已经改变,请确认是否保存!", "保存更改?", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, buttonTexts, buttonTexts[0]); if(value==JOptionPane.YES_OPTION) SaveActionPerformed(evt); else if(value==JOptionPane.NO_OPTION){ } else if(value==JOptionPane.CANCEL_OPTION) return; } else{ SaveAsActionPerformed(evt); } fileName=null; Text.setText(""); setTitle("新建记事本"); isChange=false; return; }//GEN-LAST:event_NewActionPerformed private void QuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_QuitActionPerformed if(hasChange()){ int value = JOptionPane.showConfirmDialog(this,"是否保存当前文本文档?", "保存提示", JOptionPane.OK_OPTION,JOptionPane.WARNING_MESSAGE); if (value == JOptionPane.OK_OPTION) { SaveActionPerformed(evt); } else System.exit(0); }//GEN-LAST:event_QuitActionPerformed else System.exit(0); } private void CopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CopyActionPerformed Text.copy(); Label.setText("执行了复制操作"); // TODO add your handling code here: }//GEN-LAST:event_CopyActionPerformed private void CutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CutActionPerformed Text.cut(); Label.setText("执行了剪切操作"); // TODO add your handling code here: }//GEN-LAST:event_CutActionPerformed private void PasteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PasteActionPerformed Text.paste(); Label.setText("执行了粘贴操作"); // TODO add your handling code here: }//GEN-LAST:event_PasteActionPerformed private void SelectAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SelectAllActionPerformed Text.selectAll(); Label.setText("执行了全选操作"); // TODO add your handling code here: }//GEN-LAST:event_SelectAllActionPerformed private void DeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteActionPerformed this.Text.replaceRange(null,this.Text.getSelectionStart(),this.Text.getSelectionEnd()); Label.setText("执行了删除操作"); // TODO add your handling code here: }//GEN-LAST:event_DeleteActionPerformed private void bNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bNewActionPerformed NewActionPerformed(evt); // TODO add your handling code here:}//GEN-LAST:event_bNewActionPerformed private void OpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OpenActionPerformed BufferedReader input=null; String textString; int ret=fileChooser.showOpenDialog(this); if(ret!=JFileChooser.APPROVE_OPTION) return; NewActionPerformed(evt); fileName=fileChooser.getSelectedFile(); if(fileName.exists()&&fileName.canRead()){ setTitle("记事本:"+fileName.getName()); try{ input=new BufferedReader(new FileReader(fileName)); while((textString=input.readLine())!=null) Text.append(textString+"\n"); input.close(); } catch(IOException e){ JOptionPane.showMessageDialog(this, "文件打开错误,没有打开"); } finally{ input=null; } isChange=false; return; } // TODO add your handling code here: }//GEN-LAST:event_OpenActionPerformed private void bOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bOpenActionPerformed OpenActionPerformed(evt); // TODO add your handling code here:}//GEN-LAST:event_bOpenActionPerformed private void bSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSaveActionPerformed SaveActionPerformed(evt); // TODO add your handling code here:}//GEN-LAST:event_bSaveActionPerformed private void bCutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCutActionPerformed CutActionPerformed(evt); // TODO add your handling code here:}//GEN-LAST:event_bCutActionPerformed private void bCopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCopyActionPerformed CopyActionPerformed(evt); // TODO add your handling code here:}//GEN-LAST:event_bCopyActionPerformed private void bPasteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bPasteActionPerformed PasteActionPerformed(evt); // TODO add your handling code here:}//GEN-LAST:event_bPasteActionPerformed private void bDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bDeleteActionPerformed DeleteActionPerformed(evt); // TODO add your handling code here:}//GEN-LAST:event_bDeleteActionPerformed private void bUndoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bUndoActionPerformed UndoActionPerformed(evt); // TODO add your handling code here:}//GEN-LAST:event_bUndoActionPerformed private void UndoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UndoActionPerformed Manager.undo(); }//GEN-LAST:event_UndoActionPerformed public void undoableEditHappened(UndoableEditEvent parm1) { boolean addEdit = Manager.addEdit(parm1.getEdit()); } private void SearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SearchActionPerformed final JDialog jd = new JDialog(this, "替换", true); GridBagLayout gbl = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); gbc.weightx = 1; gbc.weighty = 1; gbc.gridwidth = 1; gbc.gridheight = 1; JLabel jlFind = new JLabel("查找:"); JLabel jp = new JLabel("替换内容:"); final JTextField jtf = new JTextField(15); final JTextField jtf1 = new JTextField(15); jd.getContentPane().setLayout(gbl); jd.setSize(330, 150); jd.setResizable(false); final JButton jbReplaceAll = new JButton("替换"); final JButton jbCancel = new JButton("取消"); final JButton jbFind = new JButton("查找"); gbc.gridx = 0; gbc.gridy = 0; jd.getContentPane().add(jlFind, gbc); gbc.gridx = 1; gbc.gridy = 0; jd.getContentPane().add(jtf1, gbc); gbc.gridx = 2; gbc.gridy = 0; jd.getContentPane().add(jbFind, gbc); gbc.gridx = 0; gbc.gridy = 1; jd.getContentPane().add(jp, gbc); gbc.gridx = 1; gbc.gridy = 1; jd.getContentPane().add(jtf, gbc); gbc.gridx = 2; gbc.gridy = 1; jd.getContentPane().add(jbReplaceAll, gbc); gbc.gridx = 2; gbc.gridy = 2; jd.getContentPane().add(jbCancel, gbc); jbFind.addActionListener(new ActionListener() { private int StartFindPos=0; private int b; private int a; public void actionPerformed(ActionEvent efind) { String strA = Text.getText(); String strB = jtf1.getText(); if (a >= 0) { a = strA.indexOf(strB, StartFindPos); b = strB.length(); StartFindPos = a + b; if (a == -1) { JOptionPane.showMessageDialog(null, "没有您要查找的信息", "查找结果", 1); a = 0; StartFindPos = 0; } Text.select(a, StartFindPos); } } }); jbReplaceAll.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int a = 0; int b; int StartFindPos = 0; while (a > -1) { String strA = Text.getText(); String strB = jtf1.getText(); a = strA.indexOf(strB, StartFindPos); if (a == -1) { break; } b = strB.length(); StartFindPos = a + b; Text.select(a, StartFindPos); String strRepleaceAll = jtf.getText(); Text.replaceSelection(strRepleaceAll); StartFindPos = a + b; } JOptionPane.showMessageDialog(null, "全部替换完毕", "替换内容", 1); a = 0; StartFindPos = 0; } }); jbCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ejb) { jd.dispose(); } }); jd.setLocation(240, 200); jd.setVisible(true); jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE); // TODO add your handling code here: }//GEN-LAST:event_SearchActionPerformed private void AboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AboutActionPerformed about guanyu=new about(); guanyu.show(); // TODO add your handling code here: }//GEN-LAST:event_AboutActionPerformed private void RedoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RedoActionPerformed Manager.redo(); // TODO add your handling code here: }//GEN-LAST:event_RedoActionPerformed private void SetColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SetColorActionPerformed Color color=JColorChooser.showDialog(Text, "选择颜色", Color.BLACK); Text.setForeground(color); // TODO add your handling code her }//GEN-LAST:event_SetColorActionPerformed private void FontActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FontActionPerformed new font(Text); } class font{ private JTextArea textArea; private JButton ok,cancel; private JComboBox fontName,fontSize,fontStyle; GraphicsEnvironment ge;//定义系统字体对象 String[] size = {"8","10","12","14","16","18","20","22","24","26","28","32","36","48","72","96"}; String[] style= {"PLAIN","BOLD","ITALIC"}; JFrame jf = new JFrame("字体设置"); public font(JTextArea Text){ this.textArea=Text; JLabel label1=new JLabel(" 字体 "); JLabel label2=new JLabel(" 字号 "); JLabel label3=new JLabel(" 样式 "); ge = GraphicsEnvironment.getLocalGraphicsEnvironment();//获取系统字体 String[] fontname = ge.getAvailableFontFamilyNames(); fontName = new JComboBox(fontname); fontSize = new JComboBox(size); fontStyle = new JComboBox(style); ok = new JButton("确定"); cancel = new JButton("取消"); jf.setLayout(new BorderLayout()); JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); p1.add(label1); p1.add(label2); p1.add(label3); p2.add(fontName); p2.add(fontSize); p2.add(fontStyle); p3.add(ok);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -