我正在尝试保存选项卡式窗格内面板上的textarea的内容。
到目前为止,我已经尝试过:
bw.write(tabbedPane.getComponent(tabbedPane.getSelectedIndex()).toString());
并一直在查看tabbedpane的所有方法,我似乎无法解决它。我知道我必须从tabbedPane
获取所选组件,然后以某种方式从中获取textarea然后将其转换为我假设的字符串?
我打开文件时的代码是:
private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == fileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
JTextArea x = new JTextArea();
JScrollPane scroll = new JScrollPane(x);
p.add(scroll, BorderLayout.CENTER);
x.read( new FileReader( file.getAbsolutePath() ), null );
File selectedFile = fileChooser.getSelectedFile();
String name = selectedFile.getName();
tabbedPane.add(p,name);
tabbedPane.setSelectedComponent(p);
} catch (IOException ex) {
System.out.println("problem accessing file"+file.getAbsolutePath());
}
} else {
System.out.println("File access cancelled by user.");
}
}
我已经按照您的要求添加了课程 更新当前的SaveAs方法:
private void btnSaveAsActionPerformed(java.awt.event.ActionEvent evt) {
int returnVal = fileChooser.showSaveDialog(this);
if (returnVal == fileChooser.APPROVE_OPTION) {
File dir1 = fileChooser.getCurrentDirectory();
String dir = dir1.getPath();
String name = fileChooser.getSelectedFile().getName() + ".txt";
try {
File file = new File(dir,name);
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
JPanel no = (JPanel) tabbedPane.getSelectedComponent();
JTextArea bo = (JTextArea) no.get
bw.write(bo.getText());
bw.close();
tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(), name);
} catch(Exception e) {
System.out.println(e);
}
}
}
当前打开文件方法:
private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == fileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
FilePanel p = new FilePanel(file);
tabbedPane.add(p,p.getName());
tabbedPane.setSelectedComponent(p);
} catch (IOException ex) {
System.out.println("problem accessing file"+file.getAbsolutePath());
}
} else {
System.out.println("File access cancelled by user.");
}
}
我是否需要将新类的新对象添加到数组中,或者将它们插入tabbedPane是否正常?
答案 0 :(得分:2)
这里没有什么魔力,而是你编码的所有方式。包含JTextArea的类应该有一个公共方法,类似于返回JTextArea的getTextArea()
。然后,当您通过getSelectedComponent()
获得选定选项卡的组件时,可以在返回的组件上调用此方法。
修改强>
根据您发布的代码,您需要重新考虑您的程序设计。您的JTextArea是一个局部变量,因此不容易访问,因此存在问题。我建议:
getTextArea()
方法,则只有getTextAreaText()
返回JTextArea保存的文本。你的代码对外部扰动和副作用的限制越多越好。
编辑2
例如,您可以创建一个在JPanel中保存JTextArea的类,类似于:
class FilePanel extends JPanel {
private File file;
private JTextArea textArea;
private String name;
public FilePanel(File file) throws FileNotFoundException, IOException {
this.file = file;
setLayout(new BorderLayout());
textArea = new JTextArea();
JScrollPane scroll = new JScrollPane(textArea);
add(scroll, BorderLayout.CENTER);
textArea.read(new FileReader(file.getAbsolutePath()), null);
name = file.getName();
}
public File getFile() {
return file;
}
public JTextArea getTextArea() {
return textArea;
}
public String getName() {
return name;
}
}
然后每当从JTextPane获取selectedComponent时,请确保它不为null,将其强制转换为FilePanel并在其上调用getTextArea()。
编辑3
例如:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class FooSwing extends JFrame {
private static final int PREF_W = 600;
private static final int PREF_H = 450;
private JFileChooser fileChooser = new JFileChooser();
private JTabbedPane tabbedPane = new JTabbedPane();
public FooSwing() {
JPanel btnPanel = new JPanel();
btnPanel.add(new JButton(new AbstractAction("Open") {
@Override
public void actionPerformed(ActionEvent e) {
btnOpenActionPerformed(e);
}
}));
btnPanel.add(new JButton(new AbstractAction("Get Selected Text") {
@Override
public void actionPerformed(ActionEvent e) {
FilePanel selectedComp = (FilePanel)tabbedPane.getSelectedComponent();
if (selectedComp != null) {
String text = selectedComp.getTextArea().getText();
System.out.println(text);
} else {
System.out.println("No component selected");
}
}
}));
add(tabbedPane, BorderLayout.CENTER);
add(btnPanel, BorderLayout.SOUTH);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
JPanel filePanel = new FilePanel(file);
tabbedPane.add(filePanel, filePanel.getName());
tabbedPane.setSelectedComponent(filePanel);
} catch (IOException ex) {
System.out.println("problem accessing file"
+ file.getAbsolutePath());
}
} else {
System.out.println("File access cancelled by user.");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShow();
}
});
}
private static void createAndShow() {
FooSwing fooSwing = new FooSwing();
fooSwing.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fooSwing.pack();
fooSwing.setLocationRelativeTo(null);
fooSwing.setVisible(true);
}
}
class FilePanel extends JPanel {
private File file;
private JTextArea textArea;
private String name;
public FilePanel(File file) throws FileNotFoundException, IOException {
this.file = file;
setLayout(new BorderLayout());
textArea = new JTextArea();
JScrollPane scroll = new JScrollPane(textArea);
add(scroll, BorderLayout.CENTER);
textArea.read(new FileReader(file.getAbsolutePath()), null);
name = file.getName();
}
public File getFile() {
return file;
}
public JTextArea getTextArea() {
return textArea;
}
public String getName() {
return name;
}
}