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() + "\n");

            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()

效果演示

  • 保存文件

  • 查看文件


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM