JFrame无法正常运作

时间:2019-03-17 02:03:01

标签: java swing sorting jframe

我写出了应该读取文件的代码,使用字符串标记器遍历文件,实例化Car对象并将对象(String,String,int,int)存储在2个不同的列表中,其中一个按品牌(如汽车品牌)排序,不按品牌排序,它们显示在2列JFrame上。

我不知道我在做什么错。每当我运行它时,框架就完全空了,这让我发疯。我不知道这是框架设置还是readFile方法本身的问题。

这是CarGUI类,在这里我设置框架和readFile方法。

import java.awt.GridLayout;
import javax.swing.JTextArea;
import javax.swing.JFrame;

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

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

    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("Project 1");
   }




 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.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(), 5, 20);
        this.rightTextArea = new JTextArea(this.rightSide.toString(), 5, 20);
        this.getContentPane().add(this.leftTextArea);
        this.getContentPane().add(this.rightTextArea);
        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());
        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);
}

这是应该发生魔术的selectionSort方法:

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
        }

    }
}

Project1

import java.io.FileNotFoundException;


public class Project1 {

    public static void main(String arg[]) throws FileNotFoundException{
         String file = "cars.txt";
            CarGUI testGUI = new CarGUI();
            testGUI.readFile(file);

    }
}

selectionSort方法可能存在问题。由于Eclipse不接受,因此我使用temptemp2notTemp进行了设置 sortedList2.get(i) = sortedList2.get(min);,因为

The left-hand side of an assignment must be a variable

有什么想法吗?不要问为什么我使用选择排序方法,这是必需的。 :(

0 个答案:

没有答案