Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带有排序JScrollPane的建议_Java_Sorting_Collections_Jscrollpane - Fatal编程技术网

Java 带有排序JScrollPane的建议

Java 带有排序JScrollPane的建议,java,sorting,collections,jscrollpane,Java,Sorting,Collections,Jscrollpane,我目前很难弄清楚如何排序和显示一个显示矩形对象的长度、宽度、周长和面积的列表。目前,JScrollPane gui将显示22行这样的内容: 随机长度空间随机宽度空间计算周长空间计算面积 这都是在一个22大小的向量中,它被转换成一个toString,依此类推。现在要求的一部分是为每个相应的值设置一个按钮,该按钮将排序显示该值的最高值。示例:我按下宽度按钮,将按降序显示列表。但我遇到的问题是,我不确定如何实际排序,因为collection.sort在我尝试执行以下操作时会给我一条错误消息: Coll

我目前很难弄清楚如何排序和显示一个显示矩形对象的长度、宽度、周长和面积的列表。目前,JScrollPane gui将显示22行这样的内容:

随机长度空间随机宽度空间计算周长空间计算面积

这都是在一个22大小的向量中,它被转换成一个toString,依此类推。现在要求的一部分是为每个相应的值设置一个按钮,该按钮将排序显示该值的最高值。示例:我按下宽度按钮,将按降序显示列表。但我遇到的问题是,我不确定如何实际排序,因为collection.sort在我尝试执行以下操作时会给我一条错误消息: Collections.sortRectangleStringList,宽度;表示类型集合中的方法sortList、Comparator不适用于参数向量int。 根据我对collections.sort的理解,第一个变量是要排序的列表,而第二个变量应该是实现comparator的变量。我尝试在构造函数上这样做,它创建并将所有不同的元素放到GUI上,但给了我一条错误消息。 如果我不清楚我在问什么,下面是代码:

package a3part3;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;

import a2.Rectangle;

