GUI上仅打印第一行

时间:2019-03-17 04:24:51

标签: java swing user-interface

给定C辆车(未提供编号),为每辆车输入两个字符串,品牌和型号,两个整数,年份和里程。 (使用stringTokenizer帮助输入)

将C车存储在两个不同的ArrayList中。一个是按Make排序的,另一个不是。

它们将被打印在GUI窗口的不同末端。

问题:目前,我所能实现的就是将文件的第一行打印到GUI上。我尝试弄乱leftSide.append(unsortedList.get(i).toString() + "\n");的位置及其正确的位置,但无济于事。我不确定这是readFile方法的问题,实现JFrame的问题还是负责通过将arrayLists追加到StringBuilders进行迭代的效率低下的for循环。

@SuppressWarnings("serial")
public class CarGUI extends JFrame{
    private JTextArea leftTextArea;
    private JTextArea rightTextArea;
    private StringBuilder leftSide;
    private StringBuilder rightSide;

    public static ArrayList<Car> unsortedList = new ArrayList<Car>();
    public static ArrayList<Car> sortedList = new ArrayList<Car>();

     public CarGUI() //default constructor for the GUI class
   {
        // Instance variables
       this("TITLE");
   }



     public CarGUI(String title) //the 1-argument parameter constructor 
        {
            // Call the super class constructor to initialize the super
            // class variables before initializing this class's variables
            super(title);

            // Configure the JFrame
            // Configure the JFrame components we inherited
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(400, 400);
            this.setLocation(200, 200);


            this.getContentPane().setLayout(new GridLayout(1, 2)); //1 row and 2 column
            this.leftSide = new StringBuilder("Unsorted Cars\n"); 
            this.rightSide = new StringBuilder("Sorted Cars\n");
            this.leftTextArea = new JTextArea(this.leftSide.toString());
            this.rightTextArea = new JTextArea(this.rightSide.toString());
            this.getContentPane().add(this.leftTextArea);
            this.getContentPane().add(this.rightTextArea);

            this.setVisible(true);
        }


public void readFile(String file) throws FileNotFoundException{
    File myFile = new File("Cars.txt");
    Scanner scanner = new Scanner(myFile);
    String line = scanner.nextLine();
    String delimiter = ",";
    StringTokenizer tokenizer = new StringTokenizer(line, delimiter);
    int tokenCount = new StringTokenizer(line, ",").countTokens(); //counts the tokens, should yield 4 
    while(tokenizer.hasMoreTokens()){ 
        if(tokenCount != 4){ //if there isn't exactly 4 tokens, print the rest to the console
            System.out.println(tokenizer.toString());
        }
        else {
            //newCar(Make, Model, Year, Mileage);
            Car newCar = new Car(tokenizer.nextToken(), tokenizer.nextToken(), Integer.parseInt(tokenizer.nextToken()), Integer.parseInt(tokenizer.nextToken()));
            unsortedList.add(newCar);
            sortedList.addAll(unsortedList);
            scanner.close();
        }
    }
    selectionSort(sortedList);
}



public void selectionSort(ArrayList<Car> sortedList2) {
        for (int i = 0; i < sortedList2.size(); i++) {
            int min = i;            
            for (int j = min + 1; j < sortedList2.size(); j++)
                if (sortedList2.get(j).getMake().compareTo(sortedList2.get(min).getMake()) < 0)
                    min = j;  
            Car temp1 = sortedList2.get(i);
            Car temp2 = sortedList2.get(i);
            Car notTemp = sortedList2.get(min);
            temp1 = notTemp; // sortedList2.get(i) = sortedList2.get(min);
            notTemp = temp2; // sortedList2.get(min) = sortedList2.get(i);
            //doing this because I need to set a variable on the left-hand side
        }
        for(int i = 0; i < sortedList.size(); i++){
            leftSide.append(unsortedList.get(i).toString() + "\n");
            rightSide.append(sortedList.get(i).toString() + "\n");
            }
         this.leftTextArea.setText(this.leftSide.toString());
         this.rightTextArea.setText(this.rightSide.toString());

    }   
}

代码有点混乱,我只提供了这个类,因为其他类只是简单的main和Car类。这样一共3个。

car.txt文件看起来像

Subaru,Forester,2018,12902
Toyota,Camry,2016,24536
Nissan,Maxima,2009,45648
Honda,Civic,2002,98304
Subaru,Legacy,2014,2034
Hyundai,Kona,2012,27890
Toyota,Rav4,2013,6547
Honda,Accord
Honda,CR-V,2010,13904
Nissan,Altima,2012,45376
Honda,Pilot,2013,54398
Nissan,Leaf,2018,2300
Acura,MDX,2017,3892

1 个答案:

答案 0 :(得分:3)

您正在读取文件仅一行。行读取代码不在您的循环中,并且您在第一次迭代中关闭了扫描仪。像这样修复它。

11111111