调用main不是静态的方法

时间:2013-10-22 14:02:10

标签: java swing static

目前我的代码不会运行,因为我没有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"));
            }
        };
    }
}

3 个答案:

答案 0 :(得分:3)

首先,不要把所有东西都放在一个班级里。创建一些其他类,然后创建该类型的对象,并调用其方法,它在main()方法中将如下所示:

 MyClass myClass = new MyClass();
 myClass.doStuff();

答案 1 :(得分:2)

在主要内部:

new Swinggui();

这会让你退出静态上下文并带你进入非静态Swinggui构造函数

答案 2 :(得分:0)

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

Swinggui gui = new Swinggui();

现在你必须让gui可见,为此写作。

gui.setVisible(true);

你很高兴。

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