Using a text field and a button to initialize a variable

I have a piece of code like this, where I am trying to use button to access a 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();
        }
    }
}

I am getting the following errors in my code, and I don;t understand why…

  1. error cannot find symbol String name = textfield.getText();
  2. error cannot find symbol textarea.append(name);
  3. error cannot find symbol textfield.selectAll();

I think the cause of all 3 of these errors is the same – you need to move the variables out to a higher level so that both methods can access them…

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

With Java, and most other languages, make sure you solve the first error before you try to solve the others – often the first error can cause other errors later on.

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Using a text field and a button to initialize a variable

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录