刚刚开始学习,新写了一个记事本

package NOTEPAD;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Container;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.BorderFactory;
import javax.swing.JFileChooser;


public class JNotePadUI extends JFrame {
 public JNotePadUI(){
  super("新建文本文件");
  setUpUIComponent();
  setUpEventListener();
  setVisible(true);
 }
 
 // 菜单栏
 private JMenuBar menubar = new JMenuBar();
 
 //设置“文件”,"编辑",“关于”菜单
 private JMenu filemenu = new JMenu("文件");
 private JMenu editmenu = new JMenu("编辑");
 private JMenu aboutmenu = new JMenu("关于");
 
 //设置菜单下的选项
 private JMenuItem menuopen = new JMenuItem("打开");
 private JMenuItem menusave = new JMenuItem("保存");
 private JMenuItem menusaveas = new JMenuItem("另存为");
 private JMenuItem menuclose = new JMenuItem("关闭");
 private JMenuItem filecut = new JMenuItem("剪切");
 private JMenuItem filecopy = new JMenuItem("复制");
 private JMenuItem filepaste = new JMenuItem("粘贴");
 private JMenuItem about = new JMenuItem("关于NOTEPAD");
 
 //加入文件编辑框
 private JTextArea textarea = new JTextArea();
 
 //设置滚动条
 private JScrollPane panel = new JScrollPane(textarea,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
 
 //设置状态栏
 private JLabel statebar = new JLabel("未修改");
 
 private JPopupMenu popupmenu;
 
 private JFileChooser fileChooser = new JFileChooser();
 
 private void setUpUIComponent(){
  setSize(640,480);

  //快捷键设置
  menuopen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
  menusave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
  menuclose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));

  //将子项加入到“文件”下
  filemenu.add(menuopen);
  //设置分隔线
  filemenu.addSeparator();
  filemenu.add(menusave);
  filemenu.add(menusaveas);
  filemenu.addSeparator();
  filemenu.add(menuclose);
  
  filecut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
  filecopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
  filepaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
  
  editmenu.add(filecut);
  editmenu.add(filecopy);
  editmenu.add(filepaste);

  aboutmenu.add(about);
  
  //将文件,编辑,关于加入到菜单栏之中
  menubar.add(filemenu);
  menubar.add(editmenu);
  menubar.add(aboutmenu);
  
  //设置菜单栏
  setJMenuBar(menubar);
  

  //定议文本框字体及大小
  textarea.setFont(new Font("华文彩云",Font.PLAIN,16));
  //设置自动换行
  textarea.setLineWrap(true);

  
  //更新JFrame中的ContainPane
  Container contentpane = getContentPane();
  //将滚动条加入到Container中
  contentpane.add(panel,BorderLayout.CENTER);
  
  

