日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
使用Java帶你打造一款簡單的英語學(xué)習(xí)系統(tǒng)

【一、項(xiàng)目背景】

隨著移動(dòng)互聯(lián)網(wǎng)的發(fā)展,英語學(xué)習(xí)系統(tǒng)能結(jié)構(gòu)化的組織海量資料。針對用戶個(gè)性需求,有的放矢地呈現(xiàn)給用戶,從而為英語學(xué)習(xí)者提供便利,提升他們的學(xué)習(xí)效率。

【二、項(xiàng)目目標(biāo)】

1. 實(shí)現(xiàn)美觀的界面,添加需要的組件。

2. 能夠基本實(shí)現(xiàn)改變字體,顏色,背景,頁面切換功能。

3. java讀取txt文件,簡化代碼。

【三、項(xiàng)目實(shí)施】

使用eclipse軟件開發(fā),先上效果圖,如下圖所示。可以看到在界面上有可以改變字體、顏色、設(shè)置選項(xiàng)的菜單欄,頁面切換的功能。

接下來,小編帶大家進(jìn)行具體的實(shí)現(xiàn),具體的實(shí)現(xiàn)步驟如下。

【四、實(shí)現(xiàn)步驟】

一、首先實(shí)現(xiàn)窗體界面

具體的代碼實(shí)現(xiàn)過程如下:

 
 
 
 
  1. public static void main(String[] args){ 
  2.     // TODO Auto-generated method stub 
  3.         EnglishSystem es =new EnglishSystem(); 
  4.         es.setTitle("英語學(xué)習(xí)系統(tǒng)"); 
  5.         es.setSize(750, 600); 
  6.         es.setVisible(true); 
  7.         es.setResizable(false); 
  8.         es.setLocationRelativeTo(null); 
  9.  
  10.   } 

使用new關(guān)鍵字創(chuàng)建EnglishSystem類;

setTitle表示設(shè)置界面的標(biāo)題;

setSize(寬,高)表示窗體大小;

setVisible(true或false)表示窗體是否可見;

setResizable(true或false)表示窗體是否可以由用戶調(diào)整大小;

setLocationRelativeTo()表示設(shè)置窗口相對于指定組件的位置。

二、實(shí)現(xiàn)菜單欄

1. 創(chuàng)建JFrame實(shí)例、JPanel面板,然后把面板添加到JFrame中。

2. 創(chuàng)建JMenuBar菜單欄對象,JMenu在創(chuàng)建菜單對象,將菜單對象添加到菜單欄對象中。

