gpt4 book ai didi

java - 在组合框中获取文本文件

转载 作者:行者123 更新时间:2023-12-01 14:38:14 24 4
gpt4 key购买 nike

我以前在java中没有太多使用comboBox,并且在显示我的文本文件时遇到了一些麻烦。我相信我已正确加载文件,但似乎我发现很难在代码中实现它。我的文本文件中有多个电影名称。当在组合框中选择不同的电影时,它会更改价格、评级等...

我曾经使用初始化数组正确地完成了此操作。

文本文件示例[拍摄,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);


}
}
}

最佳答案

  1. 学习使用布局管理器
  2. JComboBox 不采用 ArrayList(或 Collection)作为可行参数(它可以采用 Object[] vector )
  3. movieNamemovieRatingmovieTimemoviePrice 在您创建 UI 时都未初始化,因为您初始化了它们创建 UI 之后。
  4. JTextField#setText 不采用 ArrayList 作为可行参数
  5. 学习从控制台或 IDE 读取应用程序的输出
  6. 学习使用调试器 - 它会为您节省很多时间的挫败感和烦恼;)

关于java - 在组合框中获取文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16269871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com