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

在详细说明按钮的功能之前,我只是想把所有东西都整理好,但由于某种原因,我的按钮在任何地方都看不到!!它一直给我一个找不到符号错误。我对 Java 不是很好,所以任何帮助都会很有帮助。是因为我将按钮对象声明为受保护吗?

4

3 回答 3

1

您的JButton实例在您的侦听器中不可见;它们被定义在一个完全不同的类中。

于 2013-08-05T20:42:07.743 回答
1

您的 JButton 无法在 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

于 2013-08-05T20:50:56.597 回答
0

移动MainScreen到它自己的 java 文件并将类 Listener 作为内部类移动到 newMainScreen

于 2013-08-05T20:44:15.083 回答