所以我有一个简单的GUI,只能打开文本文件,应该只是显示在要编辑的文本区域。我知道我的字符串包含文件内容,因为我可以打印出来,但是当我尝试将它添加到我的文本区域时,它没有显示出来。我想知道这是否是重叠文本区域的问题,但我似乎找不到错误。
我的代码的第一部分只是创建GUI。另一部分应该打开一个文件并用它填充文本区域。问题到底在哪里,我该如何解决?如有任何帮助,不胜感激。
这是我的代码的一部分,处理创建框架和面板:
public class MenuView extends JFrame {
private JPanel centerPanel;
private JPanel bottomPanel;
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenuItem openItem;
private JMenuItem closeItem;
private JButton setButton;
private JTextField text;
private JTextArea label;
private JMenuItem fileNew;
public MenuView(){
super();
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setTitle("Menu Demo");
//The center panel that will contain text
centerPanel = new JPanel();
centerPanel.setLayout(new FlowLayout());
label = new JTextArea(400,500);
centerPanel.add(label);
add(centerPanel, BorderLayout.CENTER);
//The bottom panel with the text field and button
bottomPanel = new JPanel();
bottomPanel.setLayout(new GridLayout(1, 2));
setButton = new JButton("Set Text");
text = new JTextField();
bottomPanel.add(setButton);
bottomPanel.add(text);
add(bottomPanel, BorderLayout.SOUTH);
//Setting up the menu
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
fileNew = new JMenu("New");
openItem = new JMenuItem("Open");
closeItem = new JMenuItem("Exit");
fileMenu.add(openItem);
fileMenu.add(closeItem);
fileMenu.add(fileNew);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
setButton.addActionListener(new ButtonCommand(label, text));
closeItem.addActionListener(new QuitMenuCommand());
openItem.addActionListener(new OpenMenuCommand(label));
}
public static void main(String [] args){
MenuView v = new MenuView();
v.setVisible(true);
}
}
下面是处理打开文件的代码:
public class OpenMenuCommand implements ActionListener {
private JTextArea theLabel;
private JFileChooser fc;
private String k = "";
public OpenMenuCommand(JTextArea l){
theLabel = l;
theLabel.getParent();
fc = new JFileChooser();
fc.setFileFilter(new FileNameExtensionFilter("Text file", "txt"));
}
public void actionPerformed(ActionEvent e) {
StringBuffer text = new StringBuffer();
int returnValue = fc.showOpenDialog(null);
if(returnValue == fc.APPROVE_OPTION){
theLabel.removeAll();
File f = fc.getSelectedFile();
try{
BufferedReader inFile = new BufferedReader(new FileReader(f));
String in = inFile.readLine();
while(in != null){
k = k + in;
in = inFile.readLine();
}
System.out.println(k);
theLabel.setText(k);
inFile.close();
theLabel.setVisible(true);
}catch(FileNotFoundException exc){
//Should never trigger
}catch(IOException exc){
theLabel.setText("Error reading in file.");
}
}
}
}
你的东西被添加到JTextArea,但你没有看到由于JTextArea的大小。实际上是一个非常大的JTextArea:
label = new JTextArea(400, 500);
并且通过将巨大的JTextArea添加到使用flowlayout的JPanel中,它的大部分都离开了屏幕。要明白我的意思,把这个添加到你的actionPerformed方法中:
System.out.println(theLabel.getBounds());
你会看到宽度和高度都很大,更重要的是,左边是一个很大的负数。
解决方案:使你的JTextArea更合理的大小(在API中看到这些数字意味着什么——行和列,而不是点),并将其添加到JScrollPane,然后添加BorderLayout。
例如,小GUI SSCCE在FlowLayout中使用左侧的JPanel显示JTextArea,在右侧的JScrollPane中显示JTextArea:
import java.awt.*;
import javax.swing.*;
public class MenuViewSSCCE {
private static final Dimension APP_SIZE = new Dimension(500, 400);
private static void createAndShowUI() {
JTextArea label = new JTextArea(400, 500); // friggin big!
JTextArea label2 = new JTextArea(400, 500); // friggin big!
label.setText("Look at how big this JTextArea is!");
label2.setText("Look at how big this JTextArea is!");
JPanel centerPanel = new JPanel();
centerPanel.setPreferredSize(APP_SIZE);
centerPanel.setLayout(new FlowLayout()); // this line is redundant
centerPanel.add(label);
JScrollPane myScrollpane = new JScrollPane(label2);
myScrollpane.setPreferredSize(APP_SIZE);
JPanel gridPanel = new JPanel(new GridLayout(1, 0));
gridPanel.add(centerPanel);
gridPanel.add(myScrollpane);
JFrame frame = new JFrame("Your code on left, mine on right");
frame.getContentPane().add(gridPanel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}