【问题标题】:cant create a JTextField无法创建 JTextField
【发布时间】:2011-04-03 07:15:15
【问题描述】:
import org.jsoup.Jsoup;


@SuppressWarnings({ "unused", "serial" })

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";
    String word2;


    public SimpleWebCrawler() throws MalformedURLException  {

        yourInputField.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            word2 = yourInputField.getText();
        }
        });


        _resultArea.setEditable(false);


        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,BorderLayout.SOUTH);


        this.setContentPane(content);
        this.setTitle("Crawled Links");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        JPanel content2 = new JPanel();
        this.setContentPane(content2);
        this.setTitle("Input the URL");
        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 来接受用户输入。输入将转到这行代码来处理代码。

URL my_url = new URL("http://" + word2 + "/");

         String url = "http://" + word2 + "/";

但是,代码运行时无需询问用户输入。 JTextField 没有出现,我直接得到一个错误,因为我输入了输入。

我试图让 JTextField 接受来自用户的输入。但是,它没有出现,并且代码直接进行处理,最终得到空的 my_url 和 rmpty url 变量。

如何根据我发布的代码创建 JTextField?我创建的 Jtextfield 似乎与我的代码发生冲突。

【问题讨论】:

  • 为了尽快获得更好的帮助,请发布SSCCE(不是每个人都使用自动插入导入的 IDE!)。

标签: java swing textfield


【解决方案1】:

Java swing 不遵循命令式方法,而是事件驱动的。你的构造方法是全部执行的,不会等待你的输入。

您不得将业务逻辑(即所有这些读/写内容)包含在此方法中,而是包含在一个单独的方法中,并从您在输入字段中注册的操作侦听器中调用它。 (参见例如http://download.oracle.com/javase/tutorial/uiswing/events/index.html

注意 A:如果您的逻辑非常繁重,您应该生成一个后台线程,而不是直接在您的动作侦听器中执行它(请参阅Swingworker)。

注意 B:你的班级中有很多奇怪的代码。

this.setContentPane(content);
this.setTitle("Crawled Links");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
JPanel content2 = new JPanel();
this.setContentPane(content2);
this.setTitle("Input the URL");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();

如前所述,这将立即运行,因此您的面板 content 根本不会显示,因为它将被 content2 覆盖。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    相关资源
    最近更新 更多