Reputation: 13
I want to implement a GUI program that can write and read files, but the interface after my program runs is blank without any components. I use IDEA, and I have created the file in advance.The ultimate goal of this program is to design a text field and two buttons. When the "read file" button is clicked, the data information in the disk file will be displayed in the text field.
When the: Write File button is clicked, the user's input in the text field is written to the disk file.
This is the default interface for my program when it starts running. As you can see, it's just a small form control
enter image description here This is the default interface for my program when it starts running. As you can see, it's just a small form control
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.*;
import javax.swing.*;
public class Ftest extends JFrame{
private JScrollPane scrollPane;
private JPanel jContentPane=null;
private JTextArea jTextArea=null;
private JButton openButton=null;
private JButton closeButton=null;
private JPanel controlPanel=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 java.awt.event.ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
File file=new File("D:\\tpg.txt");
try{
FileWriter out=new FileWriter(file);
String s=jTextArea.getText();
out.write(s);
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
return openButton;
}
private JButton getCloseButton(){
if(closeButton==null){
closeButton=new JButton();
closeButton.setText("读取文件");
closeButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
File file=new File("D:\\tpg.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();
}
private JPanel initialize(){
if(jContentPane==null){
jContentPane=new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getScrollPane(),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);
}
protected JScrollPane getScrollPane(){
if(scrollPane==null){
scrollPane=new JScrollPane();
scrollPane.setViewportView(getjTextArea());
}
return scrollPane;
}
}
Upvotes: 0
Views: 50
Reputation: 324088
By default Swing components have a size of (0, 0). You need to give all your components a size by invoking the layout manager on all your panels. You do this by invoking the pack()
method before the frame is made visible:
thisClass.pack();
thisClass.setVisible(true);
Upvotes: 1