简单Java仿windows记事本 下载本文

内容发布更新时间 : 2024/12/25 0:25:31星期一 下面是文章的全部内容请认真阅读。

jmEdit.add(jmiUndoItem); jmEdit.add(new JSeparator()); jmEdit.add(jmiCutItem); jmEdit.add(jmiCopyItem); jmEdit.add(jmiPasteItem); jmEdit.add(jmiDeleteItem); jmEdit.add(new JSeparator()); jmEdit.add(jmiSearchItem); jmEdit.add(jmiSearchNextItem); jmEdit.add(jmiReplaceItem); jmEdit.add(jmiGoToItem); jmEdit.add(new JSeparator()); jmEdit.add(jmiSelectAllItem); jmEdit.add(jmiDateItem);

//************************* 风格菜单的实现 JCheckBoxjcbAutoNextLine=new JCheckBox(\自动换行\ JMenuItemjmiFontitemItem = new JMenuItem(\字体\ jmStyle.add(jcbAutoNextLine); jmStyle.add(jmiFontitemItem); JMenuItemjmiViewStatusItem = new JMenuItem(\查看状态栏\ jmiViewStatusItem.setEnabled(false); jmView.add(jmiViewStatusItem); JMenuItemjmiAboutItem=new JMenuItem(\关于\ jmiAboutItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub newHelpDialog(Notepad.this); } });

//**********************帮助菜单 jmHelp.add(jmiAboutItem); setLayout(new BorderLayout()); JScrollPanejsp=new JScrollPane(jta); jsp.requestFocus(true); content.add(jsp,BorderLayout.CENTER);

}

public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub try {

UIManager.setLookAndFeel(\ } catch (Exception e) { System.out.print(\ System.exit(0); // TODO: handle exception } JFrame frame=new Notepad(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { int r=JOptionPane.showConfirmDialog(null, \确认退出吗?\ if(r==JOptionPane.OK_OPTION) System.exit(0); } }); frame.setSize(1000,500); frame.setLocationRelativeTo(null); frame.setVisible(true); }

//***************************** 和剪贴板相关函数的实现 public String getClipbordContent(Clipboard clip) throws Exception { Transferable trans = clip.getContents(null); if(trans!=null) { if(trans.isDataFlavorSupported(DataFlavor.stringFlavor)) return (String)trans.getTransferData(DataFlavor.stringFlavor); else return \ } else return \ } public void setClipbordContent(Clipboard clip,Stringstr) throws Exception { Transferable trans=new StringSelection(str); clip.setContents(trans, null);

}

//********************************监听编辑菜单下item的事件 classEditListener implements ActionListener { private String tempString=\ String textString=jta.getText(); String searchString=\ intstringLength=0; intstartPos; JTextFieldjtField=null; @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub

if(e.getActionCommand().equals(\剪切(T) Ctrl+T\{ tempString=jta.getSelectedText(); try { setClipbordContent(clip, tempString); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } jta.replaceSelection(\}

else if (e.getActionCommand().equals(\复制(C) Ctrl+C\ tempString=jta.getSelectedText(); try { setClipbordContent(clip, tempString); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } }

else if (e.getActionCommand().equals(\粘贴(P) Ctrl+V\ try { tempString=getClipbordContent(clip); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } intpos=jta.getCaretPosition(); jta.insert(tempString, pos);

}

else if (e.getActionCommand().equals(\删除(L) Del\ jta.replaceSelection(\}

else if (e.getActionCommand().equals(\查找(F) Ctrl+F\ jdSearchDialog=new JDialog(Notepad.this,\查找\ Container content=jdSearchDialog.getContentPane(); JLabeljl=new JLabel(\查找内容:\ jtField=new JTextField(20); jtField.requestFocus();

JButton jbt1=new JButton(\查找\

jbt1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub searchString=jtField.getText(); stringLength=searchString.length(); if(stringLength>0) { startPos=jta.getText().indexOf(searchString); if(startPos>=0) { jta.select(startPos, startPos+stringLength); } else { JOptionPane.showConfirmDialog(Notepad.this, \找不到\ } } else { JOptionPane.showConfirmDialog(Notepad.this, \请输入查找内容\ } } }); JPanel jp1=new JPanel(new BorderLayout(3,1)); jp1.add(jl,BorderLayout.WEST); jp1.add(jtField,BorderLayout.CENTER); jp1.add(jbt1,BorderLayout.EAST); JPanel jp3 = new JPanel(new GridLayout(1,2)); jp3.setBorder(BorderFactory.createTitledBorder(\方向\

Clicked\

JRadioButton jrb1=new JRadioButton(\向上(U)\ JRadioButton jrb2=new JRadioButton(\向下(D)\ ButtonGroupbgButtonGroup=new ButtonGroup(); bgButtonGroup.add(jrb1); bgButtonGroup.add(jrb2); jp3.add(jrb1); jp3.add(jrb2);

JCheckBoxjcb=new JCheckBox(\区分大小写(c)\ JPanel jp2 = new JPanel(new BorderLayout());

JButton jbt2=new JButton(\取消\

jbt2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub jdSearchDialog.dispose(); } });

jp2.add(jcb,BorderLayout.WEST); jp2.add(jp3,BorderLayout.CENTER); jp2.add(jbt2,BorderLayout.EAST);

content.setLayout(new BorderLayout()); content.add(jp1,BorderLayout.NORTH); content.add(jp2,BorderLayout.CENTER);

jdSearchDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); jdSearchDialog.setLocationRelativeTo(null); jdSearchDialog.setSize(400,100); jdSearchDialog.setResizable(false); jdSearchDialog.setVisible(true);

}

else if(e.getActionCommand().equals(\查找下一个(N) F3\

{ JOptionPane.showConfirmDialog(Notepad.this, \查找下一个(N) }

else if (e.getActionCommand().equals(\替换\ JOptionPane.showConfirmDialog(Notepad.this, \替换 Clicked\}

else if(e.getActionCommand().equals(\转到\{ JOptionPane.showConfirmDialog(Notepad.this, \转到\}

F3