NotePad version 1.0

NotePad version 1.0

自己写的一个非常简陋的类似于记事本的程序,只有简单的打开、保存功能。


/** 
 * @author nlee
 * @version 5:54:57 PM Jan 8, 2015
 */
package java_notepad;

import java.awt.Color;

import javax.swing.JFrame;

public class TxtFrame extends JFrame
{
    public static void main(String[] args)
    {
        // 设置TxtFrame
        TxtFrame tf = new TxtFrame();
        tf.setBackground(Color.darkGray);
        tf.setBounds(100, 100, 800, 600);
        tf.setLayout(null);
        tf.setVisible(true);

        // 添加菜单栏
        TxtMenuBar tmb = new TxtMenuBar();
        tmb.setFile();
        tmb.setEdit();
        tmb.setTool();
        tf.setJMenuBar(tmb);

        // 添加TxtPanel
        TxtPanel tp = new TxtPanel();
        // 添加快捷按钮
        tp.addButton();
        // 添加TextArea
        tp.addTextArea();

        tf.setContentPane(tp);
    }
}

/** 
 * @author nlee
 * @version 6:11:07 PM Jan 8, 2015
 */
package java_notepad;

import java.awt.Color;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class TxtMenuBar extends JMenuBar
{
    public TxtMenuBar()
    {
        super();
        this.setBackground(Color.white);
        this.setBounds(0, 0, 40, 30);
        this.setVisible(true);
    }

    // 添加File菜单栏
    public void setFile()
    {
        JMenu mu_file = new JMenu("File");
        mu_file.setBackground(Color.white);

        String[] mi_name = { "New", "Open", "Save", "Save as", "Exit" };
        for (int i = 0; i < mi_name.length; i++)
        {
            JMenuItem mi = new JMenuItem("   " + mi_name[i]);
            mu_file.add(mi);
        }

        this.add(mu_file);
    }
    
    // 添加Edit菜单栏
    public void setEdit()
    {
        JMenu mu_edit = new JMenu("Edit");
        mu_edit.setBackground(Color.white);

        String[] mi_name = { "Undo", "Redo", "Find", "Replace" };
        for (int i = 0; i < mi_name.length; i++)
        {
            JMenuItem mi = new JMenuItem("   " + mi_name[i]);
            mu_edit.add(mi);
        }
        this.add(mu_edit);
    }

    // 添加Tool菜单栏
    public void setTool()
    {
        JMenu mu_tool = new JMenu("Tool");
        mu_tool.setBackground(Color.white);

        this.add(mu_tool);
    }
}

/** 
 * @author nlee
 * @version 6:15:53 PM Jan 8, 2015
 */
package java_notepad;

import java.awt.Button;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JPanel;
import javax.swing.JTextArea;

public class TxtPanel extends JPanel
{
    public JTextArea ta;
    
    public TxtPanel()
    {
        super();
        this.setLayout(null);
        this.setBackground(Color.gray);
        this.setBounds(0, 30, 800, 600);
        this.setVisible(true);
    }
    
    // 添加按钮
    public void addButton()
    {
        String[] bt_name = {"new", "open", "save", "undo", "redo", "find" };
        for (int i = 0; i < bt_name.length; i++)
        {
            Button bt = new Button(bt_name[i]);
            bt.setBackground(Color.white);
            bt.setBounds(40 * i, 0, 40, 30);
            
            if (bt_name[i] == "open")
            {
                // 添加事件监听器
                bt.addMouseListener(new MouseAdapter()
                {
                    // 事件处理方法
                    @Override
                    public void mouseClicked(MouseEvent e)
                    {
                        // 将文件中的内容输出到JTextArea中
                        System.out.println("open button has been clicked!");
                        openText();
                    }
                });
            }
            
            if (bt_name[i] == "save")
            {
                // 添加事件监听器
                bt.addMouseListener(new MouseAdapter()
                {
                    // 事件处理方法
                    @Override
                    public void mouseClicked(MouseEvent e)
                    {
                        // 将JTextArea中的内容保存到文件中
                        System.out.println("save button has been clicked!");
                        saveText();
                    }
                });
            }
            
            this.add(bt);
        }
    }
    
    // 添加文本
    public void addTextArea()
    {
        ta = new JTextArea();
        ta.setBackground(Color.LIGHT_GRAY);
        ta.setVisible(true);
        ta.setBounds(0, 30, 800, 570);
        
        this.add(ta);
    }
    
    // 将JTextArea中的内容保存到文件中
    public void saveText()
    {
        String tmp = this.ta.getText();
        System.out.println(tmp);
        
        try
        {
            FileOutputStream fos = new FileOutputStream("mytxt.txt");
            byte[] b = new byte[tmp.length()];
            b = tmp.getBytes();
            fos.write(b);
        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    // 将文件中的内容输出到JTextArea中
    public void openText()
    {
        try
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    new FileInputStream("mytxt.txt")));
            String line = null;
            while ((line = br.readLine()) != null)
            {
                this.ta.append(line + "\n");
            }
        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

转载于:https://www.cnblogs.com/Coder816/p/4234857.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值