-1

目前我的代码不会运行,因为我没有 main,但是当我创建 main 时,它必须是静态的,而且我的印象是我不应该将 Swing 元素的所有变量都设为静态,根据建议许多。我不确定如何在不使用 main 作为构造函数的情况下调用方法,目前我的 gui 没有出现。

谢谢。

 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.File;
import java.io.IOException;

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;

public class Swinggui {
    JButton enter;
    public JTextField movietext;

    JList listofmovies;// converts moviestowatch into gui
    // element.
    File textfilemovie; // file which movies marked for watching
    // are saved
    java.util.List<String> moviestowatch; // arraylist which is
    // populated by
    // textfilemovie
    // than printed to
    // GUI element
    ListSelectionListener setSearch;
    JButton add;
    String info;

    public Swinggui() throws IOException {
        yourMovies();
        gui();
        jsonAndButtons();

    }

    public 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);
        JTextArea movieinfo = new JTextArea(info, 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);
        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 void jsonAndButtons() {
        enter.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

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

        });
        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);
                    listofmovies = new JList(moviestowatch.toArray());

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        });

    }

    public 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) {
                info = apicall.getMovieInfo(((String) listofmovies
                        .getSelectedValue()).replaceAll(" ", "%20"));
            }
        };
    }
}
4

3 回答 3

3

首先,不要把所有东西都放在一个类中。创建一些其他类,然后创建该类型的对象,并调用它的方法,它在你的方法中看起来像这样main()

 MyClass myClass = new MyClass();
 myClass.doStuff();
于 2013-10-22T14:06:04.300 回答
2

在你的主要放里面:

new Swinggui();

这会将您从静态上下文中拉出来并带您进入非静态 Swinggui 构造函数

于 2013-10-22T14:03:41.107 回答
0

让你的类 Swinggui 扩展 JFrame。然后创建一个main方法并创建Swinggui的对象

Swinggui gui = new Swinggui();

现在你必须让 gui 可见,写这篇文章。

gui.setVisible(true);

你很高兴。

使用“this”引用代码中的所有内容,您将拥有非静态项目。

于 2013-10-22T14:12:01.670 回答