黑马程序员_javaGUI综合部件布局与事件处理
2015-11-30 22:35
579 查看
------- android培训、java培训、期待与您交流! ----------
构建简单的记事本
类的基本结构:
猜一下代码有多长?
之间粘贴上来肯定没人去看,我还是添加到附件上吧。
我擦‘’‘’‘~谁能告诉我在哪里可以添加附件!!!!
No! 竟然连个添加附件都没有!!希望是我眼瞎,不然真不知道你们是怎么过来的,
哎,那我也只好把思想心得添加都源代码上的,留给自己以后回忆曾经逝去的青春中的一枚符号吧。
学了将近一个月的java,从一开始的十几行,到现在不经意间就上百行了,心里还是觉得震惊的,
虽然可能质量还不咋的,而且还是不是的出现一些更让人想吐血的错误,但分析问题的能力算是提高一小节亦大阶梯。
有了自己的学习方法,还明白了自己的不足,则将会成为今后学习中的转折点,加油!为自己! 加油!为明天!
构建简单的记事本
类的基本结构:
猜一下代码有多长?
之间粘贴上来肯定没人去看,我还是添加到附件上吧。
我擦‘’‘’‘~谁能告诉我在哪里可以添加附件!!!!
No! 竟然连个添加附件都没有!!希望是我眼瞎,不然真不知道你们是怎么过来的,
哎,那我也只好把思想心得添加都源代码上的,留给自己以后回忆曾经逝去的青春中的一枚符号吧。
public class Wordpad { private static Frame f; private MenuBar bar; private Menu wj; private Menu cx; private MenuItem open; private MenuItem close; private MenuItem save; private MenuItem exit; private MenuItem search; private MenuItem replace; private static TextArea ta; private static FileDialog fd; private static File file; //构造方法 Wordpad(){ init(); } //设计主页面 private void init() { f = new Frame("记事本"); bar = new MenuBar(); wj = new Menu("文件"); cx = new Menu("查找"); open = new MenuItem("打开"); close = new MenuItem("关闭"); save = new MenuItem("保存"); exit = new MenuItem("退出"); search = new MenuItem("查询"); replace = new MenuItem("替换"); ta = new TextArea(); f.setBounds(200,100,600,550); f.setMenuBar(bar); bar.add(wj); wj.add(open); wj.add(close); wj.add(cx); wj.add(save); wj.add(exit); cx.add(search); cx.add(replace); f.add(ta); myEvent(); f.setVisible(true); } //给主页的组件添加事件 private void myEvent() { f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); open.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ WjTool.openFile(); } }); close.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ WjTool.closeFile(); } }); save.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ WjTool.saveFile(); } }); exit.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.exit(0); } }); search.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ new SeachTishi(); } }); replace.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ new ReplaceTishi(); } }); } //文件工具类. private static class WjTool{ static void openFile() { fd = new FileDialog(f,"打开",FileDialog.LOAD); fd.setVisible(true); String dirPath = fd.getDirectory(); String fileName = fd.getFile(); if(dirPath==null||fileName==null) return ; ta.setText(""); file = new File(dirPath , fileName); BufferedReader buf = null; try { buf = new BufferedReader(new FileReader(file)); String s = null ; while((s=buf.readLine())!=null){ ta.append(s+"\r\n"); } } catch (IOException e) { throw new RuntimeException(e.getMessage()); } finally{ try { if(buf!=null) buf.close(); } catch (IOException e) { e.printStackTrace(); } } } static void closeFile() { saveFile(); ta.setText(""); file = null; } static void saveFile() { if(file==null){ fd = new FileDialog(f,"保存",FileDialog.SAVE); fd.setVisible(true); String dirPath = fd.getDirectory(); String fileName = fd.getFile(); if(dirPath==null||fileName==null) return ; file = new File(dirPath,fileName); } BufferedWriter buf = null; try { buf = new BufferedWriter(new FileWriter(file)); buf.write(ta.getText()); } catch (IOException e) { e.printStackTrace(); } finally{ try { if(buf!=null) buf.close(); } catch (IOException e) { e.printStackTrace(); } } } } //查询操作信息提示窗口类(内部类) private class SeachTishi{ Dialog d; TextField tf; Label lb; Button but; SeachTishi(){ info(); } //创建设置提示窗体页面 private void info() { d = new Dialog(f,"查找",false); tf = new TextField(40); but = new Button("确认"); lb = new Label(); d.setBounds(300,200,400,150); d.setLayout(new FlowLayout()); d.add(tf); d.add(but); d.add(lb); myEvent(); d.setVisible(true); } //给提示窗体页面中的组件添加事件监听器 private void myEvent(){ d.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ d.setVisible(false); } }); tf.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent e){ if(e.getKeyCode()==KeyEvent.VK_ENTER) searchShow(); } }); but.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ searchShow(); } }); } private void searchShow() { String key = tf.getText(); if(key.equals("")) return ; String isExist =ta.getText().contains(key)? "存在":"不存在"; lb.setText("\""+key+"\""+"在文本中"+isExist); d.setVisible(true); } } //替换操作信息提示窗口类(内部类) private class ReplaceTishi{ Dialog d; TextField tfkey; Label lb; TextField tfvalue; Label lbl; Button but; ReplaceTishi(){ info(); } //创建设置提示窗体页面 private void info() { d = new Dialog(f,"替换",false); tfkey = new TextField(15); tfvalue = new TextField(15); lb = new Label("替换为"); but = new Button("确认"); lbl = new Label(); d.setBounds(300,200,400,150); d.setLayout(new FlowLayout()); d.add(tfkey); d.add(lb); d.add(tfvalue); d.add(but); d.add(lbl); myEvent(); d.setVisible(true); } //给提示窗体页面中的组件添加事件监听器 private void myEvent(){ d.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ d.setVisible(false); } }); tfvalue.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent e){ if(e.getKeyCode()==KeyEvent.VK_ENTER) replaceShow(); } }); but.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ replaceShow(); } }); } private void replaceShow() { String key = tfkey.getText(); if(key.equals("")) return ; String value = tfvalue.getText(); StringBuilder sb = new StringBuilder(ta.getText()); int index = 0; int count = 0 ; while((index=sb.indexOf(key,index))!=-1){ System.out.println(index); sb.delete(index, index+key.length()); sb.insert(index, value); //防止因为value中含有key,而也被导致替换。 index = index + value.length(); count++; } lbl.setText("替换已完成:已替换"+count+"处"); ta.setText(sb.toString()); d.setVisible(true); } } public static void main(String[] args) { new Wordpad(); } }
学了将近一个月的java,从一开始的十几行,到现在不经意间就上百行了,心里还是觉得震惊的,
虽然可能质量还不咋的,而且还是不是的出现一些更让人想吐血的错误,但分析问题的能力算是提高一小节亦大阶梯。
有了自己的学习方法,还明白了自己的不足,则将会成为今后学习中的转折点,加油!为自己! 加油!为明天!
相关文章推荐
- Masonry + UIView Animations 注意事项
- Duilib源码分析(四)绘制管理器—CPaintManagerUI
- 理解 Android Build 系统
- 【LEETCODE】62-Unique Paths
- 快速排序--quickSort
- UICollectionView
- 容器总结——array、vector、deque
- 动态修改UINavigationBar的背景色
- description The resource identified by this request is only capable of generating responses with cha
- UI(一)
- request与response
- easy-ui 小白进阶史(二):操作数据,easy-ui操作
- ui 霓虹灯
- StringBuffer和StringBuilder
- IOS开发之UIview
- UIButton 的一些属性
- 奇怪的现象:touchesBegan: 与UITapGestureRecognizer手势没有人响应 以及set方法的妙用
- 在Visual Studio中使用VueJS时,不可以用 v-bind 的简写 : 及 v-on的简写 @
- UI第十四天:UI⾼级可视化设计
- c#子线程操作ui