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.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class Update extends JFrame implements ActionListener{ /** * */ private static final long serialVersionUID = 1L; 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 update; JButton quit; JList jlist; DefaultListModel df; JScrollPane jsp; JTextArea jta; public Update(){ jp1=new JPanel(); jp2=new JPanel(); jp3=new JPanel(); ktv=new JLabel("./src/image/KTV3.jpg"); update=new JButton("修改歌曲"); quit=new JButton("取消"); jta=new JTextArea(18,33); df=new DefaultListModel(); jlist=new JList(df); jsp=new JScrollPane(jlist); jsp.add(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(jsp, BorderLayout.CENTER); jp3.add(update); jp3.add(quit); //对所用的组件加监听 update.addActionListener(this); quit.addActionListener(this); //设置属性 setTitle("修改歌曲"); setIconImage(Toolkit.getDefaultToolkit().createImage("./src/image/KTV8.jpg")); setLocation(300,100); pack(); setVisible(true); if (f.exists()){ list=new Oper().read(); Iterator iter=list.iterator(); df.removeAllElements(); while (iter.hasNext()) df.addElement(iter.next()); } else JOptionPane.showMessageDialog(this, "没有内容"); } public void actionPerformed(ActionEvent e) { if (e.getSource().equals(quit)) setVisible(false); if (e.getActionCommand().equals("修改歌曲")){ for (int j=0;j<df.size();j++){ if (jlist.isSelectedIndex(j)){ String li=df.getElementAt(j).toString(); String s[]=li.split("\t"); String id[]=s[0].split(":"); String name[]=s[1].split(":"); String singer[]=s[2].split(":"); String type[]=s[3].split(":"); String sex[]=s[4].split(":");<span style="white-space:pre"> </span>//这块其实没有搞明白:来干什么,书上是:根据“:”将一条数据进行分割,并赋值给不同的字符串 int id1=Integer.parseInt(id[1]); String name1=name[1]; String singer1=singer[1]; String type1=type[1]; String sex1=sex[1]; new Change(id1,name1,singer1,type1,sex1); } } } } }
这是歌曲修改了,基本和前面都是一个套路,但是书上牵扯到了内部类,然而我始终没有敲的没有错误,然后就自己写在另一个类里
import java.awt.BorderLayout; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Change extends JFrame implements ActionListener{ JPanel jp1,jp2,jp3,jp4; JLabel lname,lsinger,ltype,lsex,lid; JButton jb1,jb2; JTextField tname,tsinger,ttype,tsex,tid; public Change(int id1, String name1, String singer1, String type1, String sex1) { jp1=new JPanel(); jp2=new JPanel(); jp3=new JPanel(); jp4=new JPanel(); lname=new JLabel("歌曲名"); lsinger=new JLabel("歌手姓名"); ltype=new JLabel("类别"); lsex=new JLabel("性别"); lid=new JLabel("id"); tname=new JTextField(18); tname.setText(name1); tsinger=new JTextField(18); tsinger.setText(singer1); ttype=new JTextField(18); ttype.setText(type1); tid=new JTextField(18); tid.setText(id1+""); tid.setEditable(false); tsex=new JTextField(18); tsex.setText(sex1); jb1=new JButton("确定"); jb1.addActionListener(this); jb2=new JButton("取消"); jb2.addActionListener(this); jp1.setLayout(new GridLayout(5, 1)); jp2.setLayout(new GridLayout(5, 1)); jp1.add(lid); jp2.add(tid); jp1.add(lname); jp2.add(tname); jp1.add(ltype); jp2.add(ttype); jp1.add(lsex); jp2.add(tsex); jp1.add(lsinger); jp2.add(tsinger); jp3.add(jb1); jp3.add(jb2); Container con=getContentPane(); con.add(jp1, BorderLayout.NORTH); con.add(jb2, BorderLayout.CENTER); con.add(jp3, BorderLayout.SOUTH); pack(); setVisible(false); setLocation(500, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("确定")){ int id=Integer.parseInt(tid.getText()); String name=tname.getText(); String singer=tsinger.getText(); String type=ttype.getText(); String sex=tsex.getText(); new Oper().updata(id, name, singer, type, sex); setVisible(false); } if (e.getActionCommand().equals("取消")) setVisible(false); } }也是这一块和业务处理层关联,执行功能