3. 將JMenuItem菜單項(xiàng)添加到JMenu中。

 
 
 
 
  1. public class EnglishSystem extends JFrame { 
  2.  
  3.   private JPanel panel01 = new JPanel();//菜單欄 
  4.   private JMenuBar jb = new JMenuBar(); 
  5.   private JMenu menu01 = new JMenu("字體"); 
  6.   private JMenuItem item01 = new JMenuItem("宋體"); 
  7.   private JMenuItem item02 = new JMenuItem("黑體"); 
  8.  
  9.   private JMenu menu02 = new JMenu("顏色"); 
  10.   private JMenuItem item03 = new JMenuItem("玫紅色"); 
  11.   private JMenuItem item04 = new JMenuItem("藍(lán)色"); 
  12.   private JMenuItem item05 = new JMenuItem("綠色"); 
  13.   private JMenuItem item06 = new JMenuItem("橘色"); 
  14.   private JMenuItem item07 = new JMenuItem("黑色"); 
  15.  
  16.   private JMenu menu03 = new JMenu("設(shè)置"); 
  17.   private JMenuItem item08 = new JMenuItem("換壁紙"); 
  18.   private JMenuItem item09 = new JMenuItem("退出"); 

4. 實(shí)現(xiàn)單詞區(qū)

 
 
 
 
  1. private JPanel panel03 = new JPanel();//單詞顯示 
  2. private  static JTextArea text01 = new JTextArea(30,89); 

5. 實(shí)現(xiàn)上下頁切換

 
 
 
 
  1. private JPanel panel04 = new JPanel(); 
  2. private JButton btn_next = new JButton("下一頁"); 
  3. private JButton btn_last = new JButton("上一頁"); 

6. 當(dāng)前背景的圖片

 
 
 
 
  1. private int photoNum=1;//背景圖數(shù) 
  2.   private JPanel imagePanel; 
  3.   private ImageIcon bg= new ImageIcon("photo//photo"+photoNum+".png");//背景圖 
  4. private JLabel label = new JLabel(bg); 

7. EnglishSystem類構(gòu)造函數(shù):構(gòu)造這個(gè)函數(shù)主要是實(shí)現(xiàn)界面的設(shè)計(jì),添加組件。

 
 
 
 
  1. EnglishSystem(){ 
  2.     jb.add(menu01); 
  3.     jb.add(menu02); 
  4.     jb.add(menu03); 
  5.  
  6.     menu01.add(item01); 
  7.     menu01.add(item02); 
  8.  
  9.     menu02.add(item03); 
  10.     menu02.add(item04); 
  11.     menu02.add(item05); 
  12.     menu02.add(item06); 
  13.     menu02.add(item07); 
  14.  
  15.     menu03.add(item08); 
  16.     menu03.add(item09); 
  17.     panel01.add(jb); 
  18.     this.add(panel01); 
  19.     this.setJMenuBar(jb); 
  20.  
  21.     panel03.add(text01); 
  22.     text01.setText(str1); 
  23.     text01.setEditable(false); 
  24.     text01.setLineWrap(true); 
  25.     text01.setWrapStyleWord(true); 
  26.     panel03.setBorder(new TitledBorder("單詞區(qū)")); 
  27.     this.add(panel03,BorderLayout.CENTER); 
  28.   text01.setFont(new Font("黑體",Font.PLAIN,14)); 

8. 將字體、顏色、背景添加到JMenuBar菜單欄中,字體里面的菜單項(xiàng)如黑體、宋體添加到菜單中。其他顏色、背景添加組件也一樣!

 
 
 
 
  1. panel04.add(btn_last); 
  2.     panel04.add(btn_next); 
  3.     this.add(panel04,BorderLayout.SOUTH); 
  4.  
  5.     text01.setOpaque(false); 
  6.     panel01.setOpaque(false); 
  7.     panel03.setOpaque(false); 
  8.     panel04.setOpaque(false); 
  9.  
  10.      label.setBounds(0,0,bg.getIconWidth(),bg.getIconHeight());//設(shè)置邊界 
  11.         imagePanel=(JPanel)this.getContentPane();//獲取窗體的內(nèi)容面板 
  12.         imagePanel.setOpaque(false);//設(shè)置透明 
  13.     this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE)); 

9. 定義事件處理類,實(shí)現(xiàn)事件監(jiān)聽器

 
 
 
 
  1. private MyListener my = new MyListener(); 

10. 在EnglishSystem構(gòu)造函數(shù)中給指定組件添加監(jiān)聽

 
 
 
 
  1. item01.addActionListener(my); 
  2.     item02.addActionListener(my); 
  3.     item03.addActionListener(my); 
  4.     item04.addActionListener(my); 
  5.     item05.addActionListener(my); 
  6.     item06.addActionListener(my); 
  7.     item07.addActionListener(my); 
  8.     item08.addActionListener(my); 
  9.     item09.addActionListener(my); 
  10.  
  11.     btn_next.addActionListener(my); 
  12.     btn_last.addActionListener(my); 

