【问题标题】:Saving JComboBoxes values to Text File将 JComboBoxes 值保存到文本文件
【发布时间】:2015-03-25 09:56:36
【问题描述】:

我正在制作一个小 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);
 }
}

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

 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 也不应出现在新保存的文本文件中。 任何帮助将不胜感激

【问题讨论】:

    标签: java swing jcombobox


    【解决方案1】:

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

        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 字符串的不同行(删除带有拆分的行分隔符),并且仅当它不是第 0 行或第 2 行时才添加一个新行。

    【讨论】:

      【解决方案2】:
      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();
              }
          }
      });
      

      【讨论】:

        猜你喜欢
        • 2012-09-27
        • 2021-12-13
        • 1970-01-01
        • 2016-10-28
        • 1970-01-01
        • 1970-01-01
        • 2021-12-24
        • 1970-01-01
        • 2015-09-06
        相关资源
        最近更新 更多