简体   繁体   English

JTextField问题

[英]JTextField problem

import org.jsoup.Jsoup;


@SuppressWarnings("unused")
public class SimpleWebCrawler extends 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. 我试图创建一个JTextField来接收来自用户的输入。 I have created an instance of JTextField and added into the JFrame. 我创建了JTextField的实例,并将其添加到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的代码:

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

This line shows the error of the IllegalArgumentException. 此行显示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. 我希望看到弹出一个JTextField来接收输入,它是一个随机URL,并且代码将处理该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. 这里的一个问题是您没有指定要正确添加InputField的位置。 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. 如果未指定值,则BorderLayout默认会添加到居中位置,因此看不到滚动区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM