swing体系:Java中用来做UI的一套API java.awt javax.swing JFrame:窗体 JPanel:面板 JLabel:文本标签和图片标签 JTextField:文本输入框 JPasswordField:密码输入框 JButton:按钮 JComboBox:下拉框 JCkeckBox:多选框 JRadioButton:单选按钮 JTextArea:多行文本框 JMenuBar JMenu JMenuItem 在创建菜单栏时,如果有"-"必须要进行判断 if(items[i][j].equals("-")){ menu.addSeparator(); }else{ JMenuItem item = new JMenuItem(items[i][j]); item.setActionCommand(commands[i][j]); menu.add(item); 事件:对于组件的任何一个操作:点击按钮,选中菜单项 鼠标移动,鼠标按下,键盘输入... 事件监听器:用来监听在某个组件上是否发生了对应的事件 事件源:发生事件的组件对象 常用的事件监听器:(接口) 动作监听器:ActionListener 鼠标按键监听器:MouseListener 鼠标移动监听器:MouseMotionListener 鼠标滚轮监听器:MouseWheelListener 键盘监听器:KeyListener FileInputStream FileOutputStream 1.如何定义事件监听器 2.如何区分每个菜单项 3,如何给菜单项绑定监听器 4.如何将UI中的对象传递给监听器 5,如何读写硬盘中的数据
简单实现读写的记事本, 技术点是swing,awt,io组件的综合应用,代码如下.;
package 监听器的练习0621; import java.awt.Color; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.ScrollPaneConstants; import javax.swing.border.LineBorder; public class Note extends JFrame{ public static void main(String[] args) { Note note = new Note(); note.init(); } public void init(){ this.setTitle("记事本"); this.setSize(500,500); this.setDefaultCloseOperation(3); this.setResizable(false); //创建多行文本 JTextArea area = new JTextArea(); //设置多行编辑区域的边框颜色 LineBorder border= new LineBorder(Color.BLACK); area.setBorder(border); //创建菜单栏 JMenuBar bar = new JMenuBar(); //创建监听器的对象 Notelisetener lis = new Notelisetener(area); //创建Menu String[] menus = { "文件(F)", "编辑(E)", "格式(O)", "查看(V)", "帮助(H)" }; //创建Item String[][] items = {{"新建", "打开", "保存", "另存为", "-", "页面设置", "打印", "-", "退出" }, { "撤销", "-", "剪切", "复制" }, {"txt"}, {"F1"}, {"F1"}}; //设置默认的标示 String[][] commands = {{ "newFile", "open", "save", "saveAs", "-", "pageSet", "print", "-", "exit" }, { "withDraw", "-", "cut", "copy" }, {"txt"}, {"f1"}, {"f1"} }; for(int i =0;i<menus.length;i++){ JMenu menu = new JMenu(menus[i]); bar.add(menu); for(int j = 0;j<items[i].length;j++){ if(items[i][j].equals("-")){ menu.addSeparator(); }else{ JMenuItem item = new JMenuItem(items[i][j]); item.setActionCommand(commands[i][j]); menu.add(item); //添加监听器 item.addActionListener(lis); } } } //将菜单栏增加到窗体上 this.setJMenuBar(bar); //设置滚动条将多行文本放入滚动条中 //创建滚动条的对象,将需要滚动条的组件传到滚动条的对象中 JScrollPane js = new JScrollPane(area); //设置上下滚动和水平滚动 js.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); js.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); this.add(js); this.setVisible(true); } }
package 监听器的练习0621; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.swing.JTextArea; public class Notelisetener implements ActionListener { private JTextArea area; public Notelisetener(JTextArea area){ this.area = area; } @Override public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if(command.equals("save")){ //获取文本区域内的内容 String str = area.getText(); String path = "C:\\Users\\Administrator\\Desktop\\文档.txt"; try { FileOutputStream fos =new FileOutputStream(path); byte[] bs =str.getBytes(); fos.write(bs); fos.flush(); fos.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } }else if(command.equals("open")){ String path1 = "C:\\Users\\Administrator\\Desktop\\文档.txt"; try { FileInputStream fis = new FileInputStream(path1); //获取管道中流的长度 int len= fis.available(); byte[] by = new byte[len]; fis.read(by); String str = new String(by); area.setText(str); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } }