不能创建一个JTextField

问题描述:

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接受用户输入。输入将转到这行代码来处理代码。不能创建一个JTextField

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

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

然而,代码,而不要求用户进行输入运行。 JTextField没有出现,我直接得到一个错误,因为我输入了输入。

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

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

+2

为了更快得到更好的帮助,请发布[SSCCE](http://pscode.org/sscce.html)(并非所有人都使用自动插入导入的IDE!)。 – 2011-04-03 07:23:09

Java swing不遵循命令式的方法,而是事件驱动。您的构造函数方法总共执行并且不等待您的输入。

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

注意:如果您的逻辑非常重,则应该产生后台线程,而不是直接在动作侦听器中执行此操作(请参阅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被覆盖。