0

所以我在 JList 中排序了这个电影列表。这些元素的创建方式在另一个类中排序。这是列表的代码。电影列表 = 新电影列表();

    //Pre loaded film data
    filmlist.addFilm("The Matrix", "A", "18", "2", 100);
    filmlist.addFilm("Wreck It Ralph", "B", "U", "5", 150);
    filmlist.addFilm("Sin City", "C", "18", "3", 100);
    filmlist.addFilm("Starwars", "D", "PG", "3", 150);
    filmlist.addFilm("Back to the Future", "E", "PG", "2", 80);
    filmlist.addFilm("Willy Wonka & the Chocolate Factory", "F", "U", "1", 75);
    filmlist.addFilm("Avatar", "G", "PG", "2", 80);
    filmlist.addFilm("Disturba", "H", "PG", "1", 80);
    filmlist.addFilm("Monsters Inc", "I", "U", "1", 100);
    filmlist.addFilm("The Godfather", "J", "18", "1", 100 );

    theFilmList.setModel(filmlist);

我有一种方法可以模拟购买其中一部电影的票。Atm 它基本上打印出值。

    public void buyTicket() {
    String newFilmName = filmNameTF.getText();
    String newLecture = lectureTF.getText();
    String newAge = ageTF.getText();
    String newPrice = priceTF.getText();
    int newSeats = Integer.parseInt(seatsTF.getText()); // convert the int to a String
    int newSeatsNo; // the new number of seats for the film

    newSeatsNo = newSeats - 1;


    if (JOptionPane.showConfirmDialog(this, "Are you sure you want to buy a ticket for " + newFilmName + " at the cost of £" + newPrice,
            "Purchase Ticket", JOptionPane.YES_NO_OPTION) 
            == JOptionPane.YES_OPTION) {// Are you sure dialog      

        if (newFilmName.equals("") || newLecture.equals("") || 
                newAge.equals("") || newPrice.equals("")) {
            JOptionPane.showMessageDialog(this, "Please select a film from the list");
        } else {
            System.out.println("- Receipt -");
            System.out.println("Film: " + newFilmName);
            System.out.println("Lecture Hall: " + newLecture);
            System.out.println("Age Rating: " + newAge);
            System.out.println("Price of Ticket: " + "£" + newPrice);

        }

我想做的是更改列表中最后一个元素的值,即座位数。我可以通过“newSeatsNo = newSeats - 1;”来做方程式。如何在 JList 中替换该等式的值?

我希望你明白我的意思,只要说你是否需要任何类的更多代码。

public class FilmList extends DefaultListModel {
public FilmList(){
    super();
}

public void addFilm(String film, String  lecture, String age, String price, int seats){
    super.addElement(new FilmSystem(film, lecture, age, price, seats));
}

}

电影清单类。

4

1 回答 1

0

你为什么不使用 set 这是 DefaultListModel 下的方法之一

int lastElem = filmlist.getSize();

filmlist.set( lastElem, new FilmList().addFilm("The Godfather", "J", "18", "1", <new value here>);

我还没有尝试过这段代码,因为我无法访问编译器。但是你可以在这里找到 DefaultListModel 的可用方法:DefaultListModel

于 2013-03-28T01:05:23.690 回答