1

ボタンを使用して 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. エラーでシンボル文字列が見つかりません = textfield.getText();
  2. エラーでシンボルが見つかりません textarea.append(name);
  3. エラーでシンボルが見つかりません textfield.selectAll();
4

2 に答える 2

4

これら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や他のほとんどの言語では、他のエラーを解決する前に、必ず最初のエラーを解決してください。多くの場合、最初のエラーによって後で他のエラーが発生する可能性があります。

于 2012-04-08T01:20:12.477 に答える
2

宣言と使用は異なる機能にあります。そのため、アクセスできません。

于 2012-04-08T01:22:56.500 に答える