java (GUI) 制作简单记事本

  • java制作简单记事本.

1、利用学过的布局、容器、组件知识,搭建一个如下图的记事本界面。

2、利用下拉列表和按钮的监听事件,实现改变文本框和文本域的字体样式和颜色。

  • 效果展示

def544da68604f64b15a373e1c4cfaf8.png

  •  代码实现
package test;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.*;
import java.util.Date;

public class NotepadDemo extends JFrame {
    //创建菜单条
    JMenuBar menuBar;
    //菜单
    JMenu fileMenu,editMenu,formatMenu,viewMenu,helpMenu;
    //"文件"的菜单项
    JMenuItem fileMenu_Open,fileMenu_Save,fileMenu_Exit;
    //“编辑”的菜单项
    JMenuItem editMenu_TimeDate;
    //“工具”的菜单项
    JMenuItem toolMenu_form;
    //“查看”的菜单项
    JMenuItem viewMenu_Status;
    //“帮助”的菜单项
    JMenuItem helpMenu_viewHelp,helpMenu_AboutNotepad;
    //创建工具栏
    JToolBar toolbar;
    //下拉列表框
    JComboBox JComboBox_fontName,JComboBox_fontStyle,JComboBox_fontSize,JComboBox_fontColor;
    JPanel JPanel_textField_textArea,JPanel_textField,JPanel_textArea,JPanel_jLabel;
    //添加按钮
    JButton jb_add;
    //文本框
    JTextField jtf;
    //文本域
    JTextArea textArea;
    //为文本域添加滚动条变量
    JScrollPane JScrollPane_textArea;
    //底部标签
    JLabel jl;
    //设置文本域默认字体
    Font textArea_defaultFont;
    //各下拉列表框的数组
    String[] fontName,fontStyle,fontSize,fontColor;
    //字体监听区变量
    String name="楷体",text_Style;
    int type;
    int size=16;

