2013-10-22 29 views
-1

目前我的代碼將無法運行,因爲我沒有主力,但是當我做一個主要的必須是靜態的,而我的印象是我不應該讓我所有的變量對於Swing元素靜態,根據許多建議。 我不知道如何調用方法,而不使用main作爲構造函數,目前我的gui沒有出現。invoke方法是不是靜態與主

謝謝。

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")); 
      } 
     }; 
    } 
} 
+1

「*沒有使用main作爲構造函數*」 - 你打算如何使用'main'作爲構造函數? – Maroun

+0

@MarounMaroun我認爲他的意思是創建內部主要的swing元素 – Cruncher

+0

更好地將Swing GUI編碼放在一邊,並通過一個基本的Java教程和/或教科書。這在過去已被推薦給您,如果您能夠了解Java基礎知識,它只會改善您的GUI編碼。由你決定。 –

回答

3

首先,不要把一切都在一個班級。創建一些其他的類,然後創建該類型的對象,並調用它的方法,它看起來像這樣在你的main()方法:

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

你的主把裏面:

new Swinggui(); 

這會拉你出來的靜態文本,並帶您進入非靜態Swinggui構造

0

讓你的類Swinggui延伸的JFrame。 然後創建一個主要方法,並創建對象Swinggui

Swinggui gui = new Swinggui(); 

現在你必須GUI中可見,該寫。

gui.setVisible(true); 

而且你很好走。

使用「this」引用代碼中的所有內容,並且您將擁有非靜態項目。

+1

爲什麼建議該類「必須擴展JFrame」。這顯然是不真實的,事實上大多數Swing專家並不推薦這樣做。 –

+0

@HovercraftFullOfEels我喜歡擴展JFrame,這只是我。 「你的班級Swinggui必須擴展JFrame'不僅僅是誤導。 – Cruncher

+0

@HovercraftFullOfEels - 我剛纔讀了官方oracle的文檔,每個例子中他們擴展JPanel並在主要中創建JFrame的對象,並使用「frame.add(panel)」。這是推薦的方法嗎?如果不是,你能指向我的鏈接或任何來源。 在此先感謝 –

相關問題