首页上一页 1 下一页尾页 1 条记录 1/1页
程序编写正确,打印出来有问题
发表在Java图书答疑
2016-04-07
是否精华
是
否
版块置顶:
是
否
package file;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;
public class Ftest extends JFrame {//创建类继承JFrame
private static final long serialVersionUID=1L;
private JPanel jContentPanel=null;//创建面板对象
private JTextArea jTextArea=null;//创建文本域对象
private JPanel controlPanel=null;//创建面板对象
private JButton openButton=null;//创建按钮对象
private JButton closeButton=null;//创建按钮对象
private JTextArea getJTextArea(){
if(jTextArea==null){
jTextArea=new JTextArea();
}
return jTextArea;
}
private JPanel getControlPanel(){
if(controlPanel==null){
FlowLayout flowLayout=new FlowLayout();
flowLayout.setVgap(1);
controlPanel=new JPanel();
controlPanel.setLayout(flowLayout);
controlPanel.add(getOpenButton(),null);
controlPanel.add(getCloseButton(),null);
}
return controlPanel;
}
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 byt[]=new char[1024];
int len =in.read(byt);
jTextArea.setText(new String(byt,0,len));
in.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
});
}
return closeButton;
}
public Ftest(){
super();
initialize();
}
public void initialize(){
this.setSize(300,200);
this.setContentPane(getContentPane());
this.setTitle("JFrame");
}
private JPanel getJContentPane(){
if(jContentPanel==null){
jContentPanel=new JPanel();
jContentPanel.setLayout(new BorderLayout());
jContentPanel.add(getJTextArea(),BorderLayout.CENTER);
jContentPanel.add(getControlPanel(), BorderLayout.SOUTH);
}
return jContentPanel;
}
public static void main(String[] args) {
Ftest thisClass=new Ftest();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
}
这个程序运行之后只有一个JFrame框架,里面的文本区域和按钮都没有,为什么?谢谢
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;
public class Ftest extends JFrame {//创建类继承JFrame
private static final long serialVersionUID=1L;
private JPanel jContentPanel=null;//创建面板对象
private JTextArea jTextArea=null;//创建文本域对象
private JPanel controlPanel=null;//创建面板对象
private JButton openButton=null;//创建按钮对象
private JButton closeButton=null;//创建按钮对象
private JTextArea getJTextArea(){
if(jTextArea==null){
jTextArea=new JTextArea();
}
return jTextArea;
}
private JPanel getControlPanel(){
if(controlPanel==null){
FlowLayout flowLayout=new FlowLayout();
flowLayout.setVgap(1);
controlPanel=new JPanel();
controlPanel.setLayout(flowLayout);
controlPanel.add(getOpenButton(),null);
controlPanel.add(getCloseButton(),null);
}
return controlPanel;
}
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 byt[]=new char[1024];
int len =in.read(byt);
jTextArea.setText(new String(byt,0,len));
in.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
});
}
return closeButton;
}
public Ftest(){
super();
initialize();
}
public void initialize(){
this.setSize(300,200);
this.setContentPane(getContentPane());
this.setTitle("JFrame");
}
private JPanel getJContentPane(){
if(jContentPanel==null){
jContentPanel=new JPanel();
jContentPanel.setLayout(new BorderLayout());
jContentPanel.add(getJTextArea(),BorderLayout.CENTER);
jContentPanel.add(getControlPanel(), BorderLayout.SOUTH);
}
return jContentPanel;
}
public static void main(String[] args) {
Ftest thisClass=new Ftest();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
}
这个程序运行之后只有一个JFrame框架,里面的文本区域和按钮都没有,为什么?谢谢