이 프로그램에서는 책 개체 배열을 초기화하고 사용자가 어떤 라디오 단추를 선택했는지에 따라 정렬되도록 JPanel에 표시하려고합니다.도서 정렬 프로그램
일부 백엔드 정렬 생성자가 완료되지 않았지만 먼저 전체 기능을 수행하려고합니다. 여기
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를 업데이트하는 라디오 버튼에 대한 사용자 정의 코딩이있는 마지막 클래스입니다. 정렬 클래스의 (난 아직도 쇼에 입력해야하는() 메소드 :
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를 표시하는 데 문제가 있습니다"라고 말하면 일부 요소가 부적절하게 표시되거나 GUI가 전혀 렌더링되지 않는다는 것을 의미합니까? – MarsAtomic
전혀 렌더링되지 않습니다 ... 내 문제가 어디에 있는지 알 수 없습니다 ... – midrigs