텍스트 파일에서 읽고 JTextArea에 내용을 쓰는 클래스가 있습니다. 이 잘 작동하지만 다른 텍스트 파일에 다시 창의 내용을 쓸 수 없습니다. 목적은 텍스트 영역에 다른 내용을 손으로 쓴 다음 필요에 따라 파일에 저장하는 것입니다. 버튼이 있지만 클릭 할 때 아무 것도하지 않는 것 같습니다. 어떤 충고라도 잘 될 것입니다.JTextArea에서 읽을 수없고 파일에 쓸 수 없습니다.
코드에 3 개의 창 (카드)이 있습니다. 나는 그 순간에 card1만을 사용하고 있습니다.
감사
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
public class MobileHome extends JFrame implements ActionListener {
final String card1Text = "Card 1";
final String card2Text = "Card 2";
final String card3Text = "Card 3";
final JPanel cards; //a panel that uses CardLayout
// button commands
final String FIRST = "FIRST";
final String NEXT = "NEXT";
final String PREVIOUS = "PREVIOUS";
final String LAST = "LAST";
File file = new File("mobOne.txt");
BufferedReader reader = null;
public String read()
{
String savetext = "";
try{
reader = new BufferedReader(new FileReader(file));
String text = null;
while((text = reader.readLine()) != null){
final String LS = System.getProperty("line.separator");
savetext += text + LS;
}
}
catch(IOException jim){
jim.printStackTrace();
}
return savetext;
}
public void actionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout) (cards.getLayout());
String cmd = e.getActionCommand();
if (cmd.equals(FIRST)) {
cl.first(cards);
} else if (cmd.equals(NEXT)) {
cl.next(cards);
} else if (cmd.equals(PREVIOUS)) {
cl.previous(cards);
} else if (cmd.equals(LAST)) {
cl.last(cards);
}
}
JButton saveOne = new JButton("Save to file");
JTextArea mobOneText = new JTextArea("TextField on Card 1", 15, 20);
public MobileHome() {
super("Mobile Wizard");
//Create the "cards".
JPanel card1 = new JPanel();
saveOne.addActionListener(this);
card1.add(saveOne);
card1.add(mobOneText);
mobOneText.setText(read());
card1.setBackground(new Color(255,0,0));
JPanel card2 = new JPanel();
card2.add(new JTextField("TextField on Card 2", 20));
card2.setBackground(new Color(0,255,0));
JPanel card3 = new JPanel();
card3.add(new JLabel("Card 3"));
card3.setBackground(new Color(0,0,255));
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, card1Text);
cards.add(card2, card2Text);
cards.add(card3, card3Text);
JButton btn1 = new JButton("First");
btn1.setActionCommand(FIRST);
btn1.addActionListener(this);
JButton btn2 = new JButton("Next");
btn2.setActionCommand(NEXT);
btn2.addActionListener(this);
JButton btn3 = new JButton("Previous");
btn3.setActionCommand(PREVIOUS);
btn3.addActionListener(this);
JButton btn4 = new JButton("Last");
btn4.setActionCommand(LAST);
btn4.addActionListener(this);
JPanel controlButtons = new JPanel();
controlButtons.add(btn1);
controlButtons.add(btn2);
controlButtons.add(btn3);
controlButtons.add(btn4);
ImageIcon img = new ImageIcon("hand.jpg");
setIconImage(img.getImage());
Container pane = this.getContentPane();
pane.add(cards, BorderLayout.CENTER);
pane.add(controlButtons, BorderLayout.PAGE_END);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
setVisible(true);
}
public void actionPerformed1(ActionEvent f) {
if (f.getSource().equals(saveOne)) {
try {
BufferedWriter fileOut = new BufferedWriter(new FileWriter("filename.txt"));
String myString1 =mobOneText.getText();
String myString2 = mobOneText.getText();
System.out.println(myString2);
fileOut.write(myString1);
fileOut.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
당신의 saveOne은 아마도 ** 잘 작동하고 있습니다 만, 나는 당신이 파일의 올바른 위치를 찾고 있지 않을 것이라고 확신합니다. 사용자의 디렉토리를 보았습니까? println을 사용하여'file.getAbsolutePath()'가 파일을 찾기 위해 리턴하는 것을 볼 수있다. –
오류 메시지가 나타 납니까? – Sanchit
['JTextComponent.write (Writer)'] 사용 (http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#write%28java.io.Writer%290)) 대신. –