从文本字段获取文本不起作用

发布于 2025-01-11 11:56:43 字数 2153 浏览 0 评论 0原文

您好,我在 yt 上找到了一个 Projekt,您可以在其中搜索关键字,它将显示在 google 上找到的所有网站。现在我正在尝试恢复用户在文本字段中输入的关键字,但它不起作用。它找不到我制作的文本字段(tf1),但我不知道我做错了什么。提前致谢!

这是我的代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Main implements ActionListener {

    public static void main(String[] args) {
        
        int frameWidth = 600;
        int frameHeight = 600;  
        
        JFrame f = new JFrame();
        JLabel l1 = new JLabel();
        JTextField tf1 = new JTextField();
        JButton b1 = new JButton();
        
        
        
        f.getContentPane().setBackground(Color.PINK);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(false);
        f.setSize(frameWidth, frameHeight);
        f.setTitle("Search");
        f.setLocationRelativeTo(null);
        f.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
        
        f.add(l1);
        f.add(tf1);
        f.add(b1);

        l1.setText("Enter Keywords");
        
        tf1.setPreferredSize(new Dimension(200, 20));
        tf1.revalidate();
        
        b1.setText("Search");
        b1.addActionListener(new Main());
        
        
        f.setVisible(true);
        
//      ArrayList<WebCrawler> bots = new ArrayList<>();
        
//      bots.add(new WebCrawler("", 1));
//      bots.add(new WebCrawler("", 2));
//      bots.add(new WebCrawler("", 3));
        
//      for(WebCrawler w : bots) {
//          try {
//              w.getThread().join();
//              
//          }catch(InterruptedException e) {
//              e.printStackTrace();
//          }
//      }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
    
        String keyword = tf1.getText(); //Here it does not find the tf I made
        System.out.println(keyword);    //just for confirmation
    }
}

Hello I found a Projekt on yt where you can search for a keyword and it will show all the websites it found on google. And right now I am trying to revive the keyword the user put in the textfield but it isn't working. It does not find the textfield (tf1) I made, but I don't know what I did wrong. Thanks in advance!

here's my code:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Main implements ActionListener {

    public static void main(String[] args) {
        
        int frameWidth = 600;
        int frameHeight = 600;  
        
        JFrame f = new JFrame();
        JLabel l1 = new JLabel();
        JTextField tf1 = new JTextField();
        JButton b1 = new JButton();
        
        
        
        f.getContentPane().setBackground(Color.PINK);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(false);
        f.setSize(frameWidth, frameHeight);
        f.setTitle("Search");
        f.setLocationRelativeTo(null);
        f.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
        
        f.add(l1);
        f.add(tf1);
        f.add(b1);

        l1.setText("Enter Keywords");
        
        tf1.setPreferredSize(new Dimension(200, 20));
        tf1.revalidate();
        
        b1.setText("Search");
        b1.addActionListener(new Main());
        
        
        f.setVisible(true);
        
//      ArrayList<WebCrawler> bots = new ArrayList<>();
        
//      bots.add(new WebCrawler("", 1));
//      bots.add(new WebCrawler("", 2));
//      bots.add(new WebCrawler("", 3));
        
//      for(WebCrawler w : bots) {
//          try {
//              w.getThread().join();
//              
//          }catch(InterruptedException e) {
//              e.printStackTrace();
//          }
//      }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
    
        String keyword = tf1.getText(); //Here it does not find the tf I made
        System.out.println(keyword);    //just for confirmation
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江南烟雨〆相思醉 2025-01-18 11:56:43

你有一个参考问题。

tf1main 中声明为局部变量,这使得任何其他方法/上下文都无法访问它。再加上 mainstatic 的事实,您就会遇到另一个问题领域。

简单的解决方案是将 tf1 设为实例字段。例如,如果您将 UI 逻辑分组到一个类中,这将进一步简化...

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel label;
        private JTextField textField;
        private JButton searchButton;

        public TestPane() {
            setBorder(new EmptyBorder(32, 32, 32, 32));
            setBackground(Color.PINK);
            setLayout(new GridBagLayout());

            label = new JLabel("Enter Keywords: ");
            textField = new JTextField(20);
            searchButton = new JButton("Search"); 

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(8, 8, 8, 8);
            gbc.gridx = 0;
            gbc.gridy = 0;

            add(label, gbc);
            gbc.gridx++;
            add(textField, gbc);

            gbc.gridy++;
            gbc.gridx = 0;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(searchButton, gbc);

            ActionListener listener = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String text = textField.getText();
                    JOptionPane.showMessageDialog(TestPane.this, "You want to search for: " + text);
                }
            };

            textField.addActionListener(listener);
            searchButton.addActionListener(listener);
        }

    }
}

这是基本的 Java 101。您可能会发现类似 局部变量、实例字段、输入参数和类字段之间有什么区别?< /a> 帮助

You have a reference issue.

tf1 is declared as a local variable within main, which makes it inaccessible to any other method/context. Add into the fact that main is static and you run into another problem area.

The simple solution would be to make tf1 a instance field. This would be further simplified if you grouped your UI logic into a class, for example...

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel label;
        private JTextField textField;
        private JButton searchButton;

        public TestPane() {
            setBorder(new EmptyBorder(32, 32, 32, 32));
            setBackground(Color.PINK);
            setLayout(new GridBagLayout());

            label = new JLabel("Enter Keywords: ");
            textField = new JTextField(20);
            searchButton = new JButton("Search"); 

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(8, 8, 8, 8);
            gbc.gridx = 0;
            gbc.gridy = 0;

            add(label, gbc);
            gbc.gridx++;
            add(textField, gbc);

            gbc.gridy++;
            gbc.gridx = 0;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(searchButton, gbc);

            ActionListener listener = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String text = textField.getText();
                    JOptionPane.showMessageDialog(TestPane.this, "You want to search for: " + text);
                }
            };

            textField.addActionListener(listener);
            searchButton.addActionListener(listener);
        }

    }
}

This is basic Java 101. You might find something like What is the difference between a local variable, an instance field, an input parameter, and a class field? of help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文