按下其他JButton后没有调用JButton

时间:2013-10-21 15:55:39

标签: java swing jbutton

如果在JTextBox中输入一个字符串,那么按下“输入按钮”,它会在JTextField中返回正确的数据,如果我更改了字符串,并且重新输入也会返回正确的数据。如果我按下“输入按钮”并按下“添加”按钮(将数据添加到JList,并重新调用Gui方法以更新JList),如果我尝试在JTextField中输入另一个字符串当我按下回车键时没有任何反应。 我不明白为什么会这样,如果有人看到我不知道的东西,如果你能告诉我的话会很酷。

package movieinfo;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Map;
import javax.swing.BorderFactory;
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;

import com.json.parsers.JSONParser;
import com.json.parsers.JsonParserFactory;

public class Swinggui {
    private static JButton enter;
    private static JTextField movietext;
    private static JTextArea movieinfo;
    private static JList listofmovies;// converts moviestowatch into gui
                                        // element.
    private static File textfilemovie; // file which movies marked for watching
                                        // are saved
    private static java.util.List<String> moviestowatch; // arraylist which is
                                                            // populated by
                                                            // textfilemovie
                                                            // than printed to
                                                            // GUI element
    private static JsonParserFactory factory;
    private static JSONParser parser;
    @SuppressWarnings("rawtypes")
    private static Map jsonData;
    private static ListSelectionListener setSearch;
    private static JButton add;

    public static void main(String[] args) throws IOException {
        yourMovies();
        gui();
        jsonAndButtons();

    }

    public static void gui() {
        JFrame maingui = new JFrame("Gui");
        maingui.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.VERTICAL;
        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);
        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);
        final 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);
        final JLabel info = new JLabel("Info");
        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(moviestowatch.toArray());
        c.gridx = 4;
        c.gridy = 3;
        maingui.add(new JScrollPane(listofmovies), c);
        movieinfo.setLineWrap(true);
        movieinfo.setWrapStyleWord(true);
        movieinfo.setEditable(false);
        scrolll.getPreferredSize();
        listofmovies.addListSelectionListener(setSearch);
        maingui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        maingui.pack();

    }

    public static void jsonAndButtons() {
        enter.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                System.out.println(apicall.getMovieInfo(movietext.getText().replaceAll(" ", "%20")));
                factory = JsonParserFactory.getInstance();
                parser = factory.newJsonParser();
                jsonData = parser.parseJson(apicall.getMovieInfo(movietext
                        .getText().replaceAll(" ", "%20")));
                String Title = (String) jsonData.get("Title");
                String Year = (String) jsonData.get("Year");
                String Plot = (String) jsonData.get("Plot");
                movieinfo.setText("Title: " + Title + "\nYear: " + Year
                        + "\nPlot: " + Plot);

            }

        });
        add.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                try {
                    FileUtils.writeStringToFile( new File(
                            org.apache.commons.io.FileUtils.getUserDirectory()
                            + "/yourmovies.txt"), "\n" + movietext.getText(), true);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }  
                try {
                    moviestowatch = FileUtils.readLines(textfilemovie);
                    jsonAndButtons();
                    gui();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        }); 

    }

    public static void yourMovies() throws IOException {
        textfilemovie = new File(
                org.apache.commons.io.FileUtils.getUserDirectory()
                        + "/yourmovies.txt");
        textfilemovie.createNewFile();
        moviestowatch = FileUtils.readLines(textfilemovie);
        setSearch = new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent arg0) {
                factory = JsonParserFactory.getInstance();
                parser = factory.newJsonParser();
                jsonData = parser.parseJson(apicall.getMovieInfo(((String) listofmovies
                        .getSelectedValue()).replaceAll(" ", "%20")));
                String Title = (String) jsonData.get("Title");
                String Year = (String) jsonData.get("Year");
                String Plot = (String) jsonData.get("Plot");
                movieinfo.setText("Title: " + Title + "\nYear: " + Year
                        + "\nPlot: " + Plot);
            }
        };
    }
}

1 个答案:

答案 0 :(得分:1)

在用于“添加”按钮的actionListener中,您再次调用jsonAndButtons()和gui()方法,即重新创建按钮。这可能会导致奇怪的行为。