书籍排序程序

时间:2014-07-24 22:15:08

标签: java swing sorting event-handling radio-button

在这个程序中,我试图初始化一个图书对象数组,并将它们显示在JPanel中,以便根据用户选择的单选按钮对它们进行排序。

我还没有完成一些后端排序构造函数,但我试图首先完成全部功能。这是我的对象类,包含所有getter / setter:

import java.util.Comparator;


public class SchoolTextBook {

private String author;
private String title;
private int pageCount;
private String ISBN;
private double price;

public String getAuthor() {
    return author;
    }
public void setAuthor(String author) {
    this.author = author;
    }

public String getTitle() {
    return title;
    }
public void setTitle(String title) {
    this.title = title;
    }

public int getPageCount() {
    return pageCount;
}
public void setPageCount(int pageCount) {
    this.pageCount = pageCount;
}

public String getISBN() {
    return ISBN;
}
public void setISBN(String iSBN) {
    ISBN = iSBN;
}

public double getPrice() {
    return price;
}
public void setPrice(double price) {
    this.price = price;
}

public static Comparator<SchoolTextBook> BookAuthorComparator 
                    = new Comparator<SchoolTextBook>() {

    public int compare(SchoolTextBook book1, SchoolTextBook book2) {

        String bookName1 = book1.getAuthor().toUpperCase();
        String bookName2 = book2.getAuthor().toUpperCase();

        //ascending order
        return bookName1.compareTo(bookName2);

    }

};

public static Comparator<SchoolTextBook> BookTitleComparator 
                    = new Comparator<SchoolTextBook>() {

    public int compare(SchoolTextBook book1, SchoolTextBook book2) {

        String bookName1 = book1.getTitle().toUpperCase();
        String bookName2 = book2.getTitle().toUpperCase();

        //ascending order
        return bookName1.compareTo(bookName2);

    }

};
}

这是我的排序类初始化对象并设置它们的属性(这个类也会显示gui ......我想):

import java.util.Arrays;

import javax.swing.*;

public class SchoolTextBookSort {

public static String show() {
    StringBuilder sb = new StringBuilder(64);
    sb.append("<html><table><tr><td>Item</td><td>Price</td><td>Quantity</td><td></td>Priority</tr>");
    sb.append("<tr>");
    sb.append("<td>").append("hi").append("</td>");
    sb.append("<td>").append("hi").append("</td>");
    sb.append("<td>").append("hi").append("</td>");
    sb.append("<td>").append("hi").append("</td>");
    sb.append("</tr></table></html>");
    JOptionPane.showMessageDialog(null, sb);
    return sb.toString();
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
           createAndShowGUI();
        }
    });

    SchoolTextBook[] theBooks = new SchoolTextBook[5];

    theBooks[0] = new SchoolTextBook();
    theBooks[1] = new SchoolTextBook();
    theBooks[2] = new SchoolTextBook();
    theBooks[3] = new SchoolTextBook();
    theBooks[4] = new SchoolTextBook();

    theBooks[0].setAuthor("Ernest Hemingway");
    theBooks[1].setAuthor("Mark Twain");
    theBooks[2].setAuthor("William Shakespeare");
    theBooks[3].setAuthor("Stephen King");
    theBooks[4].setAuthor("William Faulkner");

    theBooks[0].setTitle("A Farewell to Arms");
    theBooks[1].setTitle("The Adventures of Huckleberry Finn");
    theBooks[2].setTitle("Hamlet");
    theBooks[3].setTitle("Salem's Lot");
    theBooks[4].setTitle("The Sound and the Fury");

    theBooks[0].setPageCount(332);
    theBooks[1].setPageCount(320);
    theBooks[2].setPageCount(196);
    theBooks[3].setPageCount(439);
    theBooks[4].setPageCount(326);

    theBooks[0].setISBN("0099910101");
    theBooks[1].setISBN("0142437174");
    theBooks[2].setISBN("0521618746");
    theBooks[3].setISBN("0450031063");
    theBooks[4].setISBN("0679732241");

    theBooks[0].setPrice(5.99);
    theBooks[1].setPrice(7.60);
    theBooks[2].setPrice(9.41);
    theBooks[3].setPrice(16.56);
    theBooks[4].setPrice(9.60);     

    JOptionPane.showMessageDialog(null,   theBooks[0].getAuthor() + " - " + theBooks[0].getTitle() + " - " 
                                        + theBooks[0].getISBN() + " - " + theBooks[0].getPageCount() + " - "
                                        + theBooks[0].getPrice() + "\n" 
                                        + theBooks[1].getAuthor() + " - " + theBooks[1].getTitle() + " - " 
                                        + theBooks[1].getISBN() + " - " + theBooks[1].getPageCount() + " - "
                                        + theBooks[1].getPrice() + "\n"
                                        + theBooks[2].getAuthor() + " - " + theBooks[2].getTitle() + " - " 
                                        + theBooks[2].getISBN() + " - " + theBooks[2].getPageCount() + " - "
                                        + theBooks[2].getPrice() + "\n"
                                        + theBooks[3].getAuthor() + " - " + theBooks[3].getTitle() + " - " 
                                        + theBooks[3].getISBN() + " - " + theBooks[3].getPageCount() + " - "
                                        + theBooks[3].getPrice() + "\n"
                                        + theBooks[4].getAuthor() + " - " + theBooks[4].getTitle() + " - " 
                                        + theBooks[4].getISBN() + " - " + theBooks[4].getPageCount() + " - "
                                        + theBooks[4].getPrice());



    Arrays.sort(theBooks, SchoolTextBook.BookAuthorComparator);


    for(int i = 0; i < theBooks.length; i++) {

    }


}

