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 不是很好,所以任何帮助都会很有帮助。是因为我将按钮对象声明为受保护吗?