JTextField 问题 [英] JTextField problem

查看:28
本文介绍了JTextField 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);

    }
}

我正在尝试创建一个 JTextField 来接收来自用户的输入.我创建了一个 JTextField 实例并添加到 JFrame 中.但是,此代码不起作用.介意指出我的错误吗?它应该可以工作,但是我找不到问题所在.我还想念别的东西吗?

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 ?

JTextField 的代码:

The code for the JTextField :

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

这一行显示了 IllegalArgumentException 的错误.

This line shows the error of the IllegalArgumentException.

my_url.openStream()

我希望看到一个 JTextField 弹出来接收输入,它是一个随机 URL,代码将处理该 URL.对不起,我的问题显示不当.我对编程问答论坛不是很熟悉.

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.

推荐答案

这里的一个问题是您没有指定要正确添加 InputField 的位置.使用例如

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

  content.add(yourInputField, BorderLayout.SOUTH);

代替

  content.add(yourInputField);

如果您没有指定值,BorderLayout 默认添加到中心,因此您看不到滚动区域.

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

这篇关于JTextField 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