2013-08-05 148 views
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不太好,所以任何幫助都會很有幫助。是因爲我聲明我的按鈕對象是受保護的嗎?我在做什麼錯?

+0

在這行你得到這個錯誤?是否有關於您可以給出的錯誤消息的更多信息? –

+0

任何包含buttonGuns,buttonMotorcycles和buttonMusic的行。我搞砸了可見性修改器,但沒有運氣。它不斷給出一個錯誤指向這些按鈕,並說「找不到符號」。我使用TextPad是因爲我對此界面非常滿意,但它也是我理解中最不有用的開發程序。 –

+0

當您發佈有關錯誤的問題時,請務必發佈完整的確切錯誤信息,包括確切的信息和準確的信息。這樣你會得到更好的幫助。 –

回答

0

移動MainScreen到自己的Java文件和新MainScreen

+0

好的,如bellabax推薦,我將MainScreen類移入一個新文件,並將Listener放在MainScreen類中,現在它正在做我所需要的。感謝大家的幫助!! –

1

您的JButton實例在偵聽器中不可見;它們被定義在一個完全不同的類中。因爲它們在MainScreen類定義

1

你Jbutton將移動類監聽器內部類是沒有監聽器類的內部訪問。你需要把你的MainScreen類放在Listener類中。嘗試將它們放在一起,如下所示:

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

+0

謝謝你的鏈接和是的,Java真的很混亂,但這也是因爲我慢了大聲笑。 –

+0

@ChrisTran:這個解決方案你仍然有錯誤嗎? –

+0

感謝幫助的朋友!實際上我只是將MainScreen類移到了一個單獨的文件中,並且所有的東西都像它應該的那樣工作,並將Listener類放在MainScreen類中,所以現在它們是2個獨立的文件Main和MainScreen。 –