Java 程序运行后,程序生成的窗口为空且没有组件
我想实现一个可以写入和读取文件的GUI程序,但是程序运行后的界面是空白的,没有任何组件。我使用IDEA,我已经提前创建了文件。这个程序的最终目标是设计一个文本字段和两个按钮。单击“读取文件”按钮时,磁盘文件中的数据信息将显示在文本字段中 单击:Write File按钮时,用户在文本字段中的输入将写入磁盘文件 这是我的程序开始运行时的默认界面。如您所见,它只是一个小表单控件Java 程序运行后,程序生成的窗口为空且没有组件,java,swing,Java,Swing,我想实现一个可以写入和读取文件的GUI程序,但是程序运行后的界面是空白的,没有任何组件。我使用IDEA,我已经提前创建了文件。这个程序的最终目标是设计一个文本字段和两个按钮。单击“读取文件”按钮时,磁盘文件中的数据信息将显示在文本字段中 单击:Write File按钮时,用户在文本字段中的输入将写入磁盘文件 这是我的程序开始运行时的默认界面。如您所见,它只是一个小表单控件 import java.awt.*; import java.awt.event.ActionEvent; import
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;
}
}
这是我的程序开始运行时的默认界面。如您所见,它只是一个小表单控件
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;
}
}
默认情况下,Swing组件的大小为(0,0)。您需要通过调用所有面板上的布局管理器为所有组件指定一个大小。您可以通过在框架可见之前调用
pack()
方法来执行此操作:
thisClass.pack();
thisClass.setVisible(true);
这与JavaEE无关。我不知道还有什么可能是错误的,但似乎您在initialize()中创建了一个“内容窗格”并返回它,但您没有捕获该返回。您需要捕获它并将其添加到JFrame中。称它为“jContentPane”也有点混乱,因为这是Swing在其他地方使用的一个术语。我敢打赌@arcy会解决您的问题。您似乎正在以一种复杂的方式创建GUI,可能应该将代码简化一点。