再做課程設(shè)計(jì)時(shí)遇到的問(wèn)題,及相應(yīng)解決辦法
1.添加背景音樂(lè)(設(shè)計(jì)的界面太單調(diào),以前只是知道一些基本語(yǔ)法,現(xiàn)想提升一下自己)
主要代碼如下:
import java.io.*;
import java.awt.*;
import java.net.MalformedURLException; public static void main(String args[]) {
try {
URL cb;
File f = new File("C:\\Documents and Settings\\Administrator\\桌面\\刀出鞘.wav"); //引號(hào)里面的是音樂(lè)文件所在的絕對(duì)路徑
cb = f.toURL();
AudioClip aau;
aau = Applet.newAudioClip(cb);
//aau.play();
aau.loop();
//循環(huán)播放 aau.play() 單曲 aau.stop()停止播放
}
catch (MalformedURLException e){
e.printStackTrace();
}
}
2.添加背景圖片
主要代碼:
import java.swing.*;
final ImageIcon img=new ImageIcon("jiemian.jpg");
JPanel jpnl =new JPanel(){
public void paintComponent(Graphics g){
g.drawImage(img.getImage(),0,0,580,500,null,null);
super.paintComponent(g);} };
jpnl.setBounds(0,0,587,540);
jpnl.setOpaque(false);
3.鍵盤(pán)輸入識(shí)別
識(shí)別'Enter'鍵,主要代碼:
public void keyPressed(KeyEvent ke){
if(ke.getKeyChar() == ke.VK_ENTER){ //以前不知道,原來(lái)有一個(gè)系統(tǒng)中有專門(mén)的常量對(duì)應(yīng)鍵盤(pán)特殊按鈕
System.out.println ("ok................") ;
}
}
4.按鈕數(shù)組及其響應(yīng) (剛開(kāi)始時(shí)把它想復(fù)雜了)
主要代碼:
JButton btn[]=new JButton[5];
.......
void define(){
btn[0]=new JButton("btn1");
btn[1]=new JButton("btn2");
btn[2]=new JButton("btn3");
btn[3]=new JButton("<html>按鈕四<br>和 按鈕五</html>"); //<html>......<br>可以實(shí)現(xiàn)按鈕上字符串換行顯示的功能
btn[4]=new JButton("btn6");
for(int i=0;i<5;i++)
{
btn[i].addActionListener(new ActListen());
}
}
class ActListen implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton butn=(JButton)e.getSource();
if(butn==btn[1]){
}
else if(butn==btn[2]){
}
else if(butn==btn[0]){
}
else if(butn==btn[3]){
}
else if(butn==btn[4]){
}
}
.........