java Swing GUI 入门-文件读写器

java Swing GUI 入门-文件读写器

觉得有用的话,欢迎一起讨论相互学习~

我的微博我的github我的B站

首先创建一个独立的窗口

    public CoupPad(){}
    public static void main(String[] args) {

        CoupPad window = new CoupPad();

        window.setSize(400, 200);

        window.setVisible(true);

        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }//end main

精细化窗口

  • 需要使用Contariner容器向其中添加组件

容器Container是一个类,实际上是Component的子类,因此容器本身也是一个组件,具有组件的所有性质,但它的主要内容是容纳其他组件和容器,在其可视区显示这些组件。容器的各种的组件的大小和位置是由容器的布局管理器进行控制。

其实就是获取内容面板,JFrame无法直接添加组件需要getContentPane()获取面板,然后再内容面板上添加组件。
因此平时添加的窗口组件都是添加到ContentPane里的, 通常都是分开写的
Container c=this.getContentPane();//初始化一个容器 
c.add(****); //在容器上添加控件..
或是:
this.getContentPane().add();
  • 首先向innerWindow这个组件中添加元素,使用网格布局
        innerWindow.setLayout(new GridLayout(2, 2, 1, 1)); //设置JPanel的布局

        innerWindow.add(read);//JButton

        innerWindow.add(write);//JButton

        innerWindow.add(nameField);//JTextField

        innerWindow.add(file);//JLabel
        //向Jframe类型的对象中添加一个布局并且添加组件
        //边界布局具体参考博客
        //        https://xuzhiwei.blog.csdn.net/article/details/111302347
        this.getContentPane().setLayout(new BorderLayout());
       // 关于BorderLayout()边界布局法,主要是按照东南西北中的顺序进行布局
        this.getContentPane().add("North", innerWindow);//在上部分添加一个Jpanel

        this.getContentPane().add(new JScrollPane(textArea));//添加一个滑动控件

        this.getContentPane().add("Center", textArea);//添加一个文本区域
  • 调节一下颜色格式
        innerWindow.setBackground(Color.red);

        textArea.setBackground(Color.green);

        //设置文本区域
        textArea.setFont(new Font("Serif", Font.ITALIC, 20));


目前的效果是这样的,但是现在还没有加入函数响应的效果.

添加一个动作监听器

    public void actionPerformed(ActionEvent evt) {

        String fileName = nameField.getText();// 获取文本框中的文件名称

        if (evt.getSource() == read) {
//      如果此时事件的来源是read
//      调用read函数
            textArea.setText("");
            readTextFile(textArea, fileName);//读取相应的文件名称并将其显示到testArea中
        } else {
//       如果此时事件的来源不是read,也就是说事件的来源是write调用以下函数
            writeTextFile(textArea, fileName);
        }


    }//end actionPerformed()

读和写特定文件


 public CoupPad() {
        ...
        read.addActionListener(this);

        write.addActionListener(this);
 }
    //writes to a text file.  IntelliJ will look for it at the Project Folder

    private void writeTextFile(JTextArea textArea, String fileName) {

        try {

            FileWriter outStream = new FileWriter(fileName);

            outStream.write(textArea.getText());

            outStream.close();

        } catch (IOException e) {

            textArea.setText("IOERROR: " + e.getMessage() + "
");

            e.printStackTrace();

        }

    } // end writeTextFile()


    //watches the button and waits until it is clicked

    public void actionPerformed(ActionEvent evt) {

        String fileName = nameField.getText();// 获取文本框中的文件名称

        if (evt.getSource() == read) {
//      如果此时事件的来源是read
//      调用read函数
            textArea.setText("");
            readTextFile(textArea, fileName);//读取相应的文件名称并将其显示到testArea中
        } else {
//       如果此时事件的来源不是read,也就是说事件的来源是write调用以下函数
            writeTextFile(textArea, fileName);
        }


    }//end actionPerformed()

效果演示

  • 保存文件

  • 查看文件
原文地址:https://www.cnblogs.com/cloud-ken/p/14146391.html