mini记事本

大笑

/*  
这是大一写的记事本,虽然功能不全,但可以用。虽然自己依然弱鸡,但是木有关系的啦,主要是赚积分啊。哈哈哈哈哈
自己好像不会在这里传文件,所以就把源码贴出来。
项目一共有五个java文件。
分别是:
MenuItemFuncationRealize.java
NodeBase.java
NodePand.java
PopupRun.java
RightMouseMenu.java
每个文件分工还是明确的,只不过没有UML图。
*/


//MenuItemFuncationRealize.java的源码:
package notebookPackage;
import   java.awt.*;
import   java.awt.event.*;
import   javax.swing.*;
import   java.io.*;
public class MenuItemFuncationRealize {


JFrame   noteframe ;
public  MenuItemFuncationRealize(JFrame  frame , JTextArea  ta)
{
noteframe =frame;
savePanelArea = ta ;
}

public  MenuItemFuncationRealize()
{

}

//New按钮的属性
JDialog   NewDialog ;
JLabel   NewLabel ;
JTextArea   NewText ;
JButton  NewYes , NewCancel ;
static  File  titleFile;
//注意static 关建字:我找了一个月的错.......
FileOutputStream  connectTitleFile;
OutputStreamWriter   connectFileOutputStream;
JTextArea   savePanelArea ;//save的
static  boolean   notBuildFileAndSave = false;


//New的功能(新建文件的功能)
public    void   NewFuncation()
{
NewDialog  =  new   JDialog(noteframe  , "新建" , true);
NewLabel   =  new   JLabel("新建文件名:");
NewText    =  new   JTextArea(0 , 10);
NewYes     =  new   JButton("新建");
NewCancel  =  new   JButton("取消");
NewDialog.setLayout(new  GridLayout(2 ,2));


NewDialog.add(NewLabel);
NewDialog.add(NewText);
NewDialog.add(NewYes);
NewDialog.add(NewCancel);
NewYes.addMouseListener(new  NewYesGo());
NewCancel.addMouseListener(new  NewCancelGo());

NewDialog.setSize(250, 130);
NewDialog.setLocationRelativeTo(null);
//setLocationRelativeTo()一定要跟在setSize后面才有用,原因不知。
NewDialog.setVisible(true);




NewDialog.addWindowListener( new  WindowAdapter()
{
public  void  wondowClosing(WindowEvent   e)
{
NewDialog.dispose();
}
});



}




public   class   NewYesGo   extends  MouseAdapter
{
boolean   nameFileTest = false ;
public  void  mouseReleased(MouseEvent   e)
{
if(NewText.getText().trim().equals(""))      //trim是去掉字符串空格     equals比较俩字符串
{
NewText.setText("");
}
else
{
titleFile = new File("/" , NewText.getText()+".txt");
   nameFileTest = true;
   try
   {
    if(!titleFile.exists())
    titleFile.createNewFile();
   }
   catch(FileNotFoundException  m)
   {
    //m.printStackTrace();
    BuildErrowDialog();
   }
   catch(IOException  u)
   {
    //u.printStackTrace();
    BuildErrowDialog();
   }
}

notBuildFileAndSave = true;
if(nameFileTest == true)
NewDialog.setVisible(false);
}
}

//文件创建失败提示框
public  void  BuildErrowDialog()
{
JOptionPane.showMessageDialog(noteframe , "文件创建失败" ,"错误" , JOptionPane.ERROR_MESSAGE);
}


final   class   NewCancelGo   extends   MouseAdapter
{

public  void   mouseReleased(MouseEvent   e)
{
NewText.setText("");
NewDialog.setVisible(false);    //关闭的方法
}
}

//实现了New的方法,表扬自己哈哈.....





//save方法的实现


public   void    saveFuncation()
{

if(notBuildFileAndSave == true)
{
try
{
connectTitleFile = new  FileOutputStream(titleFile);
}
catch(FileNotFoundException  f)
{
f.printStackTrace();
}

connectFileOutputStream = new  OutputStreamWriter(connectTitleFile);

String   saveAreaContent ;
saveAreaContent = savePanelArea.getText();
try {
connectFileOutputStream.write(saveAreaContent);
connectFileOutputStream.flush();
} catch (NullPointerException e) {
// TODO 自动生成的 catch 块
//e.printStackTrace();
saveFieldFailed();
System.out.print("error");
}
catch(IOException  m)
{
saveFieldFailed();
}

}
else
{
quickSave();
}
}

//文件保存失败后的提示框

protected    void   saveFieldFailed()
{

JOptionPane.showMessageDialog(noteframe, "文件保存失败" ,  "错误" , JOptionPane.ERROR_MESSAGE);

}
//直接保存提示
protected void  quickSave()
{
JOptionPane.showMessageDialog(noteframe, "请先创建文件" , "错误" , JOptionPane.ERROR_MESSAGE);
}

//打开功能实现
public  void  openFuncation()
{
JFileChooser  openFc = new  JFileChooser();
openFc.showOpenDialog(noteframe);
}


//退出功能实现
public  void  ExitFuncation()
{

noteframe.dispose();
System.exit(0);
}


protected  void  aboveFuncation()
{
String  helpMessage = "如果你在使用该记事本的过程中遇到了问题\n请电话连系这位苦逼的程序员(单身)\n连系电话:18846791758";
JOptionPane.showMessageDialog(noteframe, helpMessage , "信息" , JOptionPane.INFORMATION_MESSAGE );
}
}




