2013-04-29 35 views
0

我之前没有在java中使用过很多组合框,并且我的文本文件出现时遇到了一些问题。我相信我有文件加载正确,但它似乎很难在代码中实现它。我在文本文件中有多个电影名称。并在组合框中选择不同的电影时,它会改变价格,评级等...在组合框中获取文本文件

一旦使用初始化数组,我做到了这一点。文本文件[两者,PG-13,8的

例如:上午12点,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

首先,花时间学习如何使用布局管理器 – MadProgrammer 2013-04-29 01:18:15

+0

Ps - 您的代码不能编译... – MadProgrammer 2013-04-29 01:21:13

回答

1
  1. 学习使用布局管理器
  2. JComboBox不采取ArrayList(或Collection)作为一个可行的参数(可能需要Object[]Vector
  3. movieNamemovieRatingmovieTimemoviePrice都是未初始化的时候你创建用户界面,因为您在创建用户界面后初始化用户界面。
  4. JTextField#setText不采取ArrayList作为一种可行的参数
  5. 学会阅读从控制台或IDE
  6. 您的应用程序的输出学会使用调试器 - 它会为你节省挫折和烦恼的几个小时;)