0

当我触发按钮的 actionlistener 时,JList 无法刷新。我唯一可以让它刷新的方法是调用 GUI 方法,这不是那么有效,因为它实际上只是打开了第二个窗口。有没有人有任何建议让添加和删除按钮自动更新 JList?

    package movieinfo;    
    import java.awt.Color;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;

    import javax.swing.BorderFactory;
    import javax.swing.DefaultListModel;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import org.apache.commons.io.FileUtils;
    public class Swinggui {
        public static void main(String[] args) throws IOException {
            new Swinggui();
        }

        public Swinggui() throws IOException {
            yourMovies();
            gui();
            Buttons();
        }

        JFrame maingui;
        JButton enter;
        JButton remove;
        public JTextField movietext;
        JList listofmovies;
        File textfilemovie;
        JScrollPane listscroll;
        ListSelectionListener setSearch;
        JButton add;
        JTextArea movieinfo;
        DefaultListModel lmodel;

        public void gui() throws IOException {
            maingui = new JFrame("Gui");
            maingui.setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            c.fill = GridBagConstraints.VERTICAL;
            lmodel = new DefaultListModel();
            enter = new JButton("Get Info");
            c.gridx = 2;
            c.gridy = 1;
            maingui.add(enter, c);
            add = new JButton("add");
            c.gridx = 5;
            c.gridy = 6;
            maingui.add(add, c);
            remove = new JButton("del");
            c.gridx = 6;
            c.gridy = 6;
            maingui.add(remove, c);
            movieinfo = new JTextArea(5, 20);
            movieinfo.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2,
                    Color.red));
            movietext = new JTextField(18);
            c.gridx = 1;
            c.gridy = 1;
            maingui.add(movietext, c);
            JScrollPane scrolll = new JScrollPane(movieinfo);
            c.gridx = 1;
            c.gridy = 3;
            c.gridwidth = 2;
            maingui.add(scrolll, c);
            final JLabel titlee = new JLabel("Enter movie name below!");
            c.gridx = 1;
            c.gridy = 0;
            maingui.add(titlee, c);
            c.gridx = 1;
            c.gridy = 3;
            maingui.add(titlee, c);
            final JLabel watchlist = new JLabel("Watchlist");
            c.gridx = 5;
            c.gridy = 1;
            maingui.add(watchlist, c);
            maingui.setResizable(false);
            maingui.setVisible(true);
            listofmovies = new JList(FileUtils.readLines(textfilemovie).toArray());
            listscroll = new JScrollPane(listofmovies);
            c.gridx = 4;
            c.gridy = 3;
            maingui.add(listscroll, c);
            movieinfo.setLineWrap(true);
            movieinfo.setWrapStyleWord(true);
            movieinfo.setEditable(false);
            scrolll.getPreferredSize();
            listofmovies.addListSelectionListener(setSearch);
            maingui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            maingui.pack();
        }

        public void Buttons() {
            enter.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {

                    System.out.println(apicall.getMovieInfo(movietext.getText()
                            .replaceAll(" ", "%20")));
                    movieinfo.setText(apicall.getMovieInfo(movietext.getText()
                            .replaceAll(" ", "%20")));
                }

            });
            add.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    try {
                        FileUtils.writeStringToFile(textfilemovie,
                                "\n" + movietext.getText(), true);
                        maingui.validate();
                        maingui.repaint();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            });
            remove.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    Scanner filescan = null;
                    File textfiletemp = new File(org.apache.commons.io.FileUtils
                            .getUserDirectory() + "/yourmoviestemp.txt");
                    try {
                        textfiletemp.createNewFile();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    try {
                        filescan = new Scanner(textfilemovie);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    while (filescan.hasNextLine()) {
                        String line = filescan.nextLine();
                        if (line != (String) listofmovies.getSelectedValue()) {
                            try {
                                FileUtils.writeStringToFile(textfiletemp, "\n"
                                        + line, true);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    textfiletemp.renameTo(textfilemovie);
                    textfiletemp.delete();
                }

            });
        }

        public void yourMovies() throws IOException {
            textfilemovie = new File(
                    org.apache.commons.io.FileUtils.getUserDirectory()
                            + "/yourmovies.txt");
            textfilemovie.createNewFile();
            setSearch = new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent arg0) {
                    movieinfo.setText(apicall.getMovieInfo(((String) listofmovies
                            .getSelectedValue()).replaceAll(" ", "%20")));
                }
            };
        }
    }
4

2 回答 2

2

作为第一个提示:line != (String) listofmovies.getSelectedValue()不是比较字符串的适当方法。始终使用String.equals()代替:

!line.equals((String)listofmovies.getSelectedValue())

我唯一可以让它刷新的方法是调用 GUI 方法,这不是那么有效,因为它实际上只是打开了第二个窗口。

对不起,但我不知道那是什么意思。

有没有人有任何建议让添加和删除按钮JList自动更新?

只需使用列表模型从您JListactionPerformed代码中添加/删除数据。您已经有一个引用,lmodel它是DefaultListModel. 看看DefaultListModel.addElement()DefaultListModel.removeElement()

这一切都在如何使用列表教程中进行了解释。

于 2013-10-23T14:30:12.353 回答
0

当我尝试从 FileUtils.readLines(textfilemovie).toArray() 添加元素时,我得到一个空指针。

您尝试读取的文件的名称是什么?

为什么您的代码结构仍然不遵循 Swing 教程中的示例???你需要多少时间才能被要求这样做???您的代码未在 EDT 上执行,这可能会导致随机错误!!!

当您提出问题时,发布适当的 SSCCE。我们无权访问您的 FileUtils 类。提出问题意味着您已经完成了一些基本调试并隔离了问题,因此您创建了一个演示问题的 SSCCE。我们不是她来调试你的真实代码。

我厌倦了那些不断提出问题,却不听从他们得到的任何建议的人。我们给出这个建议是有原因的。这是为了帮助您编写更好的程序,这样您就不需要因为糟糕的程序设计而不断地寻求我们的帮助。

于 2013-10-23T15:32:10.540 回答