0
package SoloProject;

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

public class Main
{
    public static void main(String[] args)
    {
        MainScreen homeScreen = new MainScreen();
        homeScreen.setSize(600, 400);
        homeScreen.setTitle("Chris Tran's Hobby Project");
        homeScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        homeScreen.setLocationRelativeTo(null);
        homeScreen.setVisible(true);
    }//end Main


    class Listen implements ActionListener
    {
        public void actionPerformed(ActionEvent click)
        {
            if (click.getSource()==buttonGuns)
            System.out.println("You are now viewing my gun hobby.");

            if (click.getSource()==buttonMotorcycles)
            System.out.println("You are now viewing my motorcycle hobby.");

            if (click.getSource()==buttonMusic)
            System.out.println("You are viewing my music hobby.");
        }

    }//end Listen

}//end Main class


class MainScreen extends JFrame
{
    protected JButton buttonGuns = new JButton("Click to view my gun hobby!");
    protected JButton buttonMotorcycles = new JButton("Click to view my motorcycle   hobby!");
    protected JButton buttonMusic = new JButton("Click to view my music hobby!");

    public MainScreen()
    {
        setLayout(new FlowLayout());
        buttonGuns.addActionListener(new Listen());
        buttonMotorcycles.addActionListener(new Listen());
        buttonMusic.addActionListener(new Listen());
        add(buttonGuns);
        add(buttonMotorcycles);
        add(buttonMusic);
    }//end MainScreen constructor

}//end MainScreen Class

I'm just trying to get everything in order before I elaborate on the functions of the button but for some reason my button can not be seen anywhere!! It keeps giving me a cannot find symbol error. I'm not very good with Java so any help will be very helpful. Is it because I'm declaring my button objects as protected?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Chris Tran
  • 13
  • 5
  • At which line do you get this error? Is there more information on the error message you can give? – But I'm Not A Wrapper Class Aug 05 '13 at 20:36
  • any line containing buttonGuns, buttonMotorcycles, and buttonMusic. I messed with the visibility modifiers but no luck. It keeps giving an error pointing to these buttons and saying "symbol not found". I'm using TextPad because I'm most comfortable with this interface but it's also the least helpful dev program from my understanding. – Chris Tran Aug 05 '13 at 20:43
  • When you post a question about an error, always post the full, exact error you're getting, including the exact line and exact message. You'll get far better help that way. – Henry Keiter Aug 05 '13 at 20:48
  • If you can use Eclipse download [WindowBuilder](http://www.eclipse.org/windowbuilder/) for GUI design – Luca Basso Ricci Aug 05 '13 at 20:50
  • I'm using TextPad and while my friends think I should use NetBeans or something better, I started on TextPad so it's my preference. I tried NetBeans and Eclipse and there was just too much onscreen. – Chris Tran Aug 05 '13 at 21:18

3 Answers3

1

Your JButton instances are not visible inside your listener; they are defined in an entirely different class.

Thorn G
  • 12,620
  • 2
  • 44
  • 56
1

Your JButtons are not accessible inside Listener class because they are defined in the MainScreen class. You need to put the Listener class with your MainScreen class. Try putting them together like this :

package SoloProject;

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



class MainScreen extends JFrame
{
    protected JButton buttonGuns = new JButton("Click to view my gun hobby!");
    protected JButton buttonMotorcycles = new JButton("Click to view my motorcycle   hobby!");
    protected JButton buttonMusic = new JButton("Click to view my music hobby!");

    public static void main(String[] args)
    {
        MainScreen homeScreen = new MainScreen();
        homeScreen.setSize(600, 400);
        homeScreen.setTitle("Chris Tran's Hobby Project");
        homeScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        homeScreen.setLocationRelativeTo(null);
        homeScreen.setVisible(true);
    }//end Main


    class Listen implements ActionListener
    {
        public void actionPerformed(ActionEvent click)
        {
            if (click.getSource()==buttonGuns)
            System.out.println("You are now viewing my gun hobby.");

            if (click.getSource()==buttonMotorcycles)
            System.out.println("You are now viewing my motorcycle hobby.");

            if (click.getSource()==buttonMusic)
            System.out.println("You are viewing my music hobby.");
        }

    }//end Listen



    public MainScreen()
    {
        setLayout(new FlowLayout());
        buttonGuns.addActionListener(new Listen());
        buttonMotorcycles.addActionListener(new Listen());
        buttonMusic.addActionListener(new Listen());
        add(buttonGuns);
        add(buttonMotorcycles);
        add(buttonMusic);
    }//end MainScreen constructor

}//end MainScreen Class

You seem to have a misunderstanding on scopes in Java. I highly recommend you read about Java scope for methods/variables/classes:

https://stackoverflow.com/a/215505/2498729

Community
  • 1
  • 1
  • thank you for the link and yeah, java is really confusing but that's also because I'm slow lol. – Chris Tran Aug 05 '13 at 21:03
  • appreciate the help friend! i actually just moved the MainScreen class to a seperate file and all seems to be working as it should and put the Listener class inside the MainScreen class so now they are 2 seperate files, Main and MainScreen. – Chris Tran Aug 05 '13 at 21:11
  • Ahh, good. Yah, the Listener must be with the MainScreen class because you set the JButton's to be protected (instead of public!). The Main can be outside as long as it can access the MainScreen class (which it can because its a public class!). – But I'm Not A Wrapper Class Aug 05 '13 at 21:13
0

Move MainScreen to its own java file and move class Listener as inner class on new MainScreen

Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69
  • Ok so as bellabax recommended, I moved the MainScreen class into a new file and put the Listener inside of the MainScreen class and now it's doing what I need it to do. Thank you all for your help!! – Chris Tran Aug 05 '13 at 21:12