简体   繁体   中英

How do I call a another class from a class in java

I am trying to call another class that is in the same directory and is compiled. I keep getting the error symbol not found when I call the other class. Can anyone look at my code and see what I am doing incorrectly please.

This is the code:

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

public class NameGameFrame extends JFrame
{
    public static String name;
    static JTextField textfield = new JTextField(20);
    static JTextArea  textarea = new JTextArea(30,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(800,800);

        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,2,2,2);

        panel.add(label,c);

        c.gridx = 0;
        c.gridy = 1;
        panel.add(textarea,c);

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

        c.gridx = 1;
        c.gridy = 0;
        panel.add(textfield,c);

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

        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                name = textfield.getText();
                java.io.File file = new java.io.File("namesdata.txt");
                try
                {
                    Scanner input = new Scanner(file);
                    num = input.nextLine();
                    while (input.hasNext())
                    {
                        NameRecord(name); 
                    }
                }
                catch(FileNotFoundException e)
                {
                    System.err.format("File does not exist\n");
                }
                textarea.setText(fields[0]);
            }
        });

    }
}

NameRecord is the name of the other class that I am calling. Also do I need to put something in the header of the second file class that I am calling?

You can't call a class. You can call methods and constructors.

Your statement NameRecord(name); is nonsense. I think you want to call the constructor:

NameRecord record = new NameRecord(name);

You need a reference to an instance of NameRecord in the class that wants to call its methods. I don't see one. Did I miss it? If not, add one.

I don't believe this is legal Java:

while (input.hasNext())
{
    NameRecord(name); 
}

You need to call new to invoke a constructor, as I see Martijn has pointed out.

In Place of this

  while (input.hasNext())
                {
                    NameRecord(name); 
                }

you have to make a object of that class

     while (input.hasNext())
                {
                    NameRecord record = new NameRecord(name); 
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM