我究竟做错了什么??

时间:2013-08-05 20:33:25

标签: java swing object jbutton symbols

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

在我详细说明按钮的功能之前,我只是想让一切顺利,但由于某种原因我的按钮在任何地方都看不到!它一直给我一个找不到符号的错误。我对Java不是很了解所以任何帮助都会非常有用。是因为我宣布我的按钮对象是受保护的吗?

3 个答案:

答案 0 :(得分:1)

您的JButton个实例在您的听众中不可见;它们的定义完全不同。

答案 1 :(得分:1)

您的JButtons在Listener类中是不可访问的,因为它们是在MainScreen类中定义的。您需要将Listener类与MainScreen类放在一起。尝试将它们组合在一起:

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

您似乎对Java中的范围存在误解。我强烈建议您阅读有关方法/变量/类的Java范围:

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

答案 2 :(得分:0)

MainScreen移动到自己的java文件中,并将类Listener作为新类MainScreen

的内部类移动