Java错误:无法找到符号,也无法弄清楚原因 [英] Java error: cannot find symbol, and can't figure out why

查看:219
本文介绍了Java错误:无法找到符号,也无法弄清楚原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误:

error: cannot find symbol

看似不起作用:

如果我写道:InvoerVakhandler扩展了boven,然后错误消失,但随后我得到一个无限循环,最后程序崩溃并说堆栈溢出错误。

If I write: "InvoerVakhandler extends boven" then the error disapears but then I get an endless loop and in the end the program crashes and says stack overflow error.

如果我尝试要更改类boven中的}符号并将其放在文本的末尾,然后我会收到更多错误消息。

If I try to change the "}" symbol from class boven and place it in the end of the text then I even get more error messages.

问题:我需要在代码中更改哪些内容才能使其正常工作?

Question: What do I have to change in my code to make it work?

注意:
我是Java的新手我知道有更多这样的帖子但是我不能将它们应用到我的代码中,因为我目前对Java的理解有限。

Note: I'm new to Java and I know there are more posts like this one but I just can't apply them to my code with my current limited understanding of Java.

如果有人想知道:我我正在使用JCreator。

If someone wants to know: I'm using JCreator.

我想做的是什么:我想要做的很简单。

What i'm trying to make: What I'm trying to make is fairly simple.

1)在JTextField中填写一个名称,按回车键,名称应出现在JTextArea中。名称在JTextArea之后,JTextField变为空,因此您可以填写另一个名称,依此类推,应该在JTextArea中显示一个名称列表。 (这就是我现在要做的)

1) Fill in a name in the JTextField, press enter and the name should appear in the JTextArea. After the name is in the JTextArea the JTextField becomes empty so you can fill another name and so on there should appear a list of names in JTextArea. (this is what I'm now trying to make)

2)按下按键kiesWin,让程序从列表中选择一个随机的人。

2) Push the button kiesWin to make the program choose a random person from the list.

3)按下resetL按钮重置程序,这样我就可以制作一个新列表,从中选择一个随机赢家。

3) Push the button resetL to reset the program so I can make a new list to choose a random winner from it.

出现错误的部分代码:(来自InvoerVakHandler类)

Part of the code where the error appears: (from the class InvoerVakHandler)

String invoer = invoervak1.getText();

由于我对java的了解有限,问题可能会在任何地方乱丢,以防我发布整个代码。

With my limited knowledge of java the problem could be litterly anywhere so in case I will post the whole code.

整个代码:

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

// Main method to make the frame
public class Loterij3 extends JFrame {
public static void main( String args[] ) {
    JFrame frame = new Loterij3();
    frame.setExtendedState( frame.MAXIMIZED_BOTH );
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frame.setTitle( "Klanten Register" );
    frame.setContentPane( new Paneel() );
    frame.setVisible( true );
}
}

class Paneel extends JPanel {
private boven Boven;
JTextArea textvak1;
JTextField textvak2;
OnthoudNaam onthoudNaam = new OnthoudNaam();

public Paneel() {
setLayout( new BorderLayout() ); // using border Layout.
setBackground( Color.LIGHT_GRAY );

textvak1 = new JTextArea();
add( new JScrollPane( textvak1 ) );
textvak1.setBackground( Color.WHITE );

textvak2 = new JTextField();
textvak2.setHorizontalAlignment(JTextField.CENTER);
textvak2.setEditable( false );

Boven = new boven();    

add( Boven, BorderLayout.NORTH );
add( textvak1, BorderLayout.CENTER );
add( textvak2, BorderLayout.SOUTH );
}

public class boven extends Paneel {
JButton kiesWin, resetL;
JLabel label1;
JTextField invoervak1;

public boven() {
    setBackground( Color.LIGHT_GRAY );
    setLayout( new GridLayout( 1, 4, 100, 5 ) ); // using GridLayout.
    Border border = 
        BorderFactory.createEmptyBorder( 10, 10, 10, 10 );
    setBorder( border );

    kiesWin = new JButton("Kies een Winnaar!");
    kiesWin.addActionListener( new kies() );
    resetL = new JButton("Reset alles");
    resetL.addActionListener( new reset() );
    label1 = new JLabel("Voer Persoon in en druk op enter: ", JLabel.RIGHT);
    invoervak1 = new JTextField( 20 );
    invoervak1.addActionListener( new InvoerVakHandler() );

    add( label1 );
    add( invoervak1 );
    add( kiesWin );
    add( resetL );
    }
}

// de naam
class naam {
    private String ingevoerdNaam;

    public naam( String ingevoerdNaam) {
        this.ingevoerdNaam = ingevoerdNaam;
    }

    public String getIngevoerdNaam() {
        return ingevoerdNaam;
    }
}

// Arraylist
class OnthoudNaam extends JPanel {
    private ArrayList<naam> lijst;

    public OnthoudNaam() {
        lijst = new ArrayList<naam>();
        }

        public void voegNaamToe(naam x ) {
        lijst.add(x);
        }

        public String toString() {
        StringBuffer buffer = new StringBuffer();
        for(naam x : lijst ) {
        buffer.append( x );
        buffer.append( "\n" );
    }
    return buffer.toString();
}
}

// this is the part where the code goes wrong
public class InvoerVakHandler implements ActionListener {
    public void actionPerformed( ActionEvent e ) {
        String invoer = invoervak1.getText();
        naam naam = new naam( invoer );
        onthoudNaam.voegNaamToe( naam );
        textvak1.setText( onthoudNaam.toString() );
    }
}
    // kies
class kies implements ActionListener {
    public void actionPerformed( ActionEvent e ) {
    }
}

// reset
class reset implements ActionListener {
    public void actionPerformed( ActionEvent e ) {
    }
}
}

对于每个想要帮助我的人:感谢您提前帮助和耐心!

推荐答案

课堂上有问题的一行: InvoerVakHandler

The line in question in in class: InvoerVakHandler

该变量在类中定义: boven

那个这就是为什么它找不到它。

That is why it can't find it.

我认为你可以从 ActionEvent 获取事件的来源传递给 actionPerformed()方法。

I think you can get the source of the event from the ActionEvent passed to the actionPerformed() method.

请注意,通常我们使用大写字母开始名称任何类和小写的开始方法和变量。 (常数是个例外。)

Note that normally we use an upper-case letter to begin the name of any class and lower case to begin methods and variables. (Constants are an exception.)

这篇关于Java错误:无法找到符号,也无法弄清楚原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