在这个程序中,我试图初始化一个book对象数组,并将它们显示在JPanel中,该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);
}
};
}
这里是我的sort类,它初始化对象并设置它们的属性(这个类还将显示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与排序的信息。(我仍然需要输入sort类的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显示,也我不认为代码是正确的事件处理。(我还没有实现对特定单选按钮的排序。)
它根本没有渲染…
我们一步一步地编写代码。排序是不相关的,所以去掉所有的代码。
第一步是显示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