我之前没有在java中使用过很多comboBox,而且我在显示文本文件时遇到了一些麻烦。我相信我正确加载文件,但似乎我发现很难在代码中实现它。我在文本文件中有多个电影名称。当在组合框中选择不同的电影时,它会更改价格,评级等......
使用初始化数组后,我正确地完成了这项工作。
文本文件的示例[Taken,PG-13,8:00Am,7,50
import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.*;
import javax.swing.*;
public class MovieSelection extends JFrame {
private JPanel ratingPanel;
private JPanel panel;
private JLabel priceLabel;
private JLabel label;
private JButton addCart;
private JButton backButton;
private JButton resetButton;
private JTextField selectedRatingPanel;
private JTextField amountTextField;
private JComboBox movieBox;
private ArrayList<String> movieName;
private ArrayList<String> movieRating;
private ArrayList<String> movieTime;
private ArrayList<String> moviePrice;
public MovieSelection() {
super("Please select your movie");
setSize(575,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
buildMoviePanel();
buildRatingPanel();
add(panel);
setVisible(true);
movieName = new ArrayList<>();
movieRating = new ArrayList<>();
movieTime = new ArrayList<>();
moviePrice = new ArrayList<>();
}
private void readMovies() {
Scanner input=null;
try{
input = new Scanner(new File("TheMovies.txt"));
while(input.hasNext()){
String str = input.nextLine();
StringTokenizer strT = new StringTokenizer(str, ",");
movieName.add(strT.nextToken());
movieRating.add(strT.nextToken());
moviePrice.add(strT.nextToken());
movieTime.add(strT.nextToken());
}
}
catch(Exception element){
input.close();
JOptionPane.showMessageDialog(null, "Error");
}
}
private void buildMoviePanel() {
panel = new JPanel();
priceLabel = new JLabel("Cost:");
backButton = new JButton("Back");
resetButton = new JButton("Rest");
backButton.addActionListener(new BackButton());
resetButton.addActionListener(new ResetButton());
addCart = new JButton("Add to cart");
JTextField totalTextField = new JTextField(10);
JTextField priceTextField = new JTextField(5);
JTextField amountTextField = new JTextField(4);
priceTextField.setEditable(false);
priceTextField.setText(moviePrice);
totalTextField.setEditable(false);
JComboBox movieLists = new JComboBox(movieName);
movieLists.setSelectedIndex(0);
movieLists.addActionListener(new MovieLists());
panel.add(movieLists).setBounds(20,52,80,40);
panel.add(priceLabel).setBounds(375,0,80,40);
panel.add(priceTextField).setBounds(375,52,75,40);
panel.add(backButton).setBounds(20,310,80,40);
panel.add(addCart).setBounds(380,310,100,40);
panel.add(resetButton).setBounds(200, 310, 80, 40);
panel.add(amountTextField);
panel.setLayout(null);
}//buildPanel
private void buildRatingPanel(){
ratingPanel = new JPanel();
label = new JLabel("Rating:");
selectedRatingPanel = new JTextField(9);
selectedRatingPanel.setEditable(false);
selectedRatingPanel.setText("R");
panel.add(label).setBounds(245, 0, 100,40);
panel.add(selectedRatingPanel).setBounds(245,52,100,40);
}
private class MovieLists implements ActionListener {
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox) e.getSource();
String theMovie = (String) cb.getSelectedItem();
System.out.println(cb.getSelectedIndex());
}
}
private class BackButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
// return back to home page
if (e.getSource() == backButton)
new SelectUserWindow();
setVisible(false);
}
}
private class ResetButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
// return back to home page
if (e.getSource() == resetButton);
}
}
}
答案 0 :(得分:1)
JComboBox
不会将ArrayList
(或Collection
)作为可行参数(可能需要Object[]
或Vector
)movieName
,movieRating
,movieTime
,moviePrice
都未初始化,因为您在创建UI后初始化它们。JTextField#setText
不会将ArrayList
作为可行参数