我如何将一个String传递给JTextField()进行搜索,并从JPanel中的控制台输出show中传递

时间:2014-01-15 15:29:17

标签: java jpanel

  

我怎么能将一个String传递给JTextField()来搜索并从JPanel中的控制台输出show?如果有任何想法,请帮助....   这是我的代码和我;我坚持在JPanel IO中做秀...   我已经尝试了很多方法将JTextField()转换为字符串来执行此操作但不起作用..请帮助...

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import com.google.gson.Gson;

public class test2 {

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

        for (int i = 0; i < 20; i = i + 4) {
            String address = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start="+i+"&q=";

            String query = "Programcreek";
            String charset = "UTF-8";

            URL url = new URL(address + URLEncoder.encode(query, charset));
            Reader reader = new InputStreamReader(url.openStream(), charset);
            GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);

            // Show title and URL of each results
            for (int m = 0; m <= 3; m++) {
                System.out.println("Title: " + results.getResponseData().getResults().get(m).getTitle());
                System.out.println("URL: " + results.getResponseData().getResults().get(m).getUrl() + "\n");
            }
        }
    }
}


class GoogleResults{

    private ResponseData responseData;
    public ResponseData getResponseData() { return responseData; }
    public void setResponseData(ResponseData responseData) { this.responseData = responseData; }
    public String toString() { return "ResponseData[" + responseData + "]"; }

    static class ResponseData {
        private List<Result> results;
        public List<Result> getResults() { return results;}
        public void setResults(List<Result> results) { this.results = results; }
        public String toString() { return "Results[" + results + "]"; }
    }

    static class Result {
        private String url;
        private String title;
        public String getUrl() { return url; }
        public String getTitle() { return title; }
        public void setUrl(String url) { this.url = url; }
        public void setTitle(String title) { this.title = title; }
        public String toString() { return "Result[url:" + url +",title:" + title + "]"; }
    }
}
  

在我尝试在Panel中显示输入和输出的地方给出了我的修复代码....

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import com.google.gson.Gson;


import java.applet.Applet;
import java.net.*;
import java.awt.*;

public class test6 extends Applet{

    TextField searchParameter;
    Choice    searchEngine;
    Button    searchButton;

   public test6(){
        init(); 
    }
    public void init() {

        setBackground(Color.white);
        searchParameter = new TextField(20);
        add(searchParameter);
        searchEngine = new Choice();


        searchEngine.addItem("Google");
        searchEngine.select(0);
        add(searchEngine);
        searchButton = new Button("Search");
        add(searchButton);
        //setTitle("eBook reader");  
        setSize(600, 1000); 
        setVisible(true); 

        for (int i = 0; i < 20; i = i + 4) {
            String address = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start="+i+"&q=";

            String query = searchParameter.getText();
            String charset = "UTF-8";
         try{
            URL url = new URL(address + URLEncoder.encode(query, charset));
            Reader reader = new InputStreamReader(url.openStream(), charset);
            GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);
         }catch (IOException err) {
             System.out.println("Error reading line");
         }

            // Show title and URL of each results
            for (int m = 0; m <= 3; m++) {

                GoogleResults results = null;
                System.out.println("URL: " + results.getResponseData().getResults().get(m).getUrl() + "\n");
            }
        }
        }

    public boolean action(Event e, Object o) {
    if (e.target.equals(searchButton)) {
        try {
        sendSearch();
        }
        catch (Exception e1) {
        showStatus("Exception caught:" + e1.toString());
        }
    }
    return true;
    }

    public void sendSearch() throws Exception {
    String searchString = searchParameter.getText();
    if (searchString.equals("")) {
        showStatus("Must enter a search string");
        return;
    }
    String url;
    switch (searchEngine.getSelectedIndex()) {

    case 1: url = "://www.google.com/search?q=";
        break;      
    default: showStatus("Invalid search engine selected.");
        return;
    }

    // encode the search data
    url += URLEncoder.encode(searchString);

    // launch the search engine
    showStatus("Connecting to search location " + url);
    getAppletContext().showDocument(new URL(url), "_top");
    }      

    public static void main(String[] args) {
        new test6().setVisible(true);


    }


public class GoogleResults{

    private ResponseData responseData;
    public ResponseData getResponseData() { return responseData; }
    public void setResponseData(ResponseData responseData) { this.responseData = responseData; }
    public String toString() { return "ResponseData[" + responseData + "]"; }

     class ResponseData {
        private List<Result> results;
        public List<Result> getResults() { return results;}
        public void setResults(List<Result> results) { this.results = results; }
        public String toString() { return "Results[" + results + "]"; }
    }

       class Result {
        private String url;
        private String title;
        public String getUrl() { return url; }
        public String getTitle() { return title; }
        public void setUrl(String url) { this.url = url; }
        public void setTitle(String title) { this.title = title; }
        public String toString() { return "Result[url:" + url +",title:" + title + "]"; }
    }
}

}

0 个答案:

没有答案