大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这期内容当中小编将会给大家带来有关利用Java怎么实现一个文本编辑器,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
创新互联建站成立与2013年,是专业互联网技术服务公司,拥有项目成都网站设计、成都做网站、外贸网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元碧江做网站,已为上家服务,为碧江各地企业和个人服务,联系电话:18980820575
Java的特点有哪些 1.Java语言作为静态面向对象编程语言的代表,实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。 2.Java具有简单性、面向对象、分布式、安全性、平台独立与可移植性、动态性等特点。 3.使用Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。
结构分析:
界面布局 : EditFrame
main方法所在: EditText
打开功能实现: FileReadThread
保存跟能实现: save
一、 EditFrame
包括一个菜单Menu
底部:日期时间
代码附上:
public class EditFrame extends JFrame { // TODO 自动生成的构造函数存根 boolean saveFlag = false; File saveFileRoot = null; JFrame jFrame; JPanel jPanelSouth; JMenuBar jMenuBar1; JMenu jMenu1; JMenuItem jMenuItem1; JMenuItem jMenuItem2; JMenuItem jMenuItem3; JMenuItem jMenuItem4; JSeparator jSeparator1; JTextArea jTextArea; JScrollPane scrollPane;// 滚动条 public EditFrame() { // TODO 自动生成的构造函数存根 jFrame = new JFrame("水中鱼之1999-文本编辑器"); jPanelSouth = new JPanel(); jMenuBar1 = new JMenuBar(); jMenu1 = new JMenu("文件"); jMenuItem1 = new JMenuItem("打开"); jMenuItem2 = new JMenuItem("保存"); jMenuItem3 = new JMenuItem("另存为"); jMenuItem4 = new JMenuItem("退出"); jSeparator1 = new JSeparator(); jTextArea = new JTextArea(); scrollPane = new JScrollPane(jTextArea); jFrame.setSize(800, 500); jFrame.setLocationRelativeTo(null); jFrame.setVisible(false); setLayout(); setSouthPanel(); // set relationship for your component setRelationShip(); // 设置 scrollPane for TextArea setScscrollPane(); iniClick(); } private void setRelationShip() { jFrame.add(BorderLayout.CENTER, scrollPane); jFrame.add(BorderLayout.SOUTH, jPanelSouth); jMenu1.add(jMenuItem1); jMenu1.add(jMenuItem2); jMenu1.add(jMenuItem3); jMenu1.add(jSeparator1); jMenu1.add(jMenuItem4); jMenuBar1.add(jMenu1); jFrame.setJMenuBar(jMenuBar1); } private void setLayout() { GridLayout gridLayout = new GridLayout(1, 2); jPanelSouth.setLayout(gridLayout); } private void setScscrollPane() { // jTextArea.setLineWrap(true);// 设置满一行自动换行 scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); } private void setSouthPanel() { // add time for SouthPanel JLabel jLabelDate = new JLabel("Date"); JLabel jLabelTime = new JLabel("Time"); Timer timeAction = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { long timemillis = System.currentTimeMillis(); // 转换日期显示格式 SimpleDateFormat date = new SimpleDateFormat("yyyy 年 MM 月 dd 日 "); jLabelDate.setText(" 当前日期: " + date.format(new Date(timemillis))); SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss "); jLabelTime.setText(" 当前时间: " + time.format(new Date(timemillis))); } }); jPanelSouth.add(jLabelDate); jPanelSouth.add(jLabelTime); timeAction.start(); } private void iniClick() { jFrame.addWindowListener(new WindowListener() { @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub int x = JOptionPane.showConfirmDialog(null, "确认退出么?", "友情提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); if (x == 0) { System.exit(0); } } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub } }); jMenuItem4.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub int x = JOptionPane.showConfirmDialog(null, "确认退出么?", "友情提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); if (x == 0) { System.exit(0); } } }); jMenuItem1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub FileReadThread fileReadThread = new FileReadThread(EditFrame.this);// 开启文件读取线程 fileReadThread.start(); System.out.println(saveFileRoot); saveFlag = true; jTextArea.setText(""); } }); jMenuItem3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub Save save = new Save(EditFrame.this); save.start(); saveFlag = true; } }); jMenuItem2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (!saveFlag) { Save save = new Save(EditFrame.this); save.start(); saveFlag = true; } else { new Save(EditFrame.this, saveFileRoot); } } }); } public JTextArea getjTextArea() { return jTextArea; } public void setjTextArea(JTextArea jTextArea) { this.jTextArea = jTextArea; } public File getSaveFileRoot() { return saveFileRoot; } public void setSaveFileRoot(File saveFileRoot) { this.saveFileRoot = saveFileRoot; } public JFrame getjFrame() { return jFrame; } public void setjFrame(JFrame jFrame) { this.jFrame = jFrame; } }
二、测试类 EditText
分两部分:
1.闪屏 由于加载页面需要时间 原想用 SplashScreen 由于打包成jar包才能用所以这里用jframe进行替代
闪屏图片直接粘贴到:
2.new 一个EditFrame 对象, 闪屏结束后设置为可见
public class EditText { public static void main(String[] args) { new Thread() { @Override public void run() { // TODO Auto-generated method stub EditFrame editFrame = new EditFrame(); JFrame jFrame = new JFrame(); JPanel jPanel = new javax.swing.JPanel(){ protected void paintComponent(java.awt.Graphics g){ super.paintComponent(g); g.drawImage(new ImageIcon("experiment_bac.jpg").getImage(),0,0,400,250,null); } }; jFrame.add(jPanel); jFrame.setVisible(true); jFrame.setSize(400, 300); jFrame.setLocationRelativeTo(null); try { sleep(1500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } jFrame.dispose(); editFrame.getjFrame().setVisible(true); } }.start(); } }
三、FileReadThread
调用swing自带的 JFileChooser
选择文件路径
class FileReadThread extends Thread { private EditFrame test; public FileReadThread(EditFrame test ) { this.test = test; } @Override public void run() { JFileChooser chooser = new JFileChooser("d:/"); chooser.setFileFilter(new FileFilter() {// 定义文件过滤器,仅显示文件夹和txt文本 @Override public String getDescription() { return null; } @Override public boolean accept(File file) { if (file.isDirectory() || file.getName().endsWith(".txt")) return true; return false; } }); int option = chooser.showOpenDialog(test); if (option == JFileChooser.APPROVE_OPTION) { File selFile = chooser.getSelectedFile(); try { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(selFile), "gbk")); test.setSaveFileRoot(selFile); String line = null; while ((line = reader.readLine()) != null) { test.getjTextArea().append(line + "\n"); Thread.sleep(30);// 线程暂停,以看到读取过程效果 } } catch (Exception e1) { e1.printStackTrace(); } JOptionPane.showMessageDialog(test, "读取完毕"); } } }
四、save 保存
保存的调用分三种:
如果该文件是打开的 那么保存到打开文件中
如果该文件还未保存 这调用:进行保存
如果该文件已经另存为,则直接保存到另存为得的目录下
public class Save extends Thread { private EditFrame area; private File saveFileRoot = null; public Save(EditFrame area, File saveFileRoot) { System.out.println(saveFileRoot + "123"); String text = area.getjTextArea().getText(); String[] lines = text.trim().split("\n"); try { PrintWriter out = new PrintWriter(new FileOutputStream(saveFileRoot), true); for (String line : lines) out.println(line); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public Save(EditFrame area) { this.area = area; JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(".")); chooser.setFileFilter(new FileFilter() { @Override public boolean accept(File f) { return f.getName().toLowerCase().endsWith(".txt"); } @Override public String getDescription() { return "SAVE TO"; } }); int r = chooser.showSaveDialog(area); if (r != JFileChooser.APPROVE_OPTION) return; File f = chooser.getSelectedFile(); area.setSaveFileRoot(f); String text = area.getjTextArea().getText(); String[] lines = text.trim().split("\n"); try { PrintWriter out = new PrintWriter(new FileOutputStream(f), true); for (String line : lines) out.println(line); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public File getSaveFileRoot() { return saveFileRoot; } public void setSaveFileRoot(File saveFileRoot) { this.saveFileRoot = saveFileRoot; } }
上述就是小编为大家分享的利用Java怎么实现一个文本编辑器了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。