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();
}*/
}
}
测试Swing窗口
于 2022-03-17 11:02:21 首次发布
这个Java程序演示了如何使用Swing创建一个窗口,包含两个按钮,分别用于写入和读取名为'world.txt'的文件内容。用户可以输入文本到文本区域,点击'写入文件'将内容保存到文件,然后点击'读取文件'从文件加载内容到文本区域。
摘要由CSDN通过智能技术生成