2013-10-21 45 views
0

如果在JTextBox中輸入一個字符串,比按下「enter button」,它會返回JTextField中的正確數據,如果我改變了字符串,並且阻止輸入正確的數據也被返回。如果我按下「輸入按鈕」並按下「添加」按鈕(將數據添加到JList,並重新調用Gui方法以更新JList),如果我嘗試在JTextField中輸入另一個字符串,當我按下輸入按鈕時,什麼都不會發生。 我不明白這是爲什麼,如果有人看到我沒有看到的東西,如果你能告訴我,它會很酷。JButton在按下其他JButton後沒有被調用

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

你的GUI的基本設計是錯誤的。你不應該使用靜態方法和變量。我建議你看一下Swing教程,瞭解如何構建你的代碼。 – camickr

+0

那麼,如果我刪除靜態修飾符,我被告知我是通過靜態方法訪問非靜態字段。我真的不知道我的訪問方式是如何靜態的,老實說,查看swing教程並沒有幫助我太明白這一點。 –

+0

現在沒有時間學習正確的代碼編寫方式。這個想法是從教程中的一個工作示例開始,然後根據您的特定需求進行更改。也許從「如何使用標籤」一節的「LabelDemo」開始。它向你展示瞭如何在Event Dispatch Thread上正確構建GUI。是的,代碼確實使用了幾個靜態方法,但這只是爲了創建框架,而不是您的應用程序的核心,它將成爲JPanel。因此,修改示例以將組件添加到面板。 – camickr

回答

1

在你的ActionListener的「添加」按鈕,你又調用jsonAndButtons()和GUI()方法,這是重新創建按鈕。這可能會導致怪異的行爲。

+0

讓我看看刷新JList中項目的更好方法。 –