  //设置状态栏中的文字显示的位置
  statebar.setHorizontalAlignment(SwingConstants.LEFT);
  //设置状态栏的界面外观
  statebar.setBorder(BorderFactory.createEmptyBorder());
  //将JLabel实例加入至Container中
  contentpane.add(statebar,BorderLayout.SOUTH);
 }
 

 private void setUpEventListener(){

  //单击窗口关闭按钮事件处理
  addWindowListener(
   new WindowAdapter(){
    public void windowClosing(WindowEvent e){
     closeFile();
    }
   }
  );

  //编辑键盘区事件
  textarea.addKeyListener(
   new KeyAdapter(){
    public void keyTyped(KeyEvent e) {
     processTextArea();
    }
   }
  );
  
  //编辑鼠标区事件
  textarea.addMouseListener(
   new MouseAdapter(){
    public void mouseReleased(MouseEvent e) {
     if(e.getButton()==MouseEvent.BUTTON3)
      popupmenu.show(editmenu,e.getX(),e.getY());
    }
    public void mouseClicked(MouseEvent e){
     if(e.getButton()==MouseEvent.BUTTON1)
      popupmenu.setVisible(true);
    }
   }
  );
  
  //菜单------打开
  menuopen.addActionListener(
   new ActionListener(){
    public void actionPerformed(ActionEvent e){
     openFile();
    }
   }
  );
  
  //菜单------保存
  menusave.addActionListener(
   new ActionListener(){
    public void actionPerformed(ActionEvent e){    //发生操作时调用
     saveFile();
    }
   }
  );
  
  //菜单------另存为
  menusaveas.addActionListener(
   new ActionListener(){
    public void actionPerformed(ActionEvent e){
     saveFileAs();
    }
   }
  );
  
  //菜单------关闭文件
  menuclose.addActionListener(
   new ActionListener(){
    public void actionPerformed(ActionEvent e){
     menuclose();
    }
   }
  );
  
  //菜单------剪切
  filecut.addActionListener(
   new ActionListener(){
    public void actionPerformed(ActionEvent e){
     filecut();
    }
   }
  );
  
  //菜单------复制
  filecopy.addActionListener(
   new ActionListener(){
    public void actionPerformed(ActionEvent e){
     filecopy();
    }
   }
  );
  
  //菜单------粘贴
  filepaste.addActionListener(
   new ActionListener(){
    public void actionPerformed(ActionEvent e){
     filepaste();
    }
   }
  );
  
  //菜单------关于
  about.addActionListener(
   new ActionListener(){
    public void actionPerformed(ActionEvent e){
     about();
    }
   }
  );
 }
 
 //键盘事件的实现
 protected void processTextArea() {
  statebar.setText("已修改");
 }


 protected void about() {
  //关于方法的具体实现过程
  System.out.println("关于");
  JOptionPane.showOptionDialog(null,"程序名称:/n    JNotePad/n作者:/n    张璟/n简介:/n    一个简易的记事本程序/n/n","关于JNotePad",JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE,null,null,null);
 }


 //粘贴方法的实现
 protected void filepaste(){
  textarea.paste();
  statebar.setText("已修改");
  popupmenu.setVisible(false);
 }
 
 //复制方法的实现
 protected void filecopy() {
  textarea.copy();
  popupmenu.setVisible(false);
 }


 //剪切方法的实现
 protected void filecut() {
  textarea.cut();
  statebar.setText("已修改");
  popupmenu.setVisible(false);
 }

 
 //打开文件方法的具体实现
 private void openFile() {
  if(isCurrentFileSaved()){
   open();
  }
  else{
   int option = JOptionPane.showConfirmDialog(null,"文件已修改,是否保存?","保存文件?",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);
   switch(option){
   case JOptionPane.YES_OPTION:    //确认文件保存
    saveFile();
    break;
   case JOptionPane.NO_OPTION:     //放弃文件保存
    open();
    break;
   }
  }
 }  

 private void open() {
  // 打开文件方法的具体实现
  int option = fileChooser.showDialog(null,null);
  if(option == JFileChooser.APPROVE_OPTION){
   try{
    //打开选取的文件
    BufferedReader buf = new BufferedReader(new FileReader(fileChooser.getSelectedFile()));  
    setTitle(fileChooser.getSelectedFile().getName());  //设置文件标题
    textarea.setText("");
    statebar.setText("未修改");
    //取得系统相依的换行字符
    String linseparator = System.getProperty("line.separator");
    //读取文件并附加至文字编辑区
    String text;
    while((text = buf.readLine())!=null){
     textarea.append(text);
     textarea.append(linseparator);
    }
    buf.close();
   }catch(IOException e){
    JOptionPane.showMessageDialog(null,e.toString(),"开启文件失败",JOptionPane.ERROR_MESSAGE);
   }
  }
  
 }


 //关闭文件方法的实现
 protected void menuclose() {
  // TODO Auto-generated method stub
  System.out.println("关闭");
 }


 //另存为方法的实现
 protected void saveFileAs() {
  //显示文件对话框
  int option = fileChooser.showDialog(null,null);
  //如果确认选择文件
  if(option==JFileChooser.APPROVE_OPTION){
   //取得选择的文件
   File file = fileChooser.getSelectedFile();
   //在标题栏上设定取得文件的名称
   setTitle(file.toString());
   try{
    //建立新的文件
    file.createNewFile();
    saveFile();
   }catch(IOException e){
    JOptionPane.showMessageDialog(null,e.toString(),"无法建立新文件",JOptionPane.ERROR_MESSAGE);
   }
  }
 }


 //关闭时方法的实现
 private void closeFile(){
  if(isCurrentFileSaved()){
   dispose();
  }
  else{
   int option = JOptionPane.showConfirmDialog(null,"文件已修改,是否保存?","保存文件?",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);
   
   switch(option){
   case JOptionPane.YES_OPTION:
    saveFile();
    break;
   case JOptionPane.NO_OPTION:
    dispose();
   }
  }
 }
 
 //确定已经保存当前文件方法的实现
 private boolean isCurrentFileSaved() {
  if(statebar.getText().equals("未修改")){
   return true;
  }
  else{
   return false;
  }
 }

 //保存文件的流程处理
 private void saveFile(){
  File file =new File(getTitle());   //从标题栏取得文件名称
  
  if(!file.exists()){
   saveFileAs();
  }
  else{
   try{
    //打开指定的文件
    BufferedWriter buf = new BufferedWriter(new FileWriter(file));
    //将文字编辑区内的文字写入文件
    buf.write(textarea.getText());
    buf.close();
    statebar.setText("未修改");
   }catch(IOException e){
    JOptionPane.showMessageDialog(null,e.toString(),"写入文件失败",JOptionPane.ERROR_MESSAGE);
   }
  }
 }
 
 public static void main(String[] args){
  new JNotePadUI();
 }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你可以使用记事本编写小游戏代码,首先打开记事本,然后输入代码并保存为 .py 文件。在编写代码时,你需要了解编程语言和游戏设计的基础知识。你可以在网上搜索相关的教程和资料,或者参考一些开源的小游戏代码来学习。 ### 回答2: 使用记事本编写小游戏代码需要遵循以下步骤: 1. 打开记事本应用程序,可以在Windows操作系统中通过点击“开始”按钮,搜索“记事本”来找到它。 2. 在记事本中创建一个新文件,点击“文件”菜单,选择“新建”。 3. 在新建文件中,输入你的小游戏代码。小游戏代码可以是任何编程语言,如Python、Java、C++等。你可以参考在线的编程教程或书籍中的示例代码,在记事本中逐行输入代码。 4. 保存你的代码文件,点击“文件”菜单,选择“保存”。选择一个合适的文件名,并为文件选择一个合适的文件类型,以 .py/.java/.cpp 为扩展名分别表示Python/Java/C++代码文件。 5. 运行你的小游戏代码,通过在记事本中点击“文件”菜单然后选择“运行”,或者按下键盘上的相应快捷键来运行代码。请注意,记事本并没有集成开发环境(IDE)的功能,因此你可能需要在系统的命令行界面或者其他编程环境中调用运行你的代码文件。 6. 测试你的小游戏代码,观察输出、运行速度和交互效果等,如果需要,可以对代码进行调试和修改,然后保存并重新运行。 使用记事本编写小游戏代码相较于使用专业的代码编辑器会更为基础和简单。但记事本并不提供代码自动补全、语法高亮等功能,因此对于大型项目而言,推荐使用专业的集成开发环境(IDE)。 ### 回答3: 要用记事本写小游戏代码,首先需要学习基本的编程语言知识,例如Python或JavaScript。这些语言都有自己的语法和规则,需要我们理解和掌握。 开始写代码前,可以先确定游戏的主题和基本规则,例如是否是一个文本冒险游戏或迷宫游戏。然后,我们可以使用记事本打开一个新的文本文件,并将其保存为一个具有合适扩展名的文件(例如.py或.js),以便识别该文件是代码文件。 在记事本中,我们可以按照语言的语法规则来编写代码。例如,在Python中,我们可以使用if/else语句来处理条件判断,使用for循环来进行重复操作,使用函数来组织代码等等。我们可以根据游戏逻辑和需求,逐步编写代码,实现游戏的基本功能。 编写代码时需要注意语法的正确性,如括号的配对、缩进的规范等。如果遇到错误,记事本常常无法提供像IDE(集成开发环境)那样的实时错误提示,因此需要仔细检查代码并查找错误。 当我们完成编写后,可以保存代码文件,并使用适当的命令行工具来运行代码。在命令行中输入相应的命令,即可运行游戏。如果代码中有错误,命令行会显示相应的错误信息,我们需要根据提示进行修改。 使用记事本写小游戏代码需要耐心和仔细,因为记事本并不像IDE那样提供编码辅助和自动完成功能。但通过理解编程语言的基础知识,熟悉语法规则,我们仍然可以用记事本编写而运行小游戏代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值