    //设置边框的特性是立体的边界:突起
    Border etched = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);

    public NotepadDemo(){

        //设置标题
        this.setTitle("记事本");

        //设置窗口位置大小
        this.setBounds(400,200,700,500);

        //设置窗体背景颜色
        this.setBackground(Color.pink);
        //this.getContentPane().setBackground(Color.black);

        //设置窗口布局方式
        this.setLayout(new BorderLayout());

        //创建菜单栏
        menuBar = new JMenuBar();
        menuBar.setBackground(new Color(53,231,244));

        //创建菜单
        fileMenu = new JMenu("文件(F)");
        editMenu = new JMenu("编辑(E)");
        formatMenu = new JMenu("工具(T)");
        viewMenu = new JMenu("查看(V)");
        helpMenu = new JMenu("帮助(H)");

        //将菜单添加到菜单栏中
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(formatMenu);
        menuBar.add(viewMenu);
        menuBar.add(helpMenu);

        //创建菜单项
        fileMenu_Open = new JMenuItem("打开(O)");
        fileMenu_Save = new JMenuItem("保存(S)");
        editMenu_TimeDate = new JMenuItem("时间日期");
        viewMenu_Status = new JMenuItem("状态栏");
        helpMenu_viewHelp = new JMenuItem("查看帮助");
        helpMenu_AboutNotepad = new JMenuItem("关于记事本");
        fileMenu_Exit = new JMenuItem("退出(X)");
        toolMenu_form = new JMenuItem("表格(B)");

        //将菜单项添加到菜单中
        fileMenu.add(fileMenu_Open);
        fileMenu.add(fileMenu_Save);
        fileMenu.addSeparator();
        fileMenu.add(fileMenu_Exit);
        editMenu.add(editMenu_TimeDate);
        formatMenu.add(toolMenu_form);
        viewMenu.add(viewMenu_Status);
        helpMenu.add(helpMenu_viewHelp);
        helpMenu.add(helpMenu_AboutNotepad);

        //创建工具栏
        toolbar = new JToolBar();
        toolbar.setBackground(new Color(180,231,29));
        toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));

        //创建标签
        jl = new JLabel("Made in 小李");

        //创建面板
        JPanel_textField_textArea = new JPanel();
        JPanel_textField = new JPanel();
        JPanel_textField.setBackground(new Color(200,191,231));
        JPanel_textArea = new JPanel();
        JPanel_jLabel = new JPanel();
        JPanel_textField_textArea.setLayout(new BorderLayout());
        JPanel_textArea.setLayout(new GridLayout());
        JPanel_textField_textArea.add(JPanel_textField,BorderLayout.NORTH);
        JPanel_textField_textArea.add(JPanel_textArea,BorderLayout.CENTER);

        //字体数组
        fontName=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        fontStyle= new String[]{"常规", "粗体", "斜体", "粗斜体"};
        fontSize= new String[]{"8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28", "36", "48", "72"};
        fontColor= new String[]{"黑色","红色", "绿色", "黄色", "粉色"};

        //创建下拉列表框
        JComboBox_fontName = new JComboBox(fontName);
        JComboBox_fontName.setEditable(true);
        JComboBox_fontName.setBackground(new Color(254,218,234));
        JComboBox_fontStyle = new JComboBox(fontStyle);
        JComboBox_fontStyle.setEditable(true);
        JComboBox_fontStyle.setBackground(new Color(254,218,234));
        JComboBox_fontSize = new JComboBox(fontSize);
        JComboBox_fontSize.setEditable(true);
        JComboBox_fontSize.setBackground(new Color(254,218,234));
        JComboBox_fontColor = new JComboBox(fontColor);
        JComboBox_fontColor.setEditable(true);
        JComboBox_fontColor.setBackground(new Color(254,218,234));

        //设置下拉列表框的大小
        JComboBox_fontName.setMinimumSize(new Dimension(110,35));

        //设置边框特性
        JComboBox_fontName.setBorder(etched);
        JComboBox_fontStyle.setBorder(etched);
        JComboBox_fontSize.setBorder(etched);
        JComboBox_fontColor.setBorder(etched);
        toolbar.setBorder(etched);

        //将列表框添加到工具栏中
        toolbar.add(JComboBox_fontName);
        toolbar.add(JComboBox_fontStyle);
        toolbar.add(JComboBox_fontSize);
        toolbar.add(JComboBox_fontColor);

        //创建按钮
        jb_add = new JButton("添加");
        jb_add.setBackground(new Color(248,139,239));

        //创建文本框
        jtf = new JTextField(60);
        jtf.setBackground(new Color(239,228,176));

        //创建文本域
        textArea = new JTextArea();
        textArea.setWrapStyleWord(true);
        textArea.setLineWrap(true);
        textArea.setBackground(new Color(200,191,231));

        //文本域默认全局字体
        textArea_defaultFont = new Font("微软雅黑",Font.PLAIN,16);
        textArea.setFont(textArea_defaultFont);

        //为文本域添加滚动条
        JScrollPane_textArea = new JScrollPane(textArea);
        JScrollPane_textArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        JScrollPane_textArea.setBorder(etched);

        //面板添加
        JPanel_textField.add(jtf);
        JPanel_textField.add(jb_add);
        JPanel_textArea.add(JScrollPane_textArea);
        JPanel_jLabel.add(jl,FlowLayout.LEFT);

        //窗口添加
        this.setJMenuBar(menuBar);//菜单栏
        this.add(toolbar,BorderLayout.NORTH);//工具栏
        this.add(JPanel_textField_textArea,BorderLayout.CENTER);//面板1
        this.add(JPanel_jLabel,BorderLayout.SOUTH);//面板4



        //设置窗体可见和关闭事件
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    //选项监听事件方法
   public void actionListener(){

        //按钮监听事件
        jb_add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String s = jtf.getText();
                textArea.setText(s);
                JOptionPane.showMessageDialog(null, "添加成功!");
                jtf.setText("");
            }
        });

        //工具监听事件
       toolMenu_form.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               JOptionPane.showMessageDialog(null,"抱歉,此功能尚未实现!!!","警告",JOptionPane.WARNING_MESSAGE);
           }
       });

        //选项文件打开监听事件
       fileMenu_Open.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFrame jf = new JFrame();
                FileDialog fdopen = new FileDialog(jf, "打开(O)", FileDialog.LOAD);
                fdopen.setVisible(true);
                BufferedReader in = null;
                try {
                    in = new BufferedReader(new FileReader(fdopen.getDirectory() + fdopen.getFile()));
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                }
                String str = null;
                while(true) {
                    try {
                        if (!((str = in.readLine()) != null)) break;
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    byte[] bytes = str.getBytes();
                    String s = new String(bytes);
                    textArea.setText(s);
                }
                try {
                    in.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            }
        });

       //选项文件保存监听事件
       fileMenu_Save.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               JFrame jf = new JFrame();
               FileDialog fd = new FileDialog(jf, "保存(S)", FileDialog.SAVE);
               fd.setVisible(true);
               String path = fd.getDirectory();//获取路径
               String filename = fd.getFile();//获取文件名
               try {
                   FileOutputStream out = new FileOutputStream(path + filename, false);
                   String s = textArea.getText();//输出到文件
                   byte[] b = s.getBytes();
                   out.write(b);
                   out.close();
               } catch (IOException ex) {
                   System.out.println(ex);
               }
           }
       });

        //编辑选项时间日期监听事件
       editMenu_TimeDate.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Date date = new Date();
                String sj = new String(String.valueOf(date));
                textArea.setText(sj);
            }
        });

        //状态栏监听事件
       viewMenu_Status.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               JOptionPane.showMessageDialog(null,"抱歉,此功能尚未实现!!!","警告",JOptionPane.WARNING_MESSAGE);
           }
       });

       //查看帮助监听事件
       helpMenu_viewHelp.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               JOptionPane.showMessageDialog(null,"抱歉,此功能尚未实现!!!","警告",JOptionPane.WARNING_MESSAGE);
           }
       });

       //关于记事本监听事件
       helpMenu_AboutNotepad .addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {

               JOptionPane.showMessageDialog(null,
                       "============================================\n"+
                               " 编写者:Alan_木子李                                \n"+
                               " 编写时间:2022-04-30                         \n"+
                               " 不足之处,一起交流学习。                                \n"+
                               "                 -------------Flighting-------------\n"+
                               "欢迎使用!!!                                 \n"+
                               "============================================\n",
                       "记事本",JOptionPane.INFORMATION_MESSAGE);

           }
       });

       //文件选项退出监听事件
       fileMenu_Exit.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               System.exit(0);
           }
       });
    }

    //字体设置方法
    public void setTextAreaFont(String name,int type,int size){

        textArea.setFont(new Font(name,type,size));
        jtf.setFont(new Font(name,type,12));

    }


    //列表项监听事件
    public void ItemListener(){
        //字体fontName监听
        JComboBox_fontName.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                final DefaultComboBoxModel model = new DefaultComboBoxModel(fontName);
                if (model.getIndexOf(e.getItem()) < 0) {
                    model.addElement(e.getItem());
                }
                name=(String) e.getItem();
                setTextAreaFont(name,type,size);
            }
        });

        //字体fontStyle监听
        JComboBox_fontStyle.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                final DefaultComboBoxModel model = new DefaultComboBoxModel(fontStyle);
                if (model.getIndexOf(e.getItem()) < 0) {
                    model.addElement(e.getItem());
                }
               text_Style=(String) e.getItem();
                if (text_Style.equals("常规")){

                    type=Font.PLAIN;

                } else if (text_Style.equals("粗体" )){

                    type=Font.BOLD;

                } else if(text_Style.equals("斜体" )){

                    type=Font.ITALIC;

                }else{

                    type=Font.ITALIC+Font.BOLD;
                }
                setTextAreaFont(name,type,size);
            }
        });
        //字体fontSize监听
        JComboBox_fontSize.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                final DefaultComboBoxModel model = new DefaultComboBoxModel(fontSize);
                if (model.getIndexOf(e.getItem()) < 0) {
                    model.addElement(e.getItem());
                }
                String s=(String) e.getItem();
                size=Integer.parseInt(s);
                setTextAreaFont(name,type,size);
            }
        });
        //字体fontColor监听
        JComboBox_fontColor.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                final DefaultComboBoxModel model = new DefaultComboBoxModel(fontColor);
                if (model.getIndexOf(e.getItem()) < 0) {
                    model.addElement(e.getItem());
                }
                String int_color = (String) e.getItem();
                if(int_color.equals("黑色")){

                    jtf.setForeground(Color.black);
                    textArea.setForeground(Color.black);

                }else if(int_color.equals("红色")){

                    jtf.setForeground(Color.RED);
                    textArea.setForeground(Color.red);

                }else if(int_color.equals("绿色")){

                    jtf.setForeground(Color.green);
                    textArea.setForeground(Color.green);

                }else if(int_color.equals("黄色")){

                    jtf.setForeground(Color.yellow);
                    textArea.setForeground(Color.yellow);

                }else{

                    jtf.setForeground(Color.pink);
                    textArea.setForeground(Color.pink);

                }
            }
        });
    }

    public static void main(String[] args) {
        NotepadDemo nd = new NotepadDemo();
        nd.actionListener();
        nd.ItemListener();
        //nd.setTextAreaFont(nd.name, nd.type, nd.size);
    }
}

  • 20
    点赞
  • 95
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alan木子李

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值