//NodeBase.java的源码:
package notebookPackage;
import   java.awt.*;
import   java.awt.event.*;
import   javax.swing.*;
public class NoteBase {


JFrame   noteFrame = new  JFrame("迷你记事本");
JMenuBar  menuBar ;
JMenu     menu ;
JMenu     help;
JMenuItem    New , Open , Save  , Exit , Above ;
JPanel   TextPanel;
public  JTextArea   PanelArea ;


public    NoteBase()
{
menuBar = new  JMenuBar();
menu = new  JMenu("文件(F)");
help = new  JMenu("帮助");
New  = new  JMenuItem("新建(N)" , KeyEvent.VK_N);
Open = new  JMenuItem("打开" , KeyEvent.VK_O);
Save = new  JMenuItem("保存(S)..." , KeyEvent.VK_S);
Exit = new  JMenuItem("退出(E)" , KeyEvent.VK_E);
Above = new JMenuItem("关于我...");
TextPanel  =  new  JPanel();
PanelArea  =  new  JTextArea(21 , 62);
PanelArea.setEditable(true);



menuBar.add(menu);
menuBar.add(help);
menu.add(New);
menu.add(Open);
menu.add(Save);
menu.addSeparator();
menu.add(Exit);
help.add(Above);
noteFrame.setJMenuBar(menuBar);
TextPanel.add(PanelArea);


New.addMouseListener( new  NewMouseListener());
Open.addMouseListener( new  OpenMouseListener());
Save.addMouseListener(new  SaveMouseListener());
Exit.addMouseListener( new  ExitMouseListener());
Above.addMouseListener(new  aboveMouseListener());



noteFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
noteFrame.getContentPane().add(TextPanel, BorderLayout.WEST);
noteFrame.setVisible(true);
noteFrame.setSize(712, 400);
noteFrame.setEnabled(true);
//noteFrame.setResizable(false);
noteFrame.setLocationRelativeTo(null);
}



protected  class  NewMouseListener    extends  MouseAdapter
{
public    void   mouseReleased(MouseEvent   e)
{

MenuItemFuncationRealize    mifr = new  MenuItemFuncationRealize(noteFrame , PanelArea);
mifr.NewFuncation();
}
}


protected  class  OpenMouseListener   extends   MouseAdapter
{
public    void   mouseReleased(MouseEvent    e)
{
//等待
MenuItemFuncationRealize    mifr = new  MenuItemFuncationRealize(noteFrame , PanelArea);
mifr.openFuncation();
}
}


protected  class  SaveMouseListener    extends   MouseAdapter
{
public    void   mouseReleased(MouseEvent     e)
{
MenuItemFuncationRealize    mifr = new  MenuItemFuncationRealize(noteFrame , PanelArea);
mifr.saveFuncation();
}
}


protected   class   ExitMouseListener    extends   MouseAdapter
{
public   void   mouseReleased(MouseEvent       e)
{
MenuItemFuncationRealize    mifr = new  MenuItemFuncationRealize(noteFrame , PanelArea);
mifr.ExitFuncation();
}
}

protected  class  aboveMouseListener   extends  MouseAdapter
{
public  void  mouseReleased(MouseEvent   e)
{
MenuItemFuncationRealize    mifr = new  MenuItemFuncationRealize(noteFrame , PanelArea);
mifr.aboveFuncation();
}
}
}




//NodePand.java的源码:
package notebookPackage;


public class NotePand {


public   static  void  main(String[]  args)
{
NoteBase  nb = new  NoteBase();
//Runnable  r = new PopupRun();
//Thread  t = new  Thread(r);
//t.start();
RightMouseMenu pim = new RightMouseMenu();
}
}



