【一、项目背景】
随着移动互联网的发展,英语学习系统能结构化的组织海量资料。针对用户个性需求,有的放矢地呈现给用户,从而为英语学习者提供便利,提升他们的学习效率。
【二、项目目标】
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共享之家

------------------- End -------------------










