| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 804 人关注过本帖
标题:关于JAVA记事本问题
只看楼主 加入收藏
anhuizxw
Rank: 1
等 级:新手上路
帖 子:17
专家分:6
注 册:2009-7-12
结帖率:83.33%
收藏
已结贴  问题点数:20 回复次数:4 
关于JAVA记事本问题
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import *;
import java.text.SimpleDateFormat;
import java.util.Calendar;


public class MyNoteBook
{
    String title="ERROR MESSAGE";
   
    int type=JOptionPane.ERROR_MESSAGE;
   
  public MyNoteBook()
  {
    final JFrame frame = new JFrame("我的记事本");
   
    final JTextArea text=new JTextArea();
   
    frame.setSize(600, 500);
   
    frame.addWindowListener(new WindowAdapter()
    {
        public void windowClosing(WindowEvent e)
        {
             System.exit(0);
        }
    });
   
    JPanel panel=new JPanel();
   
    panel.setLayout(new GridLayout(1,1));
   
    panel.add(new JScrollPane(text));
   
    frame.getContentPane().add(panel);
   
    JMenuBar Mbar = new JMenuBar();
   
    frame.setJMenuBar(Mbar);
   
    JMenu jfile = new JMenu("文件(F)", true);
   
    JMenu jedit = new JMenu("编辑(E)", true);
   
    JMenu jmode = new JMenu("格式(O)", true);
   
    JMenu jhelp = new JMenu("帮助(H)", true);
   
    jfile.setMnemonic('F');
   
    jedit.setMnemonic('E');
   
    jmode.setMnemonic('O');
   
    jhelp.setMnemonic('H');
   
    Mbar.add(jfile);
   
    Mbar.add(jedit);
   
    Mbar.add(jmode);
   
    Mbar.add(jhelp);
   
   
    JMenuItem jnew = new JMenuItem("新建");
    jnew.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
              text.setText(" ");
        }
    });
    jnew.setMnemonic('N');
    jnew.setAccelerator( KeyStroke.getKeyStroke('N',java.awt.Event.CTRL_MASK,true));
   
    JMenuItem jopen = new JMenuItem("打开");
    jopen.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
             JFileChooser openfile=new JFileChooser();
             openfile.setDialogTitle("打开文件");
             openfile.setApproveButtonText("打开");
             openfile.showOpenDialog(frame);
             File file=openfile.getSelectedFile();
             FileInputStream inputfile=null;
             String message="The file not Found";
            try
            {
              inputfile=new FileInputStream(file);
            }
             catch(FileNotFoundException fe)
             {      
             JOptionPane.showMessageDialog(frame,message,title,type);
             }
             int readbytes;
             String message1="read file error";
             try
             {
                while((readbytes=inputfile.read())!=-1)
                {
                   text.append(String.valueOf((char)readbytes));   
                }
             }
             catch(IOException ioe)
             {
               JOptionPane.showMessageDialog(frame,message1,title,type);  
             }
             String closemessage="close stream error";
             try{
                     inputfile.close();
             }
             catch(IOException ioe)
             {
               JOptionPane.showMessageDialog(frame,closemessage,title,type);   
             }
        }
    });
    jopen.setMnemonic('O');
    jopen.setAccelerator(KeyStroke.getKeyStroke('O',java.awt.Event.CTRL_MASK,true));
   
    JMenuItem jsave = new JMenuItem("保存");
    jsave.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
           JFileChooser savefile=new JFileChooser();
           savefile.setApproveButtonText("保存");
           savefile.setDialogTitle("保存文件");
           savefile.showSaveDialog(frame);
           File filesa=savefile.getSelectedFile();
           String messagef="File not Found";
           FileOutputStream outputfile=null;
           try
           {
             outputfile=new FileOutputStream(filesa);
           }
           catch(FileNotFoundException fe)
           {      
             JOptionPane.showMessageDialog(frame,messagef,title,type);
           }
           String filecontent=text.getText();
           String wrmessage="write error";
           try
           {
              outputfile.write(filecontent.getBytes());
           }
           catch(IOException ioe)
           {
            JOptionPane.showMessageDialog(frame,wrmessage,title,type);   
           }
           String cmessage="close stream error";
           try
           {
               outputfile.close();
           }
           catch(IOException ioe)
           {
               JOptionPane.showMessageDialog(frame,cmessage,title,type);
           }
        }
    });
    jsave.setMnemonic('S');
    jsave.setAccelerator(KeyStroke.getKeyStroke('S',java.awt.Event.CTRL_MASK,true));
   
    JMenuItem jothersava = new JMenuItem("另存为...");
    jothersava.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
              
        }
    });
   
    JMenuItem jquite = new JMenuItem("退出");
    jquite.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            int i=JOptionPane.showConfirmDialog(this,"确定要退出吗?","关闭",JOptionPane.YES_NO_OPTION);
            if(i==JOptionPane.YES_OPTION)
            System.exit(0);
        }
    });
    jquite.setMnemonic('Q');
    jquite.setAccelerator(KeyStroke.getKeyStroke('Q',java.awt.Event.CTRL_MASK,true));
   
     
    JMenuItem jcut = new JMenuItem("剪切");
    jcut.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
              text.cut();
        }
    });
    jcut.setMnemonic('C');
    jcut.setAccelerator(KeyStroke.getKeyStroke('C',java.awt.Event.CTRL_MASK,true));
   
    JMenuItem jcopy = new JMenuItem("复制");
    jcopy.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
              text.copy();
        }
    });
    jcopy.setMnemonic('o');
    jcopy.setAccelerator(KeyStroke.getKeyStroke('O',java.awt.Event.CTRL_MASK,true));
   
    JMenuItem jpaste = new JMenuItem("粘贴");
    jpaste.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
              text.paste();
        }
    });
    jpaste.setMnemonic('P');
    jpaste.setAccelerator(KeyStroke.getKeyStroke('P',java.awt.Event.CTRL_MASK,true));
   
     JMenuItem jallselect = new JMenuItem("全选");
    jallselect.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
              text.selectAll();
        }
    });
    jallselect.setMnemonic('A');
    jallselect.setAccelerator(KeyStroke.getKeyStroke('A',java.awt.Event.CTRL_MASK,true));
   
    JMenuItem jtimeDate = new JMenuItem("插入日期时间");
    jtimeDate.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy'年'MM'月'dd'日'+HH:mm:ss");
            Calendar today = Calendar.getInstance();
            String timeDate = dateFormat.format(today.getTime());
            text.append(timeDate);
        }
     });
    jtimeDate.setMnemonic('D');
    jtimeDate.setAccelerator(KeyStroke.getKeyStroke('D',java.awt.Event.CTRL_MASK,true));
   
    JMenuItem jnewline = new JMenuItem("自动换行");
    jnewline.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            text.setLineWrap(true);
        }
    });
        
    JMenuItem jfont = new JMenuItem("字体...");
    jfont.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
   
        }
    });
    jfont.setMnemonic('F');
    jfont.setAccelerator(KeyStroke.getKeyStroke('F',java.awt.Event.CTRL_MASK,true));
        
     JMenuItem jabout = new JMenuItem("帮助");
    jabout.addActionListener(new ActionListener()
    {
         public void actionPerformed(ActionEvent e)
         {
             int type=JOptionPane.INFORMATION_MESSAGE;
             String title="关于";
             String message="我的记事本";
             JOptionPane.showMessageDialog(frame,message,title,type);
         }
    });
    jfile.add(jnew);
    jfile.add(jopen);
    jfile.add(jsave);
    jfile.add(jothersava);
    jfile.addSeparator();
    jfile.add(jquite);
    jedit.add(jcut);
    jedit.add(jcopy);
    jedit.add(jpaste);
    jedit.addSeparator();
    jedit.add(jallselect);
    jedit.add(jtimeDate);  
    jmode.add(jnewline);
    jmode.add(jfont);
    jhelp.add(jabout);
    frame.setVisible(true);
  }
 public static void main(String[] args)
 {
    MyNoteBook myNoteBook = new MyNoteBook();
 }        
         
}
另存为、自动换行、字体要怎么实现???请高手解答
搜索更多相关主题的帖子: 记事本 JAVA 
2009-12-24 16:41
流星雨
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:JAVA风暴
等 级:版主
威 望:43
帖 子:1854
专家分:1868
注 册:2004-5-30
收藏
得分:14 
换行
static void outputfile(File file) {
        BufferedReader br=null;
        try {
            String filePath = file.getPath();//获取文件的当前路径
            br = new BufferedReader(new FileReader(filePath));//这是一种缓冲读取文件方法
            String d = br.readLine();//每次只读一行
            Notepad.word.setText("");//要对文本域中的内容清空
            while (d!= null) {
                Notepad.word.append(d);//这个方法是在字符串的后面插入新的字符串
                Notepad.word.append("\n");//当读完一行后要进行换行
                d = br.readLine();
            }

            br.close();
            Notepad.word.grabFocus();//将光标转至文本域

        } catch (FileNotFoundException el) {
            el.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
    }

感谢你们带我找到星空下美丽神话,无论经历多少苦痛也不放弃的梦;插上希望翅膀乘风我和你们飞翔,飞过海天尽头携手把梦想实现.....
2009-12-24 20:35
流星雨
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:JAVA风暴
等 级:版主
威 望:43
帖 子:1854
专家分:1868
注 册:2004-5-30
收藏
得分:0 
字体设计
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class NewFont extends JDialog implements ListSelectionListener,
        ActionListener {
    /***********************************************************
     <B...................串行版本标识....................B>
     ***********************************************************/
    //private static final long serialVersionUID = 555561700072246630L;
    Container component;
    JScrollPane pane, paneSize;
    JList listBody, listType, listSize;
    JPanel panel1,panel2,panel3,panel4;
    JTextField bodyField,typeField,sizeField;
    JLabel fontBodyLabel,fontTypeLabel,fontSizeLabel,example;
    JButton yes, no;
    String[] s2 = { "常规", "粗体", "斜体", "粗斜体" };
    String[] s3 = { "8", "9", "10", "11", "12", "14","16", "18", "20", "22", "24", "26",
            "28", "36", "48", "72", "初号","小初","一号", "小一", "二号","小二", "三号", "小三", "四号", "小四", "五号",
            "小五", "六号", "小六", "七号", "八号",};
    int [] size={8, 9, 10, 11, 12, 14,16,18, 20, 22, 24, 26,
                28, 36, 48,72,42,36,26,24,22,18,16,14,13,12,11,9,
                8,7,6,5};
    /***********************************************************
     <B...................获取本地系统中的所有字体....................B>
     ***********************************************************/
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String[] fontlist = ge.getAvailableFontFamilyNames();

      public NewFont() {
        super(Notepad.myFrame1);
        setTitle("字体设置");
        setSize(360, 250);
        setResizable(false);
        setLocation(100, 100);
        component = this.getContentPane();
        component.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 0));

        panel1 = new JPanel();
        panel1.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 0));
        panel1.setPreferredSize(new Dimension(340, 23));
        // panel1.setBorder(BorderFactory.createEtchedBorder());
        fontBodyLabel = new JLabel("字体(F):");
        fontBodyLabel.setPreferredSize(new Dimension(120, 25));
        fontTypeLabel = new JLabel("字形(Y):");
        fontTypeLabel.setPreferredSize(new Dimension(120, 25));
        fontSizeLabel = new JLabel("大小(S):");
        panel1.add(fontBodyLabel);
        panel1.add(fontTypeLabel);
        panel1.add(fontSizeLabel);

        
        /***********************************************************
         * <B.........在第二个面板中放上三个文本框,显示从JList中选取的项的值......B>
         ***********************************************************/
        panel2 = new JPanel();
        panel2.setPreferredSize(new Dimension(340, 22));
        panel2.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 0));
        // panel2.setBorder(BorderFactory.createEtchedBorder());
        bodyField = new JTextField("宋体", 19);
        typeField = new JTextField("常规", 19);
        sizeField = new JTextField("四号", 9);

        bodyField.setEditable(false);
        typeField.setEditable(false);
        sizeField.setEditable(false);
        bodyField.setBackground(Color.white);
        typeField.setBackground(Color.white);
        sizeField.setBackground(Color.white);
        panel2.add(bodyField);
        panel2.add(typeField);
        panel2.add(sizeField);
        /***********************************************************
        <B...........在第三个面板中放上三个列表框,JLis显示供选取项的值...........B>
         ***********************************************************/
        panel3 = new JPanel();
        panel3.setPreferredSize(new Dimension(340, 120));
        panel3.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 0));
        // panel3.setBorder(BorderFactory.createEtchedBorder());
        listBody = new JList(fontlist);
        listType = new JList(s2);
        listSize = new JList(s3);
        listBody.setSelectedIndex(128);
        listType.setSelectedIndex(0);
        listSize.setSelectedIndex(8);
        listBody.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        listType.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        listSize.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        listBody.addListSelectionListener(this);
        listType.addListSelectionListener(this);
        listSize.addListSelectionListener(this);
        pane = new JScrollPane(listBody,
                    ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        paneSize = new JScrollPane(listSize,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        pane.setBorder(BorderFactory.createLoweredBevelBorder());
        
        
        pane.setPreferredSize(new Dimension(120, 105));
        listType.setPreferredSize(new Dimension(120, 105));
        listType.setBorder(BorderFactory.createLoweredBevelBorder());
        paneSize.setPreferredSize(new Dimension(60, 105));
        
        panel3.add(pane);
        panel3.add(listType);
        panel3.add(paneSize);
        /***********************************************************
        <B...........在第四个面板中放上三个列表框,JLis显示供选取项的值...........B>
         ***********************************************************/
        panel4 = new JPanel();
        panel4.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 0));
        panel4.setPreferredSize(new Dimension(340, 40));
        // panel4.setBorder(BorderFactory.createEtchedBorder());//测试用的
        example = new JLabel("东软Neusoft");
        example.setFont(new Font("宋体", Font.ITALIC, 18));
        example.setBackground(Color.white);
        example.setPreferredSize(new Dimension(160, 35));
        example.setBorder(BorderFactory.createLoweredBevelBorder());
        yes = new JButton("确定");
        no = new JButton("取消");
        yes.setFocusPainted(false);//不让按钮获取焦点,主要是为了美观
        no.setFocusPainted(false);
        yes.addActionListener(this);//给按钮添加鼠标监听器
        no.addActionListener(this);
        panel4.add(example);
        panel4.add(yes);
        panel4.add(no);
        component.add(panel1);
        component.add(panel2);
        component.add(panel3);
        component.add(panel4);
