2011-03-31 7 views
0
System.out.println("Please enter the website :"); 
    Scanner scan2 = new Scanner(System.in); 
    String word2 = scan2.nextLine(); 

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

    System.out.println("\n"); 
    System.out.println("\n"); 
    System.out.println("\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) { 

안녕하세요. 웹 주소에서 링크를 추출하는 코드입니다. 사용자가 원하는 URL을 입력하면이 코드는 URL에서 링크를 추출합니다.사용자로부터 입력을 받기 위해 JTextField 만들기

이 코드는 사용자가 ECLIPSE IDE 콘솔에서 URL을 입력하도록 요청합니다. 입력을 키 입력하면 코드는 URL에서 링크를 추출하여 출력을 JTextArea로 전송합니다.

내가 지금하고 싶은 것은 콘솔의 입력에 사용자 키가 아닌 사용자 입력을받는 Jtextfield를 생성하고 싶습니다.

문자열 입력을 처리하기위한 책임이 코드 줄은 다음과 같습니다

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

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

나는 GUI의 개발에없는 많은 경험을 가지고, 나는 누군가가 나를 인도 할 수 있기를 바랍니다. 감사.

+2

:

... MyFrame myFrame = new MyFrame(); myFrame.pack(); myFrame.setVisible(true); ... 

이 시점 후, 당신은 같은 당신의 프레임에 대한 참조를 사용하여 텍스트 상자에 값을 얻을 수 있습니다 – MByD

+2

이중 슬래시를 사용하면 이식성이 없어집니다. "C : \\ Users \\ User \\ fypworkspace \\ FYP \\ 링크 \\ abc .txt " –

답변

1

사용자가 JButtonActionListener을 사용하여 사용자가 단추를 클릭 할 때 입력을 가져올 수 있습니다. 텍스트 필드가있는 문자열을 가져 오려면 textField.getText()을 사용하십시오.

다음은 간단한 예입니다 :

// Create a Textfield 
    JTextField textField = new JTextField(); 

    // Create a Button 
    JButton button = new JButton("Let's go"); 

    // Add an Actionlistener to the button 
    button.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent event) { 
      // Set word2 with the input string from the textfield 
      word2 = textField.getText(); 
     } 
    }); 

    // Create a window 
    JFrame window = new JFrame(); 
    // Exit on close 
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    // set the LayoutManager 
    window.setLayout(new BorderLayout()); 
    // Add the textfield and button to the JFrames contentpane. 
    window.add(textField); 
    window.add(button, BorderLayout.SOUTH); 

    window.pack(); 
    window.setVisible(true); 

이 JTextFields의 사용 설명 오라클의 훌륭한 예입니다 http://download.oracle.com/javase/tutorial/uiswing/components/textfield.html

0

나는 아래의 코드를 테스트하지 않았다, 그리고 당신은 MByD을 청취한다 Swing 튜토리얼을 따라 가십시오. 그러나 당신에게 예를 드리기 위해 그것이 어떻게 작동 할 수 있는지가 나와 있습니다.

public class MyFrame extends JFrame{ 
    private JTextField textField; 
    public MyFrame(){ 
     this.textField = new JTextField(); 
     add(this.textField); 
    } 

    public JTextField getTextField(){ 
     return this.textField; 
    } 
} 

그리고 어딘가에 코드 : 나는 추천

myFrame.getTextField.getText(); 
관련 문제