2013-05-02 121 views
1

我有一个类从文本文件读取并将内容写入JTextArea。这工作正常,但即时无法将窗格的内容写回到不同的文本文件。其目的是让其他内容可以手动写入文本区域,然后在需要时保存到文件中。我有一个按钮,但它似乎没有做任何事情,当点击。任何建议都会很棒。无法读取JTextArea并写入文件

请注意,代码包含三个窗格(卡片)。我目前只使用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(); 
     } 
    } 

    } 
} 
+0

您的saveOne **可能**工作正常,但我敢打赌,您没有在文件的正确位置查找。你看过用户的目录吗?你知道,你可以通过println来查看'file.getAbsolutePath()'返回的文件。 – 2013-05-02 03:28:49

+0

您是否收到任何错误消息? – Sanchit 2013-05-02 03:31:48

+4

使用['JTextComponent.write(Writer)'](http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#write%28java.io.Writer%290 )代替。 – 2013-05-02 03:33:59

回答

1

actionPerformed1(ActionEvent的F)中没有的ActionListener定义的方法。这里是你的固定代码。

package javaapplication7; 

/** 
    * 
    * @author imran 
    */ 
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.*; 
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("C:/Users/imran/Desktop/JavaApplication7/src/javaapplication7/mobile.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(); 

    card1.add(saveOne); 
    saveOne.addActionListener(this); 

    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 actionPerformed(ActionEvent f) { 

System.out.println("hi"); 
    CardLayout cl = (CardLayout) (cards.getLayout()); 
String cmd = f.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); 
} 
if (f.getSource().equals(saveOne)) { 
    try { 
     System.out.println("hello"); 
     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(); 
    } 
} 

} 
public static void main(String[] args) { 
    new MobileHome(); 
} 
} 

我刚才删除你的代码中actionPerformed1(动作事件f)和复制此方法的所有内容中的actionPerformed(ActionEvent的E)。

+0

伊姆兰,你是一个明星,谢谢你这么多:) – 2013-05-02 04:05:21