2011-05-21 71 views
2

我一直在使用「學習Java第2版」一書,試圖讓我的Java應用程序將我的輸入寫入名爲properties的文本文件。我已經將教科書示例操縱到自己的代碼中,但仍然遇到問題試圖使其工作。我想我可能需要將它與我的提交按鈕掛鉤,但本章沒有提及。如何使用java寫入文本文件?

基本上我試圖將信息存儲在文本文件中,然後在另一個位置使用該文本文件來讀取所有屬性的詳細信息。

這是我的代碼爲AddProperty頁面迄今爲止任何意見將不勝感激。 目前四號撞牆。

/** 
* 
* @author Graeme 
*/ 
package Login; 

import java.io.*; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.border.EmptyBorder; 

public class AddProperty 
{ 
public void AddProperty() 
{ 

    JFrame frame = new JFrame("AddPropertyFrame"); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    // having to set sizes of components is rare, and often a sign  
    // of problems with layouts. 
    //frame.setSize(800,600); 
    JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20,20)); 
    // make it big like the original 
    panel.setBorder(new EmptyBorder(100,20,100,20)); 
    frame.add(panel); 
    //panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); 

    JLabel HouseNumber = new JLabel("House Number/Name"); 
    panel.add(HouseNumber); 
    JTextField HouseNumber1 = new JTextField(10); 
    panel.add(HouseNumber1); 

    JLabel HousePrice = new JLabel("House Price"); 
    panel.add(HousePrice); 
    JTextField HousePrice1 = new JTextField(10); 
    panel.add(HousePrice1); 

    JLabel HouseType = new JLabel("House Type"); 
    panel.add(HouseType); 
    JTextField HouseType1 = new JTextField(10); 
    panel.add(HouseType1); 

    JLabel Location = new JLabel("Location"); 
    panel.add(Location); 
    JTextField Location1 = new JTextField(10); 
    panel.add(Location1); 

    JButton submit = new JButton("Submit"); 
    panel.add(submit); 
    submit.addActionListener(new Action()); 

    // tell the GUI to assume its natural (minimum) size. 
    frame.pack(); 
} 

static class Action implements ActionListener{ 

    @Override 
    public void actionPerformed (ActionEvent e) 
    { 
     // this should probably be a modal JDialog or JOptionPane. 
     JOptionPane.showMessageDialog(null, "You have successfully submitted a property."); 
    } 

    static class propertyList 
    { 
     public static void main (String args[]) throws Exception { 
      File properties = new File(args[0]); 

      if (!properties.exists() || !properties.canRead()) { 
       System.out.println("Cant read " + properties); 
       return; 
      } 
      if (properties.isDirectory()){ 
       String [] properties1 = properties.list(); 
       for (int i=0; i< properties1.length; i++) 
        System.out.println(); 
      } 
      else 
       try { 
        FileReader fr = new FileReader (properties); 
        BufferedReader in = new BufferedReader (fr); 
        String line; 
        while ((line = in.readLine())!= null) 
        System.out.println(line); 
       } 
      catch (FileNotFoundException e){ 
       System.out.println("Not Able To Find File"); 
      } 
     } 
    } 
} 

}

回答

2

在你的行動進行你沒有說明任何東西,例如在你的行動進行,您可以添加。

public void actionPerformed(ActionEvent e) 
      { 

       houseNumber2 = houseNumber1.getText(); 
       housePrice2 = housePrice1.getText(); 
       town1 = town.getText(); 
       comboBoxType2 = comboBoxType1.getSelectedItem(); 


      inputData = housenumber2 + "," + housePrice2 + "," + town1 + "," + comboBoxType2; 
      FileName.Filewritermethod(inputData); 
      frame.setVisible(false); 

      } 
     }); 

這串把你JTexFields的值,並將它們傳遞到文本文件只要你有一個FileWriter類

1

你的動作偵聽器,沒有做任何事情太多現在。

代碼添加到下面的方法添加屬性:

public void actionPerformed (ActionEvent e) 
{ 
    //add your code here 
} 
+0

我不認爲這是。所以基本上我需要嘗試將我的按鈕鏈接到寫入方法? – 2011-05-21 12:56:16

+0

允許main拋出異常並不是一個好習慣。 – ncmathsadist 2011-05-21 13:00:48

+0

我已經有了一個public void actionPerformed(ActionEvent e)目前dosnt真的包含很多的聲明。如果我嘗試將我的編寫代碼移動到該部分中,它會出現一個「非法啓動表達式」,我該如何克服這一點? – 2011-05-21 13:02:32