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 に答える