-1

您好,我一直无法让我的组合框列出文本文件中的项目。当我这样做时,我想问一下当我改变我的组合框时我的 radioButton 是否会动态改变,或者这会不会太麻烦?

import java.awt.GridLayout;
import java.util.Arrays;
import java.util.Scanner;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.io.*;

public class MovieSelection extends JFrame {

private JRadioButton selection1;
private JRadioButton selection2;
private JRadioButton selection3;
private JPanel moviePanel;
private JLabel priceLabel;

private JComboBox movieBox;
private ButtonGroup bg;

private String[] movieName = { "RED", "Taken", "Star Trek", "Star Wars",
        "Avatar" };


public MovieSelection() {
    super("Please select your movie");
    setSize(800, 400);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setLayout(new GridLayout(15, 4));

    setLocationRelativeTo(null);
    // PrintWriter outputFile = new PrintWriter(TheMovies.txt);
    buildMoviePanel();

    add(moviePanel);

    setVisible(true);

}



private class MovieLists implements ActionListener {
    public void actionPerformed(ActionEvent e) {

        Scanner inputFile = new Scanner(TheMovies.txt);

        while (inputFile.hasNext()) {
            String nextMovieName = inputFile.nextLine();

        }

        JComboBox cb = (JComboBox) e.getSource();
        String Themovie = (String) cb.getSelectedItem();

    }

}

}

4

1 回答 1

1

您正在编写TheMovies.txt一个表达式,因此 java 编译器将查找一个名为的类,该类TheMovies包含一个名为 的公共静态成员txt

您真正的意思是字符串文字,因此您必须将其括在引号中:

"TheMovies.txt"

但是当您将 a 传递StringScanner构造函数时,该字符串本身将被扫描,而不是被解释为文件名。因此,您必须首先从该字符串构造一个File或对象。Path

Scanner inputFile = new Scanner(new File("TheMovies.txt"));

线

JComboBox movieLists = new JComboBox(TheMovies.txt);

由于被解释为表达式的相同原因是错误的。无论如何都不确定您在这里的意图是什么,但我希望您能从中找到自己的下一步。它应该类似于迭代文件内容并一次填充组合框一项。

于 2013-04-25T09:27:38.547 回答