2016-12-02 41 views
0

我想基本上從JFrame中的按鈕單擊,顯示JSON文件的結果,但是我堅持初始化按鈕單擊在文本區域顯示結果。此時,這將正確打開JFrame,然後在控制檯中顯示結果。 (基本上,我將在JFrame表單中使用getResultsButtonActionPerformed方法)。謝謝!如何使用JSON/GSON通過鼠標單擊Jframe文件來顯示文本

import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import com.google.gson.Gson; 

class Children extends NewJFrame { 

public ArrayList<Child> children; 




@Override 
public String toString() { 

    StringBuilder B = new StringBuilder(); 

    B.append("Number of planets = " + children.size() + "\n"); 

    for (Child P : children) { 

     B.append(P.toString() + "\n"); 
    } 

    return B.toString(); 
} 
} 



class Child { 

public String id; 
public String name; 
public float percentage; 
    public float sampling_error; 
    public int word_count; 
    public String word_count_message; 



// Constructor 
public Child(String id, String name, float percentage, float sampling_error, int word_count, String word_count_message) { 

    this.id = id; 
    this.name = name; 
    this.percentage = percentage; 
      this.sampling_error = sampling_error; 
      this.word_count = word_count; 
      this.word_count_message = word_count_message; 

} 


@Override 
public String toString() { 

    return "ID" + id + " : Name = " + name + "; Percentage = " + percentage + "; Sampling_Error = " + sampling_error + "; Word Count = " + word_count + "; Word Count Message = " + word_count_message; 
} 
} 



public class Assignment { 

public static void main(String[] args) throws IOException { 

    // 1. New Gson processing instance 
    Gson gson = new Gson(); 

    // 2. Create class hierarchy from the planets.json file. To level class structure is the SolarSystem class. 
    Child S = gson.fromJson(new FileReader("lol.json"), Child.class); 

    // 3. Print class hierarchy to verify JSON loaded okay. 
    System.out.println(S); 

      NewJFrame form = new NewJFrame(); 
      form.setVisible(true); 
} 
} 

而且我的JFrame代碼,因爲我使用NetBeans

public class NewJFrame extends javax.swing.JFrame { 

/** 
* Creates new form NewJFrame 
*/ 
public NewJFrame() { 
    initComponents(); 
} 
private void getResultsButtonActionPerformed(java.awt.event.ActionEvent evt) {             

; 

}             

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 

/* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new NewJFrame().setVisible(true); 
     } 
    }); 
} 

// Variables declaration - do not modify      
private javax.swing.JButton getResultsButton; 
private javax.swing.JScrollPane result; 
private javax.swing.JTextArea resultsText; 
// End of variables declaration     
} 

回答

0

查找附上一個小例子。您應該用您的代碼替換

MyFrame.this.text.setText(... 

以獲取JSON字符串。

public class MyFrame extends JFrame { 

    JTextArea text = new JTextArea(); 

    public MyFrame() { 
     setSize(400, 400); 
     setLayout(new BorderLayout()); 

     add(text, BorderLayout.CENTER); 

     JButton but = new JButton("Go"); 
     add(but, BorderLayout.SOUTH); 

     but.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent ae) { 
       SwingWorker work = new SwingWorker() { 
        @Override 
        protected Object doInBackground() throws Exception { 
         MyFrame.this.text.setText("Hello @" + new Date()); 
         return null; 
        } 
       }; 
       work.execute(); 
      } 
     }); 
    } 

    public static void main(String[] args) { 
     MyFrame f = new MyFrame(); 
     f.setVisible(true); 
    } 
} 
+0

您好,感謝的評論,我有點糊塗發生的事情在NewJFrame.this.text.SetText(發生的事情在「文本」一部分,它的未來udnerlined) – R0ckTillWeDr0p

+0

我想繼續它簡單但完整。你真的只需要'text.setText('這裏給出的例子明確指出'text'是從[嵌套內部匿名類](https://docs.oracle.com/javase/)引用封閉類教程/ JAVA/javaOO/nested.html)。 – PeterMmm

相關問題