private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("Book Sorting");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    JComponent newContentPane = new RadioButtonDisplay();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
    }
}

我的最后一个课程有自定义编码的单选按钮,它将使用已排序的信息更新GUI。 (我仍然需要进入排序类的show()方法:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.*;

import javax.swing.*;

public class RadioButtonDisplay extends JPanel
                            implements ActionListener {
String authorString = "Author";
String titleString = "Title";
String priceString = "Price";
String pageCountString = "Page Count";

JLabel sortedArray;

public RadioButtonDisplay() {

    JRadioButton authorButton = new JRadioButton(authorString);
    authorButton.setMnemonic(KeyEvent.VK_C);
    authorButton.setActionCommand(authorString);

    JRadioButton titleButton = new JRadioButton(titleString);
    titleButton.setMnemonic(KeyEvent.VK_D);
    titleButton.setActionCommand(titleString);

    JRadioButton priceButton = new JRadioButton(priceString);
    priceButton.setMnemonic(KeyEvent.VK_D);
    priceButton.setActionCommand(priceString);

    JRadioButton pageCountButton = new JRadioButton(pageCountString);
    pageCountButton.setMnemonic(KeyEvent.VK_D);
    pageCountButton.setActionCommand(pageCountString);

    ButtonGroup group = new ButtonGroup();
    group.add(authorButton);
    group.add(titleButton);
    group.add(priceButton);
    group.add(pageCountButton);

    authorButton.addActionListener(this);
    titleButton.addActionListener(this);
    priceButton.addActionListener(this);
    pageCountButton.addActionListener(this);

    JPanel radioPanel = new JPanel(new GridLayout(0, 1));
    radioPanel.add(authorButton);
    radioPanel.add(titleButton);
    radioPanel.add(priceButton);
    radioPanel.add(pageCountButton);

    add(radioPanel, BorderLayout.LINE_START);
    add(sortedArray, BorderLayout.CENTER);
    setBorder(BorderFactory.createEmptyBorder(20,20,20,20));

}

public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    sortedArray.setText(SchoolTextBookSort.show());e.getActionCommand();

}
}

我无法显示GUI,而且我也不认为代码在事件处理方面是正确的。 (我还没有对特定的单选按钮实现排序。)

非常感谢任何帮助/见解。

1 个答案:

答案 0 :(得分:1)

  

根本没有渲染......

一次一步地编写代码。排序是无关紧要的,所以摆脱所有的代码。

第一步是显示GUI组件。然后下一步是进行排序。保持代码简单,一次一步。以这种方式调试更容易。

不确定是否是问题,但默认情况下,JPanel使用FlowLayout。您的代码假定为BorderLayout:

add(radioPanel, BorderLayout.LINE_START);
add(sortedArray, BorderLayout.CENTER);

不确定它是否会产生影响(因为FlowLayout可能只是忽略了约束)但你应该使用:

setLayout( new BorderLayout() );
add(radioPanel, BorderLayout.LINE_START);
add(sortedArray, BorderLayout.CENTER);

此外,JPanel默认是不透明的,因此不需要以下代码:

//newContentPane.setOpaque(true); //content panes must be opaque