JAVA语言课程设计报告
题 目:记事本的制作
设 计 者:
专业班级:
学 号:
2012年 12 月 24 日
目录
1、系统需求分析 3
2.系统总体设计 3
3 系统详细设计 5
4软件测试 13
5 系统总结 13
6系统设计心得体会 13
7参考文献 13
1、系统需求分析
1.1系统名称:
Window记事本
1.2系统介绍:
开发一个window记事本,主要有一个主界面及以下功能:
1:文件的新建,打开,保存,打印,另存等;
2:对文件的编辑,如全选,复制,剪切,粘贴等;
3:对文件的格式操作,如自动换行,字体等;
4:帮助,包括帮助主题,关于。
1.3开发环境
Eclipse
2.系统总体设计
2.1 系统功能结构图
2.2系统文件结构图
2.3系统编辑结构图
2.4系统格式、帮助结构图
3 系统详细设计
3.1.主界面
主界面主要是用于对选择相应的功能进行相应的功能,主界面主要包括文件,编辑,格式,帮助四个下拉菜单功能。
重要代码:
①创建界面,安装各种监听器
public Notebook() {
setTitle("记事本 -- 刘兴钢");
con=getContentPane();
text=new JTextArea();
JSPane=new JScrollPane(text);
createMenu();
createPopupMenu();
setJMenuBar(mainMenuBar);
con.add(JSPane,BorderLayout.CENTER);
text.setComponentPopupMenu(popMenu);
text.getDocument().addDocumentListener(this);
text.addKeyListener(new handleKey());
text.addMouseListener(new handleMouse());
setSize(400,300);
setVisible(true);
② 菜单代码
public void createMenu(){
//创建JMenuBar
mainMenuBar=new JMenuBar();
//创建四个JMenu
fileMenu=new JMenu("文件");
editMenu=new JMenu("编辑");
formatMenu=new JMenu("格式");
helpMenu=new JMenu("帮助");
3.2.字体设计界面
主要代码
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class fontDialog extends JDialog implements ActionListener,ListSelectionListener{
public static final int Cancle=0;
public static final int OK=1;
public static final String [] style={"正常","斜体","粗体","粗斜体"};
public static final String [] size={"8","9","10","11","12","14","16",
"18","20","22","24","26","28","36","48","72"};
private Font userFont=null;
private int userSelect=Cancle;
private JFrame parent=null;
private Container con;
private JScrollPane nameSPane,styleSPane,sizeSPane;
private JPanel panel[];
private JLabel nameLbl,styleLbl,sizeLbl;
private JTextField nameText,styleText,sizeText;
private JList nameList,styleList,sizeList;
private JButton OKBtn,cancleBtn;
public fontDialog() {
this(null);
}
public fontDialog(JFrame owner){
super(owner,true);
parent=owner;
setTitle("字体");
con=getContentPane();
BoxLayout box=new BoxLayout(con,BoxLayout.Y_AXIS);
con.setLayout(box);
panel=new JPanel[4];
for(int i=0;i<3;i++){
panel[i]=new JPanel();
panel[i].setLayout(new GridLayout(1,3));
}
panel[3]=new JPanel();
panel[3].setLayout(new FlowLayout());
nameLbl=new JLabel("字体");
styleLbl=new JLabel("字形");
sizeLbl=new JLabel("大小");
panel[0].add(nameLbl);
panel[0].add(styleLbl);
panel[0].add(sizeLbl);
nameText=new JTextField("宋体");
nameText.setColumns(5);
nameText.setEditable(false);
styleText=new JTextField("正常");
styleText.setColumns(5);
styleText.setEditable(false);
sizeText=new JTextField("12");
sizeText.setColumns(5);
sizeText.setEditable(false);
panel[1].add(nameText);
panel[1].add(styleText);
panel[1].add(sizeText);
GraphicsEnvironment eq = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] availableFonts= eq.getAvailableFontFamilyNames();
nameList=new JList(availableFonts);
nameList.addListSelectionListener(this);
nameSPane=new JScrollPane(nameList);
styleList=new JList(style);
styleList.addListSelectionListener(this);
styleSPane=new JScrollPane(styleList);
sizeList=new JList(size);
sizeList.addListSelectionListener(this);
sizeSPane=new JScrollPane(sizeList);
panel[2].add(nameSPane);
panel[2].add(styleSPane);
panel[2].add(sizeSPane);
OKBtn=new JButton("确定");
OKBtn.addActionListener(this);
cancleBtn=new JButton("取消");
cancleBtn.addActionListener(this);
panel[3].add(OKBtn);
panel[3].add(cancleBtn);
for(int i=0;i<4;i++)
con.add(panel[i]);
}
public int showFontDialog(){
setSize(300,300);
int x,y;
if (parent!=null){
x=parent.getX()+30;
y=parent.getY()+30;
}else{
x=150;
y=100;
}
setLocation(new Point(x,y));
setVisible(true);
return userSelect;
}
public Font getFont(){
return userFont;
}
public void actionPerformed(ActionEvent e){
int styleIndex=Font.PLAIN,fontSize;
if(e.getSource()==OKBtn){
if(styleText.getText().equals("正常"))
styleIndex=Font.PLAIN;
if(styleText.getText().equals("斜体"))
styleIndex=Font.ITALIC;
if(styleText.getText().equals("粗体"))
styleIndex=Font.BOLD;
if(styleText.getText().equals("粗斜体"))
styleIndex=Font.BOLD | Font.ITALIC;
fontSize=Integer.parseInt(sizeText.getText());
userFont=new Font(nameText.getText(),styleIndex,fontSize);
userSelect=OK;
setVisible(false);
}else{
userSelect=Cancle;
setVisible(false);
}
}
public void valueChanged(ListSelectionEvent e){
if (e.getSource()==nameList)
nameText.setText((String)nameList.getSelectedValue());
if (e.getSource()==styleList)
styleText.setText((String)styleList.getSelectedValue());
if (e.getSource()==sizeList)
sizeText.setText((String)sizeList.getSelectedValue());
}
}
3.3.保存界面
代码
int doSave(){
FileOutputStream fout;
byte content[];
int flag;
if (!haveName){
flag = doSaveAs();
}else if(changed){
try{
fout=new FileOutputStream(file);
content=text.getText().getBytes();
fout.write(content);
fout.close();
changed=false;
flag = 1;
}catch(FileNotFoundException e){
JOptionPane.showMessageDialog(this,"指定的文件名称或属性有问题!");
flag = 0;
}catch(IOException e){
JOptionPane.showMessageDialog(this,"无法写文件,请检查文件是否被锁定");
flag = 0;
}
}else{
flag =1;
}
return flag;
}
3.4.打印设计界面