java - GUI 上仅打印第一行

标签 java swing user-interface

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

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

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

问题:目前我所能实现的就是将文件的第一行打印到 GUI 上。我尝试弄乱 leftSide.append(unsortedList.get(i).toString() + "\n"); 及其右侧对应项的位置,但无济于事。我不确定这是 readFile 方法的问题、实现 JFrame 的问题还是负责迭代将 arrayList 附加到 StringBuilder 的低效 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

最佳答案

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

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);
        }
        if(scanner.hasNextLine()){
            line = scanner.nextLine();
            tokenizer = new StringTokenizer(line, delimiter);
            tokenCount = new StringTokenizer(line, ",").countTokens(); //counts the tokens, should yield 4 
        }
        else{
            break;
        }
    }
    scanner.close();
    selectionSort(sortedList);
}

关于java - GUI 上仅打印第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55203834/

相关文章:

javascript - Angular 2/4 使用父/子组件管理表单 CRUD

java - 如何使用log4j2记录log4j日志

java - Junit 测试获取 NoSuchElementException 但仅限于特定数字

java - 在按钮单击内显示加载 GIF,并在执行操作后再次隐藏它

java - 如何构造 JTextfield,以及如何使用 selectAll() 方法

user-interface - 使用Xcode 4添加 Cappuccino CPProgressIndicator

c++ - 无法将 CLion 链接到 Windows 10 上的 QT

java printf 格式带有字符串参数的 double

javascript - 程序到 2D Array 复杂对齐

java - 使用按钮继承