public class RectangleListGUI extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;

    private final int FRAME_X = 100, FRAME_Y = 100, FRAME_WIDTH = 400,
            FRAME_HEIGHT = 333;
    private final int SCROLL_X = 2, SCROLL_Y = 0, SCROLL_WIDTH = 300,
            SCROLL_HEIGHT = 200;
    private final int WIDTHSORT_X = 150, WIDTHSORT_Y = 205,
            WIDTHSORT_WIDTH = 150, WIDTHSORT_HEIGHT = 30;
    private final int LENGTHSORT_X = 0, LENGTHSORT_Y = 205,
            LENGTHSORT_WIDTH = 150, LENGTHSORT_HEIGHT = 30;
    private final int PERIMETERSORT_X = 0, PERIMETERSORT_Y = 235,
            PERIMETERSORT_WIDTH = 150, PERIMETERSORT_HEIGHT = 30;
    private final int AREASORT_X = 150, AREASORT_Y = 235, AREASORT_WIDTH = 150,
            AREASORT_HEIGHT = 30;

    private String SORTWIDTH_STRING = "Sort by Width";
    private String SORTLENGTH_STRING = "Sort by Length";
    private String SORTPERIMETER_STRING = "Sort by Perimeter";
    private String SORTAREA_STRING = "Sort by Area";

    private JButton width;
    private JButton length;
    private JButton perimeter;
    private JButton area;

    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenuItem clearFile;
    private JMenuItem openFile;
    private JMenuItem saveFile;

    private JScrollPane scroll;
    private Vector<Rectangle> RectangleArrayList;
    private Vector<String> RectangleStringList;
    private JList<String> RectangleJList;
    private JFileChooser fileChooser;

    public static final int SAVE_DIALOG = 0;
    private int x, y, x2, y2;

    /**
     * constructor
     * 
     * @param string
     */
    public RectangleListGUI(String title) {
        super(title);
        setBounds(FRAME_X, FRAME_Y, FRAME_WIDTH, FRAME_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);
        /*
         * buttons
         */
        width = new JButton(SORTWIDTH_STRING);
        width.addActionListener(this);
        width.setBounds(WIDTHSORT_X, WIDTHSORT_Y, WIDTHSORT_WIDTH,
                WIDTHSORT_HEIGHT);
        add(width);

        length = new JButton(SORTLENGTH_STRING);
        length.addActionListener(this);
        length.setBounds(LENGTHSORT_X, LENGTHSORT_Y, LENGTHSORT_WIDTH,
                LENGTHSORT_HEIGHT);
        add(length);

        perimeter = new JButton(SORTPERIMETER_STRING);
        perimeter.addActionListener(this);
        perimeter.setBounds(PERIMETERSORT_X, PERIMETERSORT_Y,
                PERIMETERSORT_WIDTH, PERIMETERSORT_HEIGHT);
        add(perimeter);

        area = new JButton(SORTAREA_STRING);
        area.addActionListener(this);
        area.setBounds(AREASORT_X, AREASORT_Y, AREASORT_WIDTH, AREASORT_HEIGHT);
        add(area);

        RectangleStringList = new Vector<String>();
        RectangleArrayList = new Vector<Rectangle>();
        Rectangle J;
        for (int i = 0; i < 3; i++) {
            x = (int) (Math.random() * 100);
            y = (int) (Math.random() * 100);
            x2 = (x * 2) + (y * 2);
            y2 = x * y;
            J = new Rectangle(x, y, x2, y2);
            RectangleArrayList.add(J);
        }
        for (Rectangle x : RectangleArrayList) {
            String outputs = x.toString();
            System.out.println(outputs);
            RectangleStringList.add(outputs);
        }

        RectangleJList = new JList<String>(RectangleStringList);
        RectangleJList.setVisible(true);
        scroll = new JScrollPane(RectangleJList);
        scroll.setBounds(SCROLL_X, SCROLL_Y, SCROLL_WIDTH, SCROLL_HEIGHT);
        add(scroll);
        scroll.setVisible(true);

        menuBar = new JMenuBar();
        fileMenu = new JMenu("File");
        menuBar.add(fileMenu);

        clearFile = new JMenuItem("Clear File");
        clearFile.addActionListener(this);
        fileMenu.add(clearFile);

        openFile = new JMenuItem("Open File");
        openFile.addActionListener(this);
        fileMenu.add(openFile);
        fileChooser = new JFileChooser();

        saveFile = new JMenuItem("Save File");
        saveFile.addActionListener(this);
        fileMenu.add(saveFile);

        setJMenuBar(menuBar);

        setVisible(true);
    }

    public static void main(String[] args) {

        new RectangleListGUI("My Rectangle List");

    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        Object Action = ae.getSource();
        // repaint();
        if (Action.equals(width)) {
            Collections.sort(RectangleStringList);
            repaint();
        } else if (Action.equals(length)) {
            Collections.sort(RectangleStringList);
            repaint();
        } else if (Action.equals(perimeter)) {
            Collections.sort(RectangleStringList);
            repaint();
        } else if (Action.equals(area)) {
            Collections.sort(RectangleStringList);
            repaint();
        } else if (Action.equals(openFile)) {
            int returnValue = fileChooser.showOpenDialog(this);
            repaint();
        } else if (Action.equals(saveFile)) {

        } else if (Action.equals(clearFile)) {
            RectangleStringList.clear();
            repaint();
        }
    }
}

1.不要使用向量,而是使用某种列表。2-使用,这将允许自定义如何进行每种排序,而不影响基础类…那么您将如何接近数字1,或者换句话说,您将使用哪种类型的列表?我应该用列表替换所有向量吗?JList是否也在列表中?对不起,我个人对此非常缺乏经验,ArrayList和no JList不属于列表的范围
package a2;

import java.awt.Color;
import java.awt.Graphics2D;

/**
 * Title: Rectangle class for CSIS 235 Assignment #2 Description: CS 235
 * Rectangle class is a server class that is used by
 * RectangleGUI and is used by Square class as well
 * 
 * @author Mr.Mysterious
 *      * @date 10/17/2014
 */

/**
 * @invariant x1 >= 0 && y1 >= 0 && x2 >= 0 && y2 >= 0 && color != null
 * 
 */
public class Rectangle {

    private int x1, y1;
    private int x2, y2;
    private Color color;
    private int width, length;
    private int perimeter, area; 

    /**
     * method that gets x1
     * @require x1 >= 0
     * 
     * @ensure x1 >= 0
     * @return x1
     */
    public int getX1() {
        return x1;
    }

