使用Java帶你打造一款簡單的英語學習系統

【一、項目背景】

隨着移動互聯網的發展,英語學習系統能結構化的組織海量資料。針對用戶個性需求,有的放矢地呈現給用戶,從而爲英語學習者提供便利,提升他們的學習效率。

【二、項目目標】

1. 實現美觀的界面,添加需要的組件。

2. 能夠基本實現改變字體,顏色,背景,頁面切換功能。

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

【三、項目實施】

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

接下來,小編帶大家進行具體的實現,具體的實現步驟如下。

【四、實現步驟】

一、首先實現窗體界面

具體的代碼實現過程如下:

public static void main(String[] args){
    // TODO Auto-generated method stub
        EnglishSystem es =new EnglishSystem();
        es.setTitle("英語學習系統");
        es.setSize(750, 600);
        es.setVisible(true);
        es.setResizable(false);
        es.setLocationRelativeTo(null);

  }

使用new關鍵字創建EnglishSystem類;

setTitle表示設置界面的標題;

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

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

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

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

二、實現菜單欄

1. 創建JFrame實例、JPanel面板,然後把面板添加到JFrame中。

2. 創建JMenuBar菜單欄對象,JMenu在創建菜單對象,將菜單對象添加到菜單欄對象中。

3. 將JMenuItem菜單項添加到JMenu中。

public class EnglishSystem extends JFrame {

  private JPanel panel01 = new JPanel();//菜單欄
  private JMenuBar jb = new JMenuBar();
  private JMenu menu01 = new JMenu("字體");
  private JMenuItem item01 = new JMenuItem("宋體");
  private JMenuItem item02 = new JMenuItem("黑體");

  private JMenu menu02 = new JMenu("顏色");
  private JMenuItem item03 = new JMenuItem("玫紅色");
  private JMenuItem item04 = new JMenuItem("藍色");
  private JMenuItem item05 = new JMenuItem("綠色");
  private JMenuItem item06 = new JMenuItem("橘色");
  private JMenuItem item07 = new JMenuItem("黑色");

  private JMenu menu03 = new JMenu("設置");
  private JMenuItem item08 = new JMenuItem("換壁紙");
  private JMenuItem item09 = new JMenuItem("退出");

4. 實現單詞區

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

5. 實現上下頁切換

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

6. 當前背景的圖片

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

7. EnglishSystem類構造函數:構造這個函數主要是實現界面的設計,添加組件。

EnglishSystem(){
    jb.add(menu01);
    jb.add(menu02);
    jb.add(menu03);

    menu01.add(item01);
    menu01.add(item02);

    menu02.add(item03);
    menu02.add(item04);
    menu02.add(item05);
    menu02.add(item06);
    menu02.add(item07);

    menu03.add(item08);
    menu03.add(item09);
    panel01.add(jb);
    this.add(panel01);
    this.setJMenuBar(jb);

    panel03.add(text01);
    text01.setText(str1);
    text01.setEditable(false);
    text01.setLineWrap(true);
    text01.setWrapStyleWord(true);
    panel03.setBorder(new TitledBorder("單詞區"));
    this.add(panel03,BorderLayout.CENTER);
  text01.setFont(new Font("黑體",Font.PLAIN,14));

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

panel04.add(btn_last);
  panel04.add(btn_next);
    this.add(panel04,BorderLayout.SOUTH);

    text01.setOpaque(false);
    panel01.setOpaque(false);
    panel03.setOpaque(false);
    panel04.setOpaque(false);

     label.setBounds(0,0,bg.getIconWidth(),bg.getIconHeight());//設置邊界
        imagePanel=(JPanel)this.getContentPane();//獲取窗體的內容面板
        imagePanel.setOpaque(false);//設置透明
    this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));

9. 定義事件處理類,實現事件監聽器

private MyListener my = new MyListener();

10. 在EnglishSystem構造函數中給指定組件添加監聽

item01.addActionListener(my);
    item02.addActionListener(my);
    item03.addActionListener(my);
    item04.addActionListener(my);
    item05.addActionListener(my);
    item06.addActionListener(my);
    item07.addActionListener(my);
    item08.addActionListener(my);
    item09.addActionListener(my);

    btn_next.addActionListener(my);
    btn_last.addActionListener(my);

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

private class MyListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
      // TODO Auto-generated method stub

      if(e.getSource()==item01){//宋體
        text01.setFont(new Font("宋體",Font.PLAIN,14));
      }  
        if(e.getSource()==item02){//黑體
          text01.setFont(new Font("黑體",Font.PLAIN,14));
        }
        if(e.getSource()==item03){//玫紅色
          text01.setForeground(new Color(255,0,255));
        }
        if(e.getSource()==item04){//藍色
             text01.setForeground(Color.blue);
        }
        if(e.getSource()==item05){//綠色
             text01.setForeground(new Color(0,100,0));
        }
        if(e.getSource()==item06){//橘色
             text01.setForeground(new Color(255,140,0));
        }
        if(e.getSource()==item07){//黑色
             text01.setForeground(Color.BLACK);
    }
if(e.getSource()==item08){//換壁紙
photoNum++;
if(photoNum>=6){
photoNum=1;
}
label.setIcon(new ImageIcon("photo//photo"+photoNum+".png"));
}
if(e.getSource()==item09){//退出
dispose();
}
if(e.getSource()==btn_next){//下一頁
if(papeNum<s.length){//不是最後一頁
papeNum++;
btn_last.setEnabled(true);
btn_next.setEnabled(true);
}
if(papeNum==s.length){
btn_last.setEnabled(true);
btn_next.setEnabled(false);
}
}
if(e.getSource()==btn_last){//上一頁
if(papeNum>1){//不是第一頁
papeNum--;
btn_last.setEnabled(true);
btn_next.setEnabled(true);
}
if(papeNum==1){
btn_last.setEnabled(false);
btn_next.setEnabled(true);
}
}

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

讀取Txt文件:

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

13. 定義一個字符串數組

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"};
private int papeNum=1;//頁數

14. 在getFileContent函數獲取文件內容

private String getFileContent(File file) {//獲取文件內容
       BufferedReader br = null;
       StringBuffer sb = new StringBuffer();
       try {
        br = new BufferedReader(new FileReader(file));
        String hasRead = null;
        while ((hasRead = br.readLine()) != null) {
         sb.append(hasRead + "\n");
        }
       } catch (Exception e) {

       } finally {
        if (br != null) {
         try {
          br.close();
         } catch (IOException e) {

         }
        }
       }
       return sb.toString();
}

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

1. Swing是JAVA的基礎類的一部分。

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

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

【五、總結】

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

2. 事件處理函數的添加,難點是運用理解構造函數、內部類的創建。

3. 如果需要本文源碼,請在公衆號後臺回覆“英語系統”四個字獲取。

看完本文有收穫?請轉發分享給更多的人

IT共享之家

想學習更多Python網絡爬蟲與數據挖掘知識,可前往專業網站:http://pdcfighting.com/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章