将JComboBoxes值保存到文本文件

时间:2015-03-25 09:56:36

标签: java swing jcombobox

我正在制作一个小gui,其中我从文本文件中读取数据到不同的JComboBoxes,用户在JComboBoxes中编辑,然后将其保存在新的文本文件中。我的新文件被保存但不是以它读取的格式。 代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;

public class A extends JPanel 
{
public A() {

JPanel buttonPanel = new JPanel();
add(buttonPanel);
buttonPanel.setLayout(new GridLayout(0, 2, 10, 10));


JComboBox combo1 = new JComboBox();
combo1.addItem("1ms");
combo1.addItem("2ms");
buttonPanel.add(combo1);

JComboBox combo2 = new JComboBox();
combo2.addItem("1ms");
combo2.addItem("2ms");
buttonPanel.add(combo2);

JComboBox combo3 = new JComboBox();
combo3.addItem("1ms");
combo3.addItem("2ms");
buttonPanel.add(combo3);

JComboBox combo4 = new JComboBox();
combo4.addItem("1ms");
combo4.addItem("2ms");
buttonPanel.add(combo4);


JComboBox[] fieldBoxs = new JComboBox[4];
fieldBoxs[0] = combo1;
    fieldBoxs[1] = combo2;
    fieldBoxs[2] = combo3;
    fieldBoxs[3] = combo4;


ArrayList<String> list = new ArrayList<String>();

final StringBuilder temp = new StringBuilder();

try{
    InputStream ips=new FileInputStream("test.txt");
        InputStreamReader ipsr=new InputStreamReader(ips);
        BufferedReader br=new BufferedReader(ipsr);

            String line;
    boolean found = false;

        while ((line=br.readLine())!=null) {
        String[] s = line.split(" ");
        list.add(s[0]);
        list.add(s[1]);
if (s[0].equals("0") && !found)
    {
        found = true;
        temp.append(line).append("\r\n");
    }
    else if (found)
    {
        temp.append(line).append("\r\n");
    } 
    }
br.close(); 
    }       
    catch (Exception e){
        e.printStackTrace();
    }

for(int i = 0; i < fieldBoxs.length; i++) {
    fieldBoxs[i].setSelectedItem(list.get(i));
}

JButton button = new JButton("SAVE");
buttonPanel.add(button);
button.addActionListener( new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {

     try { 
            StringBuilder con = new StringBuilder();
            for (int i = 0; i < fieldBoxs.length; i++) 

                   {
                    Object Value = fieldBoxs[i].getSelectedItem();
                    con.append(" ");
            con.append(Value);
            con.append("\r\n");
           }

    FileWriter filewriter = new FileWriter(new File("A.txt"));
    filewriter.write(con.append(temp).toString());
        filewriter.flush();
    } catch (Exception ex) {
        ex.printStackTrace();
        }
    }
});
}
public static void main(String[] args) {
A app = new A();
JFrame m = new JFrame("A");
m.getContentPane().add(app);
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m.pack();
m.setVisible(true);
 }
}

这是我正在读取值的文件:test

 2ms 1ms
 1ms 2ms
 0 0
 1 2
 2 1
 1 1
 2 2

这就是我现在保存文件的方式:

 2ms
 1ms
 1ms
 2ms
 0 0
 1 2
 2 1
 1 1
 2 2

我想将其另存为:

 2 1
 1 2
 0 0
 1 2
 2 1
 1 1
 2 2

格式应与其读取的格式相同,ms也不应出现在新保存的文本文件中。 任何帮助都会得到真正的赞赏

2 个答案:

答案 0 :(得分:0)

这里有一些代码可以正确格式化您的示例。我不建议使用此代码,如果它用于工作或长期使用,因为它不灵活或不好。

    String[] cons = con.split("\n");
    for(int i = 0; i < cons.length; i++){
        if(i == 0 || i == 2){
            cons[i] = cons[i] + " ";
        }else{
           cons[i] = cons[i] + "\n"; 
        }
       }
    StringBuilder builder = new StringBuilder();
    for (String s : cons) {
        builder.append(s);
    }
    con = builder.toString();¨

基本上,代码遍历con字符串的不同行(删除带有拆分的lineseperator),并且只有当它不是0或2行时才添加新的。

答案 1 :(得分:0)

JButton button = new JButton("SAVE");
buttonPanel.add(button);
button.addActionListener( new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {

     try { 

        PrintWriter out = new PrintWriter(new File("A.txt"));
        out.print(fieldBoxs[0].getSelectedItem());
        out.print(" ");
        out.print(fieldBoxs[1].getSelectedItem());
        out.println();
        out.print(fieldBoxs[2].getSelectedItem());
        out.print(" ");
        out.print(fieldBoxs[3].getSelectedItem());
        out.println();
        out.print(temp);        
        out.close();    
    } catch (Exception ex) {
        ex.printStackTrace();
        }
    }
});