首页上一页 1 下一页尾页 3 条记录 1/1页
jContentPane添加的两个方法没有写
发表在Java图书答疑
2020-02-08
悬赏:66 学分
《Java从入门到精通(第4版)》第15章 I/O(输入/输出) 279页-281页
是否精华
是
否
版块置顶:
是
否
package assignments;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Ftest extends JFrame {
private static final long serialVersionUID = 1L;
// 创建面板对象
// 创建文本域对象
// 创建控制面板对象
// 创建按钮对象
private JPanel jContentPane = null;
private JTextArea jTextArea = null;
private JPanel controlPane = null;
private JButton closeButton = null;
private JButton openButton = null;
private JButton getOpenButton() {
if (openButton == null) {
openButton = new JButton();
openButton.setText("写入文件");
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
File file = new File("word.txt");
try {
// 创建FileWriter对象
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() {
public void actionPerformed(ActionEvent e) {
File file = new File("word.txt");
try {
FileReader in = new FileReader(file);
char ch[] = new char[1024];
int len = in.read(ch);
// 设置文本域的显示信息
jTextArea.setText(new String(ch, 0, len));
in.close();
} catch (Exception e1) {
e1.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;
}
public static void main(String[] args) {
Ftest thisClass = new Ftest();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
}![1581131535763881.jpg WAG6IRE9B8`8`72SZ]%EGZM.jpg](/MrItClass/Data/ueditor/php/upload/image/20200208/1581131535763881.jpg)

购物车
发表新帖
立即签到

已解决







