开发者

JTextField problem

开发者 https://www.devze.com 2023-02-21 20:09 出处:网络
import org.jsoup.Jsoup; @SuppressWarnings(\"unused\") public class SimpleWebCrawler exte开发者_JS百科nds JFrame {
import org.jsoup.Jsoup;


@SuppressWarnings("unused")
public class SimpleWebCrawler exte开发者_JS百科nds JFrame {

    JTextField yourInputField = new JTextField(20);
    static JTextArea _resultArea = new JTextArea(200, 200);
    JScrollPane scrollingArea = new JScrollPane(_resultArea);
    private final static String newline = "\n";



    public SimpleWebCrawler() throws MalformedURLException {


        _resultArea.setEditable(false);

        String word2 = yourInputField.getText();

        try {
            URL my_url = new URL("http://" + word2 + "/");
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    my_url.openStream()));
            String strTemp = "";
            while (null != (strTemp = br.readLine())) {
                _resultArea.append(strTemp + newline);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        _resultArea.append("\n");
        _resultArea.append("\n");
        _resultArea.append("\n");


        String url = "http://" + word2 + "/";
        print("Fetching %s...", url);

        try{
        Document doc = Jsoup.connect(url).get();
        Elements links = doc.select("a[href]");


        System.out.println("\n");

        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\user\\fypworkspace\\FYP\\Link\\abc.txt"));
        _resultArea.append("\n");
        for (Element link : links) {
            print("  %s  ", link.attr("abs:href"), trim(link.text(), 35));

            bw.write(link.attr("abs:href"));
            bw.write(System.getProperty("line.separator"));
        }
        bw.flush();
        bw.close();
        } catch (IOException e1) {

        }
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        content.add(scrollingArea, BorderLayout.CENTER);
        content.add(yourInputField);

        this.setContentPane(content);
        this.setTitle("Crawled Links");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.pack();


        }

        private static void print(String msg, Object... args) {

            _resultArea.append(String.format(msg, args) +newline);
        }

        private static String trim(String s, int width) {
            if (s.length() > width)
                return s.substring(0, width - 1) + ".";
            else
                return s;
        }

        //.. Get the content pane, set layout, add to center




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



        JFrame win = new SimpleWebCrawler();
        win.setVisible(true);

    }
}

I am trying to create a JTextField to receive input from the user. I have created an instance of JTextField and added into the JFrame. However, this code is not working. Mind point out my mistakes ? It suppose to work, however I could not find out the problem is. Do I miss something else ?

The code for the JTextField :

JTextField yourInputField = new JTextField(20);
String word2 = yourInputField.getText();
content.add(yourInputField);

This line shows the error of the IllegalArgumentException.

my_url.openStream()

I expected to see a JTextField pop up to receive inputs which is a random URL and the code will process the URL. Sorry for my bad display of question. I am not very familiar with programming question answering forum.


Where do you read input from the text field? I see it happening once in the constructor, but since you're reading the text from a new, empty text field, you won't be getting any input from the user with that call.

If you want the user to be able to input data into the text field and have the input processed afterwards, you're going to need to use event-driven programming with action listeners and the like. This will allow parts of the program to run when the user performs certain actions, like text input.


One problem here is that you are not specifying where you want to add yourInputField correctly. Use for example

  content.add(yourInputField, BorderLayout.SOUTH);

instead

  content.add(yourInputField);

If you are not specifying value the BorderLayout adds by default to center thus you do not see the scrolling area.

0

精彩评论

暂无评论...
验证码 换一张
取 消