小KTV学习-3

来源:互联网 发布:解析域名的方法 编辑:程序博客网 时间:2024/04/29 09:38

好吧,今晚敲的是歌曲查找界面,然后数字标的那行都是不知道的组件

import java.awt.BorderLayout;import java.awt.Container;import java.awt.Dimension;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.util.Iterator;import java.util.List;import java.util.Vector;import javax.swing.DefaultListModel;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;public class Search extends JFrame implements ActionListener{List<Song> list=new Vector<Song>();Oper op=new Oper();File f=new File("F:Summer/java2/KTV.data");//声明所需组件JPanel jp1;JPanel jp2;JPanel jp3;JLabel ktv;JButton delete;JButton quit;JList jlist;//1<span style="line-height: 24px; text-indent: 24px;">显示对象列表并且允许用户选择一个或多个项的组件。单独的模型 </span><code style="line-height: 24px; text-indent: 24px;">ListModel</code><span style="line-height: 24px; text-indent: 24px;"> 维护列表的内容。</span>DefaultListModel df;<span style="white-space:pre"></span>//2<span style="line-height: 24px; text-indent: 24px;">此类以松散方式实现 </span><code style="line-height: 24px; text-indent: 24px;">java.util.Vector</code><span style="line-height: 24px; text-indent: 24px;"> API,它实现 1.1.x 版本的 </span><code style="line-height: 24px; text-indent: 24px;">java.util.Vector</code><span style="line-height: 24px; text-indent: 24px;">,没有 collection 类支持,并且在发生更改时通知 </span><code style="line-height: 24px; text-indent: 24px;">ListDataListener</code><span style="line-height: 24px; text-indent: 24px;">。目前,它委托给一个 </span><code style="line-height: 24px; text-indent: 24px;">Vector</code><span style="line-height: 24px; text-indent: 24px;">,在将来的版本中,它将是一个真正的 Collection 实现。</span>JScrollPane jsp;//3<span style="color: rgb(51, 51, 51); font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 24px; text-indent: 28px;">JScrollPane类可提供轻量级组件的 scrollable 视图。JScrollPane 管理视口、可选的垂直和水平滚动条以及可选的行和列标题视口。</span>JTextArea jta;//4<span style="color: rgb(51, 51, 51); font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 24px; text-indent: 28px;">TextArea 是一个显示纯文本的多行区域。</span>JTextField jtf;JMenu jm;//5<span style="line-height: 24px; text-indent: 24px;">菜单的该实现是一个包含 </span><code style="line-height: 24px; text-indent: 24px;">JMenuItem</code><span style="line-height: 24px; text-indent: 24px;"> 的弹出窗口,用户选择 </span><code style="line-height: 24px; text-indent: 24px;">JMenuBar</code><span style="line-height: 24px; text-indent: 24px;"> 上的项时会显示该 JMenuItem。除 </span><code style="line-height: 24px; text-indent: 24px;">JMenuItem</code><span style="line-height: 24px; text-indent: 24px;"> 之外,</span><code style="line-height: 24px; text-indent: 24px;">JMenu</code><span style="line-height: 24px; text-indent: 24px;"> 还可以包含 </span><code style="line-height: 24px; text-indent: 24px;">JSeparator</code><span style="line-height: 24px; text-indent: 24px;">。</span>JMenuBar jmb;JMenuItem song;JMenuItem singer;JMenuItem type;JMenuItem sex;public Search(){//创建所需组件this.jp1=new JPanel();this.jp2=new JPanel();this.jp3=new JPanel();this.delete=new JButton("删除");this.quit=new JButton("取消");this.jtf=new JTextField(18);this.jmb=new JMenuBar();this.jm=new JMenu("分类查询");this.song=new JMenuItem("按歌名");this.singer=new JMenuItem("按歌手名");this.type=new JMenuItem("按类别");this.sex=new JMenuItem("按性别");this.ktv=new JLabel(new ImageIcon("./src/image/KTV4.jpg"));this.jta=new JTextArea(18, 33);this.df=new DefaultListModel();this.jlist=new JList(df);this.jsp=new JScrollPane(jlist);this.jsp.add(this.jta);//获得容器Container con=getContentPane();//添加组件con.add(jp1, BorderLayout.NORTH);con.add(jp2, BorderLayout.CENTER);con.add(jp3, BorderLayout.SOUTH);jsp.setPreferredSize(new Dimension(390, 200));jp1.add(ktv);jp2.add(jtf);jp3.add(jmb);jmb.add(jm);jm.add(song);jm.add(singer);jm.add(type);jm.add(sex);jp2.add(quit);jp3.add(jsp, BorderLayout.CENTER);//对所用的组件加监听song.addActionListener(this);singer.addActionListener(this);type.addActionListener(this);sex.addActionListener(this);delete.addActionListener(this);quit.addActionListener(this);//设置属性setTitle("歌曲查询");setIconImage(Toolkit.getDefaultToolkit().createImage("./src/image/KTV10.jpg"));setLocation(300, 100);pack();setVisible(true);//将所有输出到显示窗口if (f.exists()){list=new Oper().read();Iterator iter=list.iterator();//Iterator?df.removeAllElements();while (iter.hasNext()){df.addElement(iter.next());}}elseJOptionPane.showMessageDialog(this, "没有内容");}public void actionPerformed(ActionEvent e) {if (e.getSource()==quit)setVisible(false);if (e.getActionCommand().equals("按歌名"))lookType(e.getActionCommand());if (e.getActionCommand().equals("按歌手名"))lookType("按歌手名");if (e.getActionCommand().equals("按性别"))lookType("按性别");if (e.getActionCommand().equals("按类别"))lookType("按类别");}public void lookType(String str) {String s=jtf.getText();List<Song> list2=new Vector<Song>();if (str.equals("按歌名")){df.removeAllElements();if (f.exists())list2=op.searchName(s);elseJOptionPane.showMessageDialog(this, "没有 内容");}if (str.equals("按歌手名")){df.removeAllElements();if (f.exists())list2=op.searchSinger(s);elseJOptionPane.showMessageDialog(this, "没有 内容");}if (str.equals("按类别")){df.removeAllElements();if (f.exists())list2=op.searchType(s);elseJOptionPane.showMessageDialog(this, "没有 内容");}if (str.equals("按性别")){df.removeAllElements();if (f.exists())list2=op.searchSex(s);elseJOptionPane.showMessageDialog(this, "没有 内容");}Iterator iter=list2.iterator();while (iter.hasNext())df.addElement(iter.next());list2=null;}}

要学习的东西真的好多。

迭代器(Iterator)

  迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构。迭代器通常被称为“轻量级”对象,因为创建它的代价小。

  Java中的Iterator功能比较简单,并且只能单向移动:

  (1) 使用方法iterator()要求容器返回一个Iterator。第一次调用Iterator的next()方法时,它返回序列的第一个元素。注意:iterator()方法是java.lang.Iterable接口,被Collection继承。

  (2) 使用next()获得序列中的下一个元素。

  (3) 使用hasNext()检查序列中是否还有元素。

  (4) 使用remove()将迭代器新返回的元素删除。

  Iterator是Java迭代器最简单的实现,为List设计的ListIterator具有更多的功能,它可以从两个方向遍历List,也可以从List中插入和删除元素。


继续。
0 0
原创粉丝点击