小KTV学习-2
晚上剛才敲了一個這個start顯示界面的窗口類和添加歌曲界面
import java.applet.Applet; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.GridLayout; import java.awt.Toolkit;import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JProgressBar; import javax.swing.WindowConstants;public class Start extends JFrame{Thread t;int n=0;JLabel xuan=null;JLabel wait=null;JLabel inserthead=null;JPanel jp=null;JProgressBar bar;boolean f=true;public Start(){//創建所需組建this.jp=new JPanel();this.xuan=new JLabel(new ImageIcon("./src/image/KTV1.jpg")); //KTV圖片鏈接this.inserthead=new JLabel(new ImageIcon("./src/image/KTV2.jpg")); this.wait=new JLabel(new ImageIcon("./src/image/KTV3.jpg")); this.bar=new JProgressBar();bar.setBackground(Color.red);bar.setForeground(Color.black);bar.setStringPainted(true);//設置進度條,使用了多線程!使用多線程的方式設置進入主界面的進度條,在進度條讀取完畢后進入主界面t=new Thread(new Runnable() {public void run() {try {Thread.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}for (int i=0;i<101;i++){bar.setValue(i);n++;try {Thread.sleep(43);} catch (InterruptedException e) {e.printStackTrace();}if (i==100){setVisible(false);new Applet();}}}});t.start();//獲得容器Container con=this.getContentPane();jp.setLayout(new GridLayout(3, 1));//添加組件con.add(xuan);con.add(jp, BorderLayout.SOUTH);jp.add(bar);jp.add(inserthead);jp.add(wait); //就是定義一個容器,把組建都放到這里面//設置屬性setTitle("NO.1KTV選歌系統");setIconImage(Toolkit.getDefaultToolkit().createImage("./src/image/KTV4.jpg")); //載入KTV正面圖片setSize(500, 450);setLocation(200,100);setVisible(true);//窗口關閉setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);} } 首先學習了解到一個定義一個類,里面的屬性在構造函數一開始定義里面都進行了功能的定義,就是上面所說的創建所需組建。下面又用到了多線程Runnable(里面的run),這里sleep是
1.可以調用Thread類的靜態方法: public static void sleep (long millis) thorows InterruptedException 使得當前線程休眠(暫時停止執行millis毫秒) 2.由于是靜態方法,sleep可以由類名.直接調用:Thread.sleep(…) t.start()啟動多線程然后就有容器里面加組建
//獲得容器 Container con=this.getContentPane(); jp.setLayout(new GridLayout(3, 1)); //添加組件 con.add(xuan); con.add(jp, BorderLayout.SOUTH); jp.add(bar); jp.add(inserthead); jp.add(wait);當然另外還回憶了下導入圖片路徑
setIconImage(Toolkit.getDefaultToolkit().createImage("./src/image/KTV4.jpg")); //載入KTV正面圖片import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.image.LookupOp;import javax.swing.DefaultListModel; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField;public class Add extends JFrame implements ActionListener{//聲明組建JPanel jp1,jp2,jp3,jp4,jp5,jp6;JButton jb1,jb2;JLabel jktv,jid,jname,jsinger,jsex,jtype;JTextField tid,tname,tsinger,tsex,ttype;JLabel song;JList jlist;DefaultListModel df;JScrollPane jsp;JTextArea jta;public Add(int id){//創建出所需要組件this.jp1=new JPanel();this.jp2=new JPanel();this.jp3=new JPanel();this.jp4=new JPanel();this.jp5=new JPanel();this.jp6=new JPanel();this.jktv=new JLabel(new ImageIcon("./src/image/KTV6.jpg"));this.jid=new JLabel("歌曲 id:");this.jname=new JLabel("歌曲名稱");this.jsinger=new JLabel("歌手名稱");this.jsex=new JLabel("歌手性別");this.jtype=new JLabel("歌曲類型");this.tname=new JTextField(18);this.tsinger=new JTextField(18);this.tsex=new JTextField(18);this.tid=new JTextField(3);this.song=new JLabel("歌曲信息");this.jta=new JTextArea(18,33);this.df=new DefaultListModel();this.jlist=new JList(this.df);this.jsp=new JScrollPane(this.jlist);this.jsp.add(this.jta);//獲得容器Container con=this.getContentPane();con.add(jp4, BorderLayout.NORTH);con.add(jp5, BorderLayout.CENTER);con.add(jp6, BorderLayout.SOUTH);//添加組件jp4.add(jktv);jp5.add(jp1, BorderLayout.WEST);jp5.add(jp2, BorderLayout.CENTER);jp5.add(jp3, BorderLayout.EAST);jsp.setPreferredSize(new Dimension(390, 200));jp6.add(jsp, BorderLayout.CENTER);tid.setText(id+"");tid.setEditable(false);ttype=new JTextField(18);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(jid);jp2.add(tid);jp1.add(jname);jp2.add(tname);jp1.add(jsinger);jp2.add(tsinger);jp1.add(jsex);jp2.add(tsex);jp1.add(jtype);jp2.add(jtype);jp3.add(jb1);jp3.add(jb2);//設置屬性setTitle("添加歌曲");setIconImage(Toolkit.getDefaultToolkit().createImage("./src/image/KTV8.jpg"));setLocation(300, 100);pack();setVisible(true);}public void actionPerformed(ActionEvent e) {if (e.getActionCommand().equals("確定")){int id=Integer.parseInt(tid.getText());String name=tname.getText();String singer=tsinger.getText();String sex=tsex.getText();String type=ttype.getText();new Oper().add(id, name, singer, type, sex);setVisible(false);}if (e.getActionCommand().equals("取消"))setVisible(false);}}
這里的添加歌曲界面,我的天,全都是組建有關的操作,簡直了,不認識的有
還是自己做的少,沒用過,不看API,不了解吶。
但是進一步認知了
public void actionPerformed(ActionEvent e) {if (e.getActionCommand().equals("確定")){int id=Integer.parseInt(tid.getText());String name=tname.getText();String singer=tsinger.getText();String sex=tsex.getText();String type=ttype.getText();new Oper().add(id, name, singer, type, sex);setVisible(false);}if (e.getActionCommand().equals("取消"))setVisible(false);}得到getText()然后經過Oper()進入管理層的里面找到執行功能函數哦這樣子就把界面層和業務處理層聯系起來了!get
總結
- 上一篇: 大数据挖掘企业服务平台-道路运输安全大数
- 下一篇: 最新PMO项目管理OKR案例库