    /**
     * method that sets x1 to an instance of x1
     * @require x1 >= 0
     * 
     * @ensure this.x1>= 0
     */
    public void setX1(int x1) {
        this.x1 = x1;
    }

    /**
     * method that gets y1
     * @require y1 >= 0
     * 
     * @ensure y1 >= 0
     * @return y1
     */
    public int getY1() {
        return y1;
    }

    /**
     * method that sets y1 to an instance of y1
     * @require y1 >= 0
     * 
     * @ensure this.y1 >= 0
     */
    public void setY1(int y1) {
        this.y1 = y1;
    }

    /**
     * method that gets x2
     * @require x2 >= 0
     * 
     * @ensure x2 >= 0
     * @return x2
     */
    public int getX2() {
        return x2;
    }

    /**method that sets x2 to an instance of x2
     * @require x2 >= 0
     * 
     * @ensure this.x2 >= 0
     */
    public void setX2(int x2) {
        this.x2 = x2;
    }

    /**
     * method that gets y2
     * @require y2 >= 0
     * 
     * @ensure y2 >= 0
     * @return y2
     */
    public int getY2() {
        return y2;
    }

    /**
     * method that sets y2 to an instance of y2
     * @require y2 >= 0
     * 
     * @ensure this.y2 >= 0
     */
    public void setY2(int y2) {
        this.y2 = y2;
    }

    /**
     * method that gets color
     * @require color != null
     * 
     * @ensure color != null
     * @return color
     */
    public Color getColor() {
        return color;
    }

    /**
     * method that sets color to an instance of color 
     * @require color != null
     * 
     * @ensure this.color = color
     */
    public void setColor(Color color) {
        this.color = color;
    }

    /**
     * @require  x1 == 0 && y1 == 0 x2 == 0 && y2 ==0
     * @ensure  x1 == 0 && y1 == 0 x2 == 0 && y2 ==0
     */
    public Rectangle() {
    }

    /**
     * @require x2_in >= 0 && y2_in >=0
     * 
     * @ensure x1 == 0 && y1 == 0 x2 >= 0 && y2 >=0
     */
    public Rectangle(int x2_in, int y2_in) {
        x2 = x2_in;
        y2 = y2_in;
    }

    /**
     * @require x1_in >= 0 && y1_in >=0 x2_in >= 0 && y2_in >=0
     * 
     * @ensure x1 >= 0 && y1 >=0 x2 >= 0 && y2 >=0
     */
    public Rectangle(int x1_in, int y1_in, int x2_in, int y2_in) {
        x1 = x1_in;
        y1 = y1_in;
        x2 = x2_in;
        y2 = y2_in;
    }

    /**
     * @require x1_in >= 0 && y1_in >=0 x2_in >= 0 && y2_in >=0 && c_in != null
     * 
     * @ensure x1 >= 0 && y1 >=0 x2 >= 0 && y2 >=0 && color != null
     */
    public Rectangle(int x1_in, int y1_in, int x2_in, int y2_in, Color c_in) {
        x1 = x1_in;
        y1 = y1_in;
        x2 = x2_in;
        y2 = y2_in;
        color = c_in;
    }

    /**
     * @require x1 >= 0 && y1 >=0 x2 >= 0 && y2 >=0
     * 
     * @ensure x1 >= 0 && y1 >=0 x2 >= 0 && y2 >=0

     */
    public Rectangle(Rectangle r) {
        x1 = r.x1;
        y1 = r.y1;
        x2 = r.x2;
        y2 = r.y2;
    }

    /**
     * @require x1 >= 0 && y1 >=0 x2 >= 0 && y2 >=0 x2 >= x1 && y2 >= y1
     * @ensure width >= 0 height >= 0
     */
    public void draw(Graphics2D g2d) {
        int width = x2 - x1;
        int height = y2 - y1;
        if (color == null)
            g2d.drawRect(x1, y1, width, height);
        else {
            g2d.setColor(color);
            g2d.fillRect(x1, y1, width, height);
        }
    }
     public String toString() {
         int x= x1;
         int y = y1;
         int perimeter = x2;
         int area = y2;
         String Results = ("" + x + "\t       " + y + "\t          " + perimeter +  "\t        " + area);
         return Results;

     }

}