使用文本字段和按钮初始化变量

时间:2012-04-08 01:10:35

标签: java swing jpanel jtextfield

我有一段这样的代码,我试图使用按钮来访问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();

2 个答案:

答案 0 :(得分:4)

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

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 :(得分:2)

宣言和使用具有不同的功能; 所以你无法访问它。