2014-03-25 58 views
0

我正在尋找一種方法將用戶輸入從jlist保存到txt文件中。這是一個家庭作業項目。我將程序設置爲從文本字段獲取用戶輸入,然後在它們點擊添加按鈕後進入jlist。 jlist上有一個defaultlistmodel,我想知道是否有創建保存按鈕的方法,並允許它將完整的jlist保存到.txt文件中。將jList數據保存到txt文件的方法?

而且,這裏是我的代碼:

import java.awt.BorderLayout; 
import java.awt.EventQueue; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import javax.swing.DefaultListModel; 
import javax.swing.JOptionPane; 
import javax.swing.JTextField; 
import javax.swing.JButton; 
import javax.swing.JList; 
import javax.swing.border.LineBorder; 

import java.awt.Color; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 

import javax.swing.JComboBox; 
import javax.swing.JProgressBar; 
import javax.swing.JLabel; 


public class i extends JFrame { 

private JPanel contentPane; 
private JTextField jTextField; 
DefaultListModel ListModel = new DefaultListModel(); 
ArrayList<String> arr = new ArrayList <String>(); 
String str; 
/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       i frame = new i(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public i() { 
    super("List"); 

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 450, 300); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    contentPane.setLayout(null); 

    jTextField = new JTextField(); 
    jTextField.setBounds(15, 80, 168, 20); 
    contentPane.add(jTextField); 
    jTextField.setColumns(10); 

    final JList jList = new JList(ListModel); 
    jList.setBorder(new LineBorder(new Color(0, 0, 0))); 
    jList.setBounds(289, 54, 135, 197); 
    contentPane.add(jList); 


    JButton jButton = new JButton("Add"); 
    jButton.setBounds(190, 79, 89, 23); 
    contentPane.add(jButton); 
    jButton.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e) { 
      String str=jTextField.getText(); 
      ListModel.addElement(str); 
      jTextField.setText(""); 
      arr.add(str); 
      System.out.println(arr); 
     } 
    }); 



    JButton delete = new JButton("DELETE"); 
    delete.setBounds(190, 162, 89, 23); 
    contentPane.add(delete); 
    delete.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent k){ 
      ListModel.removeElement(jList.getSelectedValue()); 

     } 
    }); 

    JButton btnUp = new JButton("UP"); 
    btnUp.setBounds(190, 125, 89, 23); 
    contentPane.add(btnUp); 
    btnUp.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      int index = jList.getSelectedIndex(); 
      if(index == -1){ 
       JOptionPane.showMessageDialog(null, "Select something to move."); 
      } else if(index > 0) { 
       String temp = (String)ListModel.remove(index); 
       ListModel.add(index - 1, temp); 
       jList.setSelectedIndex(index -1); 
      } 
     } 
    }); 

    JButton btnDown = new JButton("DOWN"); 
    btnDown.setBounds(190, 196, 89, 23); 
    contentPane.add(btnDown); 
    btnDown.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      int index = jList.getSelectedIndex(); 
      if(index == -1) 
       JOptionPane.showMessageDialog(null, "Select something to move."); 
      else if(index < ListModel.size() - 1){ 
       String temp = (String)ListModel.remove(index); 
       ListModel.add(index + 1, temp); 
       jList.setSelectedIndex(index + 1); 
      } 
     } 
    }); 

    JLabel lblItems = new JLabel("Items:"); 
    lblItems.setBounds(290, 37, 46, 14); 
    contentPane.add(lblItems); 

    JButton btnPrint = new JButton("PRINT"); 
    btnPrint.setBounds(190, 228, 89, 23); 
    contentPane.add(btnPrint); 
    btnPrint.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 

     } 
    }); 
} 
} 
+0

這個問題太廣泛了。哪部分給你帶來麻煩?創建按鈕?添加一個ActionListener到按鈕?從JList獲取數據?將數據保存到文本文件?還有別的嗎?你試過什麼了?你的[MCVE](http://stackoverflow.com/help/mcve)在哪裏? –

+0

@KevinWorkman我很抱歉,我無法弄清楚如何從jlist獲取數據並將其存入文本文件。我看了其他網站和其他問題,並沒有運氣。 – engz

+0

將問題分解成更小的步驟。你能編寫一個獨立的程序,將一些硬編碼的字符串寫入文件嗎?你可以寫一個不同的獨立程序來向控制檯輸出JList的內容嗎?當你有兩個程序單獨工作時,你可以考慮將它們結合起來。您將有更好的運氣發佈關於某個特定步驟的信息,而不是發佈有關您的整個目標。 –

回答

0

你需要使用文件級的數據保存在

File file = new File("c:/test/myfile.txt"); 

列表現在文件的創建並打開了! 和寫一些輸出到文件

使用格式化類:

public class CreateFile 
{ 
    private Formatter x; 
    void openFile() 
    { 
    try 
    { 
     x = new Formatter("c:/myfile.txt"); 
     System.out.println("you created a file"); 
    } 
    catch(Exception e) 
    { 
     System.err.println("error in opening file"); 
    } 
    } 
    void addFile() 
    { 
     x.format("%s %s %s\n","20","Start","Today"); 
    } 
    void closeFile() 
    { 
     x.close(); 
    } 
} 

正如你看到的,addFile方法接受3字符串傳遞的文件 我通過了「20」,「開始」,在寫「今天「 你可以使用多於/少於%s,並傳遞用戶輸入或String中的列表來寫入文件。 祝你好運