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不太好,所以任何幫助都會很有幫助。是因爲我聲明我的按鈕對象是受保護的嗎?我在做什麼錯?
在這行你得到這個錯誤?是否有關於您可以給出的錯誤消息的更多信息? –
任何包含buttonGuns,buttonMotorcycles和buttonMusic的行。我搞砸了可見性修改器,但沒有運氣。它不斷給出一個錯誤指向這些按鈕,並說「找不到符號」。我使用TextPad是因爲我對此界面非常滿意,但它也是我理解中最不有用的開發程序。 –
當您發佈有關錯誤的問題時,請務必發佈完整的確切錯誤信息,包括確切的信息和準確的信息。這樣你會得到更好的幫助。 –