954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Jframe newbie question

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!

person192
Newbie Poster
1 post since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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

    }
}
peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

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.

Philippe.Lahaie
Posting Whiz
326 posts since Oct 2007
Reputation Points: 103
Solved Threads: 45
 

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.

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

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
Posting Whiz
326 posts since Oct 2007
Reputation Points: 103
Solved Threads: 45
 

@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

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

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.

Philippe.Lahaie
Posting Whiz
326 posts since Oct 2007
Reputation Points: 103
Solved Threads: 45
 

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

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

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

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

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)

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: