一、项目背景
随着移动互联网的发展,电子版的书成为现在主流,针对用户的需求,有的放矢地呈现给用户,阅读电子版书,提高他们的学习效率。
二、项目目标
1.实现美观的界面。
2.能够基本实现改变字号、字体、字形、颜色、壁纸等选择,按钮页面切换功能。
3.java读取txt文件,简化代码。
三、项目实施
使用eclipse软件开发,先上效果图,如下图所示。可以看到在界面上有可以改变字号、字体、字形、颜色、设置选项的菜单栏,页面切换的功能。
![](http://daogezhiyuan-article.oss-cn-beijing.aliyuncs.com/win3000/pic/fd861edd-ec7c-4a49-afc6-e5d842450dda.jpg)
接下来,小编带大家进行具体的实现,具体的实现步骤如下。
(一)首先实现窗体界面
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
StoryBook s = new StoryBook();
s.setTitle("小故事大道理");
s.setSize(600,500);
s.setVisible(true);
}
1.使用new关键字创建StoryBook类
setTitle表示设置界面的标题;
setSize(宽,高)表示窗体大小;
setVisible(true或false)表示窗体是否可见;
效果图如下图:
![](http://daogezhiyuan-article.oss-cn-beijing.aliyuncs.com/win3000/pic/9bb9f0ab-6214-4ec5-9e85-8b8c8789fef2.jpg)
(二)实现菜单栏
1.实现菜单栏步骤:
(1)创建JFrame实例、JPanel面板,然后把面板添加到JFrame中。
(2)创建JMenuBar菜单栏对象,JMenu在创建菜单对象,将菜单对象添加到菜单栏对象中。
(3)将JMenuItem菜单项添加到JMenu中。
public class StoryBook extends JFrame {
//成员变量
private static JTextArea text01 = new JTextArea("");
private String style="宋体";
private int pattern = Font.PLAIN;//字形(常规)
private int size=10;//字号
private static String[] str=new String[3];
(4)面板一实现上下页按钮布局
private JPanel panel01=new JPanel();
private JPanel panel02=new JPanel();
private JButton btn_last=new JButton("上一页");
private JButton btn_next=new JButton("下一页");
private int papeNum=1;//当前的页码 1 2 3
(5)菜单栏的字号
private JMenuBar jb = new JMenuBar();
private JMenu fontsize = new JMenu("字号");
private JMenuItem twelf = new JMenuItem("12");
private JMenuItem fiveteen = new JMenuItem("15");
private JMenuItem eighteen = new JMenuItem("18");
private JMenuItem twenty = new JMenuItem("20");
(6)菜单栏的字体
private JMenu font = new JMenu("字体");
private JMenuItem song = new JMenuItem("宋体");
private JMenuItem hei = new JMenuItem("黑体");
private JMenuItem kai = new JMenuItem("楷体");
(7)菜单栏的字形
private JMenu fontstyle = new JMenu("字形");
private JMenuItem chang = new JMenuItem("常规");
private JMenuItem jia = new JMenuItem("加粗");
private JMenuItem qing = new JMenuItem("倾斜");
(8)菜单栏的颜色
private JMenu color = new JMenu("颜色");
private JMenuItem red = new JMenuItem("红色");
private JMenuItem green = new JMenuItem("绿色");
private JMenuItem blue = new JMenuItem("蓝色");
(10)菜单栏的设置
private JMenu intall = new JMenu("设置");
private JMenuItem swap = new JMenuItem("换壁纸");
(11)当前的背景图片
private int photoNum=1;//当前显示背景图的序号
private JPanel imagePanel;
private ImageIcon bg= new ImageIcon("photo//photo"+photoNum+".jpg");//背景图
private JLabel label = new JLabel(bg);
(三)在StoryBook类的构造函数设置组件的属性
1.设置字体
text01.setLineWrap(true);//自动换行
text01.setFont(new Font(style,pattern,size));
this.add(text01,BorderLayout.CENTER);
2.设置当前第一页的上一页的按钮不可以点击,添加上下页按钮到边界布局的南方向
btn_last.setEnabled(false);
btn_next.setEnabled(true);
panel01.add(btn_last);
panel01.add(btn_next);
this.add(panel01,BorderLayout.SOUTH);
3.添加菜单栏的菜单(字号、字体、字形、颜色、设置)
jb.add(fontsize);
jb.add(font);
jb.add(fontstyle);
jb.add(color);
jb.add(intall);
4.添加字号的12、15、18、20
fontsize.add(twelf);
fontsize.add(fiveteen);
fontsize.add(eighteen);
fontsize.add(twenty);
this.setJMenuBar(jb);
效果图如下图:
![](http://daogezhiyuan-article.oss-cn-beijing.aliyuncs.com/win3000/pic/7989acfe-f4e1-4459-9d88-b98b7481cec2.jpg)
5.添加字体的宋体、黑体、楷体
font.add(song);
font.add(hei);
font.add(kai);
效果图如下图:
![](http://daogezhiyuan-article.oss-cn-beijing.aliyuncs.com/win3000/pic/0020e56a-01f9-49e2-8f59-5772f32c1beb.jpg)
6.添加字形的常规、加粗、倾斜
fontstyle.add(chang);
fontstyle.add(jia);
fontstyle.add(qing);
效果图如下图:
![](http://daogezhiyuan-article.oss-cn-beijing.aliyuncs.com/win3000/pic/c302e52c-d8dd-4b46-8247-bbd86732442b.jpg)
7.添加颜色红色、绿色、蓝色
color.add(red);
color.add(green);
color.add(blue);
效果图如下图:
![](http://daogezhiyuan-article.oss-cn-beijing.aliyuncs.com/win3000/pic/f1b65a58-3665-4467-a408-5dc38e11bc85.jpg)
8.添加设置换壁纸
intall.add(swap);
效果图如下图:
![](http://daogezhiyuan-article.oss-cn-beijing.aliyuncs.com/win3000/pic/25a5a225-8c25-428c-9c35-3af6fd6d1f35.jpg)
以上将字号、字体、字形、颜色、设置添加到JMenuBar菜单栏中,字体里面的菜单项如黑体、宋体添加到菜单中。其他字号、字形、颜色、设置添加组件也一样!
接下来实现功能请看手把手教你用Java打造一款简单故事书(下篇)!
四、总结
本文主要介绍了JPanel、JButton、JLabel、JTextArea、JMenu、JMenuItem等组件的基本使用,以及相应的事件处理。这些代码比较简单,也是一个简单的小案例,希望对你的学习有所帮助。下篇文章,我们进行具体的实现,敬请期待!
文章为用户上传,仅供非商业浏览。发布者:Lomu,转转请注明出处: https://www.daogebangong.com/articles/detail/Teach%20you%20to%20create%20a%20simple%20storybook%20with%20Java%20Part%201.html
评论列表(196条)
测试