【问题标题】:creating a JTextField to receive input from user创建一个 JTextField 以接收来自用户的输入
【发布时间】:2015-08-04 22:07:32
【问题描述】:
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 中提取链接。

此代码提示用户在 ECLIPSE IDE 控制台中键入 URL。键入输入后,代码将从 URL 中提取链接并将输出传输到 JTextArea。

我现在想做的是,我想创建一个 Jtextfield 来接收用户输入,而不是控制台内输入中的用户键。

负责处理字符串输入的代码行是:

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

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

我对GUI的开发经验不多,希望有人能指导我。谢谢。

【问题讨论】:

  • 我建议你阅读这个挥杆教程:zetcode.com/tutorials/javaswingtutorial
  • 小心使用双斜杠,这里的完整路径会破坏您的可移植性:"C:\\Users\\user\\fypworkspace\\FYP\\Link\\abc.txt"

标签: java swing jtextfield


【解决方案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);

Oracle 提供了一些很好的示例来解释 JTextFields 的用法:http://download.oracle.com/javase/tutorial/uiswing/components/textfield.html

【讨论】:

    【解决方案2】:

    我没有测试下面的代码,你应该听 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 myFrame = new MyFrame();
            myFrame.pack();
            myFrame.setVisible(true);
    ...
    

    此时,您可以使用对您的框架的引用来获取文本框中的值,例如:

    myFrame.getTextField.getText();
    

    【讨论】:

      猜你喜欢
      • 2019-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      • 2014-10-26
      相关资源
      最近更新 更多