//        try {
//   
//            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//            SwingUtilities.updateComponentTreeUI(this);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        setVisible(true);

    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        if (e.getSource() == listBody) {
            
            example.setFont(new Font(String
                    .valueOf(listBody.getSelectedValue()), listType.getSelectedIndex(),size[listSize.getSelectedIndex()]));
            bodyField.setText(String.valueOf(listBody.getSelectedValue()));

        } else if (e.getSource() == listType) {
            example.setFont(new Font(String
                    .valueOf(listBody.getSelectedValue()), listType.getSelectedIndex(),size[listSize.getSelectedIndex()]));
            typeField.setText(String.valueOf(listType.getSelectedValue()));

        } else if (e.getSource() == listSize) {
            example.setFont(new Font(String
                    .valueOf(listBody.getSelectedValue()), listType.getSelectedIndex(),size[listSize.getSelectedIndex()]));
            sizeField.setText(String.valueOf(listSize.getSelectedValue()));

        }
    }

   

感谢你们带我找到星空下美丽神话,无论经历多少苦痛也不放弃的梦;插上希望翅膀乘风我和你们飞翔,飞过海天尽头携手把梦想实现.....
2009-12-24 20:37
流星雨
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:JAVA风暴
等 级:版主
威 望:43
帖 子:1854
专家分:1868
注 册:2004-5-30
收藏
得分:0 
存储:

public static void saveFile() {
        String k = Notepad.word.getText().replaceAll("\n", "\t");//为了使保存的文本按照原有的格式
        String path = Notepad.s1.getSelectedFile().getPath();
        // s1.setFileFilter(new FileFilter("") );
        // String name = s1.getSelectedFile().getName();
        String newfile1 = path;

        try {
            FileWriter fw = new FileWriter(newfile1);
            BufferedWriter br = new BufferedWriter(fw);
            br.write(k);
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//另存你可以将path 写成全局,设置一个set方法,另存储的时候,你自己set一个路径进去就可以了。

感谢你们带我找到星空下美丽神话,无论经历多少苦痛也不放弃的梦;插上希望翅膀乘风我和你们飞翔,飞过海天尽头携手把梦想实现.....
2009-12-24 20:47
anhuizxw
Rank: 1
等 级:新手上路
帖 子:17
专家分:6
注 册:2009-7-12
收藏
得分:0 
我想问的是怎么在我上面的源代码上添加
2009-12-25 13:10
快速回复:关于JAVA记事本问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019556 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved