测试Swing窗口

这个Java程序演示了如何使用Swing创建一个窗口,包含两个按钮,分别用于写入和读取名为'world.txt'的文件内容。用户可以输入文本到文本区域,点击'写入文件'将内容保存到文件,然后点击'读取文件'从文件加载内容到文本区域。
摘要由CSDN通过智能技术生成
package com.yuyouya.bill;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

/**
 * @ program: bill
 * @ description: 测试Swing窗口
 * @ author: Mr.YuYueLong
 * @ create: 2022-03-16 20:42
 **/

public class Ftest extends JFrame {
  private static final long serialVersionUID = 1L;
  private JPanel JContentPane = null;
  private JTextArea jTextArea = null;
  private JPanel controlPanel = null;
  private JButton openButton = null;
  private JButton closeButton = null;


  private JButton getOpenButton() {
    if (openButton == null) {
      openButton = new JButton();
      openButton.setText("写入文件");
      openButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
          File file = new File("world.txt");
          try {
            FileWriter out = new FileWriter(file);
            String s = jTextArea.getText();
            out.write(s);
            out.close();
          } catch (Exception e1) {
            e1.printStackTrace();
          }

        }
      });
    }
    return openButton;
  }

  private JButton getCloseButton() {
    if (closeButton == null) {
      closeButton = new JButton();
      closeButton.setText("读取文件");
      closeButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
          File file = new File("world.txt");
          try {
            FileReader in = new FileReader(file);
            char[] byt = new char[1024];
            int len = in.read(byt);
            jTextArea.setText(new String(byt, 0, len));
            in.close();
          } catch (IOException ex) {
            ex.printStackTrace();
          }

        }
      });
    }
    return closeButton;
  }

  public Ftest() {
    super();
    initialize();
  }

  private void initialize() {
    this.setSize(300, 200);
    this.setContentPane(getJContentPane());
    this.setTitle("JFrame");
  }

  private JPanel getJContentPane() {
    if (JContentPane == null) {
      JContentPane = new JPanel();
      JContentPane.setLayout(new BorderLayout());
      JContentPane.add(getJTextArea(), BorderLayout.CENTER);
      JContentPane.add(getControlPanel(), BorderLayout.SOUTH);
    }
    return JContentPane;
  }

  private JPanel getControlPanel() {
    if (controlPanel == null) {
      controlPanel = new JPanel();
      controlPanel.setLayout(new BorderLayout());
      controlPanel.add(getOpenButton(), BorderLayout.SOUTH);
      getOpenButton().add(getCloseButton());

    }
    return controlPanel;
  }

  private JTextArea getJTextArea() {
    if (jTextArea == null) {
      jTextArea = new JTextArea();
      jTextArea.setLayout(new BorderLayout());
//      jTextArea.setText("我有一只小毛驴!");
    }
    return jTextArea;
  }

  public static void main(String[] args) {
    Ftest thisClass = new Ftest();
    thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    thisClass.setVisible(true);
    /*File file = new File("world.txt");
    try {
      FileOutputStream fileOutputStream = new FileOutputStream(file);
      byte[] buy = "我有一只小毛驴,我从来也不骑".getBytes();
      fileOutputStream.write(buy);
      fileOutputStream.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    try {
      FileInputStream fileInputStream = new FileInputStream(file);
      byte[] byt = new byte[1024];
      int len = fileInputStream.read(byt);
      System.err.println("文件中的信息是:" + new String(byt, 0, len));
      fileInputStream.close();
    } catch (IOException e) {
      e.printStackTrace();
    }*/
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一丝净土

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

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

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

打赏作者

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

抵扣说明:

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

余额充值