所以我将这个电影列表排序在JList中。如何创建这些元素在另一个类中排序。这是列表的代码。 filmlist = new FilmList();
//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);
我有一种模拟购买其中一部电影票的方法。它基本上打印出值。
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));
}
}
电影列表课程。
答案 0 :(得分:0)
为什么不使用set,这是DefaultListModel
下的方法之一int lastElem = filmlist.getSize();
filmlist.set( lastElem, new FilmList().addFilm("The Godfather", "J", "18", "1", <new value here>);
我还没有尝试过这段代码,因为我无法访问编译器。但是你可以在这里找到DefaultListModel的可用方法:DefaultListModel