//PopupRun.java的源码:
package notebookPackage;
/*
public class PopupRun   implements  Runnable{


public  void  run()
{
popupIsMouse  rm = new popupIsMouse();
}
}
*/



//RightMouseMenu.java的源码:
package notebookPackage;
import   java.awt.event.*;
import   javax.swing.*;
class RightMouseMenu extends  MenuItemFuncationRealize  implements  ActionListener {//此处继承接口ActionListener , menuItem.addActionListener(this)才能用


JPopupMenu  popup = new  JPopupMenu();
//创建右键菜单
protected  RightMouseMenu()
{
super();
JMenuItem  menuItem = new  JMenuItem("新建" , KeyEvent.VK_N);
menuItem.addActionListener(this);
menuItem.setActionCommand("new");
popup.add(menuItem);
menuItem = new  JMenuItem("打开",KeyEvent.VK_O);
menuItem.addActionListener(this);
menuItem.setActionCommand("open");
popup.add(menuItem);
menuItem  = new  JMenuItem("保存" , KeyEvent.VK_S);
menuItem.setActionCommand("save");
menuItem.addActionListener(this);
popup.add(menuItem);
popup.addSeparator();
menuItem = new  JMenuItem("退出", KeyEvent.VK_E);
menuItem.addActionListener(this);
menuItem.setActionCommand("exit");
popup.add(menuItem);
}

public  void  actionPerformed(ActionEvent  e)
{
if(e.getActionCommand() == "new")
{
super.NewFuncation();
}

if(e.getActionCommand() == "open")
{
super.openFuncation();
}

if(e.getActionCommand() == "save")
{
super.saveFuncation();
}

if(e.getActionCommand() == "exit")
{
super.ExitFuncation();
}
}

}






class popupIsMouse   extends  RightMouseMenu  implements  MouseListener{




popupIsMouse()
{
super();
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO 自动生成的方法存根
maybeShowPopup(e);
}


@Override
public void mousePressed(MouseEvent e) {
// TODO 自动生成的方法存根
maybeShowPopup(e);
}


@Override
public void mouseReleased(MouseEvent e) {
// TODO 自动生成的方法存根
maybeShowPopup(e);
}


@Override
public void mouseEntered(MouseEvent e) {
// TODO 自动生成的方法存根
maybeShowPopup(e);
}


@Override
public void mouseExited(MouseEvent e) {
// TODO 自动生成的方法存根
maybeShowPopup(e);
}

protected  void  maybeShowPopup(MouseEvent  e)
{
if(e.isPopupTrigger())
{
super.popup.show(e.getComponent() , e.getX() , e.getY());
}
}

}


//看到最后,我当然不会告诉你鼠标右键的线程好像创建失败

哇。。。。。。。(逃大笑

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的安卓记事本的代码示例,包括了界面、文件读写和一些基本操作: 1. 在 layout 目录下创建一个名为 activity_main.xml 的布局文件,代码如下: ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="top|left" android:inputType="textMultiLine" android:hint="Type your notes here"/> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/saveButton" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Save"/> <Button android:id="@+id/loadButton" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Load"/> <Button android:id="@+id/clearButton" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Clear"/> </LinearLayout> </LinearLayout> ``` 2. 在 MainActivity.java实现逻辑,代码如下: ```java import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; public class MainActivity extends AppCompatActivity { private EditText editText; private Button saveButton; private Button loadButton; private Button clearButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = findViewById(R.id.editText); saveButton = findViewById(R.id.saveButton); loadButton = findViewById(R.id.loadButton); clearButton = findViewById(R.id.clearButton); saveButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String text = editText.getText().toString(); save(text); } }); loadButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String text = load(); editText.setText(text); } }); clearButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editText.setText(""); } }); } private void save(String text) { try { FileOutputStream fos = openFileOutput("notes.txt", MODE_PRIVATE); fos.write(text.getBytes()); fos.close(); Toast.makeText(this, "Saved successfully!", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "Save failed!", Toast.LENGTH_SHORT).show(); } } private String load() { try { FileInputStream fis = openFileInput("notes.txt"); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line).append("\n"); } br.close(); isr.close(); fis.close(); Toast.makeText(this, "Loaded successfully!", Toast.LENGTH_SHORT).show(); return sb.toString(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "Load failed!", Toast.LENGTH_SHORT).show(); return null; } } } ``` 3. 在 AndroidManifest.xml 中添加文件读写权限: ```xml <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> ``` 以上代码实现了一个简单的记事本功能,用户可以在编辑框中输入并保存,也可以读取之前保存的内容或清空编辑框。当然,这只是一个简单的示例,实际应用中还需要考虑更多的功能和安全性问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值