【问题标题】:Using a text field and a button to initialize a variable使用文本字段和按钮初始化变量
【发布时间】:2012-04-08 01:10:35
【问题描述】:

我有一段这样的代码,我正在尝试使用按钮来访问 JTextField...

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class NameGameFrame extends JFrame
{
    public static void main( String[] args)
    {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Name Game");
        frame.setLocation(500,400);
        frame.setSize(500,500);

        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        JLabel label = new JLabel("Enter the Name or Partial Name to search:");
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(2,10,10,10);

        panel.add(label,c);

        JTextArea  textarea = new JTextArea(5,30);
        panel.add(textarea);

        JTextField textfield = new JTextField(20);

        JButton button = new JButton("Search");
        c.gridx = 1;
        c.gridy = 1;
        panel.add(button,c);

        panel.add(textfield);

        frame.getContentPane().add(panel, BorderLayout.NORTH);
        frame.setVisible(true);

    }
    static class Action implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            String name  = textfield.getText();
            textarea.append(name);
            textfield.selectAll();
        }
    }
}

我的代码中出现以下错误,我不明白为什么...

  1. 错误找不到符号 String name = textfield.getText();
  2. 错误找不到符号textarea.append(name);
  3. 错误找不到符号textfield.selectAll();

【问题讨论】:

标签: java swing jpanel jtextfield


【解决方案1】:

我认为所有这 3 个错误的原因是相同的 - 您需要将变量移到更高级别,以便两种方法都可以访问它们...

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class NameGameFrame extends JFrame
{

    // Moved these 2 variables to be class-level
    static JTextField textfield = new JTextField(20);
    static JTextArea  textarea = new JTextArea(5,30);

    public static void main( String[] args)
    {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Name Game");
        frame.setLocation(500,400);
        frame.setSize(500,500);

        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        JLabel label = new JLabel("Enter the Name or Partial Name to search:");
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(2,10,10,10);

        panel.add(label,c);

        panel.add(textarea);

        JButton button = new JButton("Search");
        c.gridx = 1;
        c.gridy = 1;
        panel.add(button,c);

        panel.add(textfield);

        frame.getContentPane().add(panel, BorderLayout.NORTH);
        frame.setVisible(true);

    }
    static class Action implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            String name  = textfield.getText();
            textarea.append(name);
            textfield.selectAll();
        }
    }
}

对于 Java 和大多数其他语言,请确保在尝试解决其他错误之前先解决第一个错误 - 通常第一个错误可能会在以后导致其他错误。

【讨论】:

  • 谢谢,我不断收到一个非静态变量无法从静态上下文错误中引用,这些更改。
  • 您需要将变量设为静态 - 请参阅上面编辑过的代码。
  • +1 表示解决方案,-1 表示在主函数中建议 setVisible(true)/add(...),什么时候应该在 Event Dispatcher Thread 上完成 :-) 更多信息 Java Doc
  • @WATTOStudios : 好的 :-)
【解决方案2】:

声明和使用在不同的函数中; 所以你不能访问它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 2011-11-19
    • 1970-01-01
    • 2015-12-23
    相关资源
    最近更新 更多