11. 添加事件監(jiān)聽器MyListener(自己命名)。

 
 
 
 
  1. private class MyListener implements ActionListener{ 
  2.     @Override 
  3.     public void actionPerformed(ActionEvent e) { 
  4.       // TODO Auto-generated method stub 
  5.  
  6.       if(e.getSource()==item01){//宋體 
  7.         text01.setFont(new Font("宋體",Font.PLAIN,14)); 
  8.       }   
  9.         if(e.getSource()==item02){//黑體 
  10.           text01.setFont(new Font("黑體",Font.PLAIN,14)); 
  11.         } 
  12.         if(e.getSource()==item03){//玫紅色 
  13.           text01.setForeground(new Color(255,0,255)); 
  14.         } 
  15.         if(e.getSource()==item04){//藍(lán)色 
  16.              text01.setForeground(Color.blue); 
  17.         } 
  18.         if(e.getSource()==item05){//綠色 
  19.              text01.setForeground(new Color(0,100,0)); 
  20.         } 
  21.         if(e.getSource()==item06){//橘色 
  22.              text01.setForeground(new Color(255,140,0)); 
  23.         } 
  24.         if(e.getSource()==item07){//黑色 
  25.              text01.setForeground(Color.BLACK); 
  26.     } 
  27. if(e.getSource()==item08){//換壁紙 
  28. photoNum++; 
  29. if(photoNum>=6){ 
  30. photoNum=1; 
  31. label.setIcon(new ImageIcon("photo//photo"+photoNum+".png")); 
  32. if(e.getSource()==item09){//退出 
  33. dispose(); 
  34. if(e.getSource()==btn_next){//下一頁 
  35. if(papeNum
  36. papeNum++; 
  37. btn_last.setEnabled(true); 
  38. btn_next.setEnabled(true); 
  39. if(papeNum==s.length){ 
  40. btn_last.setEnabled(true); 
  41. btn_next.setEnabled(false); 
  42. if(e.getSource()==btn_last){//上一頁 
  43. if(papeNum>1){//不是第一頁 
  44. papeNum--; 
  45. btn_last.setEnabled(true); 
  46. btn_next.setEnabled(true); 
  47. if(papeNum==1){ 
  48. btn_last.setEnabled(false); 
  49. btn_next.setEnabled(true); 

12. 程序中顯示文字是以String數(shù)組形式存儲,這種方式比較方便易懂,但卻使得代碼較多。因此,在文字較多情況下,應(yīng)考慮以txt文檔形式存儲故事文字,在程序中讀取文檔內(nèi)容,以顯示在窗口中。

讀取Txt文件:

 
 
 
 
  1. File file = new File(s[papeNum-1]); 
  2.       String str1 = getFileContent(file); 
  3.       text01.setText(str1); 

13. 定義一個(gè)字符串?dāng)?shù)組

 
 
 
 
  1. private String[] s = new   String[]{"resource//s01.txt","resource//s02.txt","resource//s0  3.txt","resource//s04.txt","resource//s05.txt","resource//s06.  txt","resource//s07.txt","resource//s08.txt","resource//s09.tx  t","resource//s10.txt","resource//s11.txt","resource//s12.txt",  "resource//s13.txt","resource//s14.txt"}; 
  2. private int papeNum=1;//頁數(shù) 

14. 在getFileContent函數(shù)獲取文件內(nèi)容

 
 
 
 
  1. private String getFileContent(File file) {//獲取文件內(nèi)容 
  2.        BufferedReader br = null; 
  3.        StringBuffer sb = new StringBuffer(); 
  4.        try { 
  5.         br = new BufferedReader(new FileReader(file)); 
  6.         String hasRead = null; 
  7.         while ((hasRead = br.readLine()) != null) { 
  8.          sb.append(hasRead + "\n"); 
  9.         } 
  10.        } catch (Exception e) { 
  11.  
  12.        } finally { 
  13.         if (br != null) { 
  14.          try { 
  15.           br.close(); 
  16.          } catch (IOException e) { 
  17.  
  18.          } 
  19.         } 
  20.        } 
  21.        return sb.toString(); 

以上用到的組件主要是Java Swing圖形界面開發(fā):

1. Swing是JAVA的基礎(chǔ)類的一部分。

2. Swing包括了圖形用戶界面(GUI)器件如:文本框,按鈕,分隔窗格和表。

3. Swing 提供了許多比 AWT 更好的屏幕顯示元素,使用純 Java 實(shí)現(xiàn),能夠更好的兼容跨平臺運(yùn)行。

【五、總結(jié)】

1. 主要介紹了JPanel、JButton、JLabel、JTextArea、JMenu、JMenuItem等組件的基本使用,以及相應(yīng)的事件處理。

2. 事件處理函數(shù)的添加,難點(diǎn)是運(yùn)用理解構(gòu)造函數(shù)、內(nèi)部類的創(chuàng)建。


網(wǎng)站標(biāo)題:使用Java帶你打造一款簡單的英語學(xué)習(xí)系統(tǒng)
本文URL:http://www.dlmjj.cn/article/djcphsg.html