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

Getting text from textfield does not work

您好,我在 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
    }
}

你有参考问题。

tf1 被声明为 main 中的局部变量,这使得任何其他 method/context 都无法访问它。加上 mainstatic 的事实,你 运行 进入另一个问题领域。

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

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

    }
}

这是基本的 Java101。您可能会找到类似 What is the difference between a local variable, an instance field, an input parameter, and a class field? 的帮助