【问题标题】:Book sort program [closed]图书分类程序[关闭]
【发布时间】:2014-09-16 16:05:41
【问题描述】:

在这个程序中,我试图初始化一个书籍对象数组并将它们显示在一个 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,而且我认为代码在事件处理方面不正确。 (我还没有实现对特定单选按钮的排序。)

任何帮助/见解将不胜感激。

【问题讨论】:

  • “我遇到了麻烦..” ..形成一个问题?您有(明确、具体的)问题吗?
  • 您应该一次将问题限制为一个问题。现在,说“我无法让 GUI 显示”是指某些元素显示不正确,还是您的 GUI 根本没有呈现?
  • 根本不渲染...不知道问题出在哪里...

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


【解决方案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

【讨论】:

    猜你喜欢
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 2019-11-08
    相关资源
    最近更新 更多