1. 程式人生 > 實用技巧 >java Swing GUI 入門-檔案讀寫器

java Swing GUI 入門-檔案讀寫器

java Swing GUI 入門-檔案讀寫器

覺得有用的話,歡迎一起討論相互學習~

首先建立一個獨立的視窗

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

效果演示

  • 儲存檔案

  • 檢視檔案