2014-07-24 55 views
-2

在这个程序中,我试图初始化一个书籍对象数组,并将它们显示在一个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

您应该一次限制您的问题为单个问题。现在,通过说“我无法让GUI显示”,你的意思是某些元素显示不正确,或者说你的GUI根本不显示? – MarsAtomic

+0

它根本不是渲染......我不知道我的问题在哪里...... – midrigs

回答

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 
相关问题