Hi daniwebbers,

I am a newbie learning java and keep getting a "cannot find symbol" error when I try to compile my program. I've tried searching but I don't really know what search terms I should be using in this case. Can anyone see the problem?

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

class menuFrame extends JFrame implements ActionListener{
	public static void main(String[] args){
	new menuFrame();
	}
	
	
	public menuFrame(){

	JPanel menuPanel = new JPanel();
	menuPanel.setLayout(null);
		
	JLabel lblName = new JLabel("Name: ");
		lblName.setBounds(30, 20, 100, 20);
	JTextField txtName = new JTextField(15);
		txtName.setBounds(80,20,150,20);
	JButton btnStart = new JButton("Start");
		btnStart.setBounds(90,60,100,20);
		
	menuPanel.add(lblName);
	menuPanel.add(txtName);
	menuPanel.add(btnStart);
	setDefaultProperties();
	}
	
	public void setDefaultProperties(){
	this.setTitle("Hello World!");
	this.setSize(300,200);
	this.setVisible(true);
	this.setLocationRelativeTo(null);
	this.add(menuPanel);
	}
	
	public void actionPerformed(ActionEvent e)
	{
	if (e.getSource() == btnStart)
	btnStart.setText("lol");
	}
	
}

Thanks in advance!

You calling new menuFrame() class, but just bellow it you have only method with similar signature. You need to make up your mind if you want to call class or just method. In case of class call your code may look like this

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

class MyFrame {
    public static void main(String[] args) {
        new MenuFrame();
    }


    public static class MenuFrame extends JFrame implements ActionListener {
        private JPanel menuPanel;
        private JButton btnStart;
        
        public MenuFrame() {
            menuPanel = new JPanel();
            menuPanel.setLayout(null);

            JLabel lblName = new JLabel("Name: ");
            lblName.setBounds(30, 20, 100, 20);
            JTextField txtName = new JTextField(15);
            txtName.setBounds(80, 20, 150, 20);
            JButton btnStart = new JButton("Start");
            btnStart.setBounds(90, 60, 100, 20);

            menuPanel.add(lblName);
            menuPanel.add(txtName);
            menuPanel.add(btnStart);
            setDefaultProperties();
        }

        public void setDefaultProperties() {
            this.setTitle("Hello World!");
            this.setSize(300, 200);
            this.setVisible(true);
            this.setLocationRelativeTo(null);
            this.add(menuPanel);
        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == btnStart)
                btnStart.setText("lol");
        }

    }
}
commented: did not point to a real problem, OP's constructor is fine. -1

actually peter his main calls the constructor and everything is named fine, the one thing that could be better is actually assigning the instance he creates to a variable but i dont believe this is what his issue is.

Person192's problem comes from the fact that he declares his JPanel inside his constructor :

public MenuFrame{
   ...
   JPanel menuPanel = new JPanel();
   ...
}

then calls it from setDefaultProperties() :

public void setDefaultProperties(){
	...
	this.add(menuPanel);
}

he also refers to his start button from action performed while its long dead since its scope is only within the constructor also :

public void actionPerformed(ActionEvent e)
{
     if (e.getSource() == btnStart)btnStart.setText("lol");
}

Its a scope problem.

Giving the poor coding I may have overlooked that... Nevertheless there was no point for down-vote from you given that even my approach is valid solution.

im not sure why mrKorbel up voted it in the first place to be honest.

i simply downvoted because the problem you pointed was not a problem, his constructor and class structure were fine, nothing personal, no hard feelings.

@Philippe.Lahaie because upvoting is about

- valuable info

- excelent suggestion

- agreement with corret answer

- nice answer

- etc possitive

otherwise on another side is down_vote, but I can't answering this instigation of down_voting, simple I can't found any answer for down_voting or all were deleted before I see that, and on all Forums where I member of

I just felt like his answer was not correct and did not understand why it was upvoted so i downvoted it back to 0 to reflect that, i would probably not have bothered down-voting it from 0 to -1 for that.

It was just simply not, in my honest opinion, a vote that should've been at 1.

Im sorry i did not use the voting system the way you guys think it should be used, i used it in a way i think it should be used. I respect that you have different opinions about this subject and befor using the voting system again i will consider that input before making a choice. Although in this specific situation, even with the insight you both have brought, i believe i would still down-vote it down to 0.

If there is a written rule about when to or not to vote on a post, please refer me to that thread and i shall respect it.

no rulles not exist, that's only about your decision, don't solve that again, are you think that somebody to solved that, hmmm nobody here, let it be, leave that :-) you're as you're don't change that nor on anonymous Forum

@peter_budo this thread is real adept for locking as not constructive or totally OT

I will give OP another 24 hours to come back on it and then I may consider close it, if before that discussion doesn't get off topic too much (Above is fine sometimes it's needed for clearing things)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.