西安文理学院数学与计算机工程学院 课程设计报告
“关于”对话框
4.2 源程序
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*;
class mynotepad extends JFrame{
File file=null; Color color=Color.red; mynotepad(){
initTextContent(); //初始化组件 initMenu(); initAboutDialog();
}
void initTextContent(){
getContentPane().add(new JScrollPane(content)); //对象
}
- 10 -
滚动面板
西安文理学院数学与计算机工程学院 课程设计报告
JTextPane content=new JTextPane(); / /创建/内容输入区
JFileChooser openfile=new JFileChooser(); //创建打开文件对话框
JColorChooser opencolor=new JColorChooser(); //颜色选择 JDialog about=new JDialog(this); //关于对话框 JMenuBar menu=new JMenuBar(); ////菜单栏的各个菜单
JMenu[] menus=new JMenu[]{ new JMenu(\文件\new JMenu(\编辑\ new JMenu(\关于\};
//“文件”菜单项的四个下
JMenuItem optionofmenu[][]=new JMenuItem[][]{{
new JMenuItem(\新建\new JMenuItem(\打开\new JMenuItem(\保存\new JMenuItem(\退出\},
//编辑菜单的四个下拉菜单 {
new JMenuItem(\复制\new JMenuItem(\剪切\new JMenuItem(\粘贴\new JMenuItem(\颜色\},
- 11 - 创建菜单栏
西安文理学院数学与计算机工程学院 课程设计报告
{
new JMenuItem(\关于\} };
void initMenu(){
for(int i=0;i for(int j=0;j optionofmenu[i][j].addActionListener( action ); } this.setJMenuBar(menu); } ActionListener action=new ActionListener(){ //添加事件监听 public void actionPerformed(ActionEvent e){ String name = e.getActionCommand(); JMenuItem MI=(JMenuItem)e.getSource(); if(\新建\监听“新建”事件 content.setText(\ file=null; } else if(\打开\监听“打开”事件 if(file !=null)openfile.setSelectedFile(file); int returnVal=openfile.showOpenDialog(mynotepad.this); if(returnVal==JFileChooser.APPROVE_OPTION){ file=openfile.getSelectedFile(); //打开选择文件 unfold(); } } - 12 - 西安文理学院数学与计算机工程学院 课程设计报告 else if(\保存\监听“保存”事件 if(file!=null) openfile.setSelectedFile(file); int returnVal=openfile.showSaveDialog(mynotepad.this); if(returnVal==JFileChooser.APPROVE_OPTION){ file=openfile.getSelectedFile(); saving(); } } else if(\退出\ mynotepad f=new mynotepad(); int s=JOptionPane.showConfirmDialog(f,\退出?\退出 \提示用户是否退出 if(s==JOptionPane.YES_OPTION) System.exit(0); } else if(\剪切\监听“剪切”事 件 content.cut(); } else if(\复制\监听“复制” 事件 content.copy(); } else if(\粘贴\监听“粘贴”事 件 content.paste(); } else if(\颜色\监听“颜色”事 - 13 -
相关推荐: