2015-05-12 46 views
-2

我有一個名爲Screen的類,其中包含actionPerformed方法。 我想爲不同的菜單項目提供不同的結果:隨機,積極和人性化。如何使用我的actionPerformed方法的結果來實現主要方法 - java

這一成果影響的主要方法,但我不能確定如何把兩者聯繫起來...

public class Screen extends JFrame 
       implements ActionListener { 

ActionListener listener; 

JMenuItem random = new JMenuItem("Random"); 
JMenuItem aggressive = new JMenuItem("Aggressive"); 
JMenuItem human = new JMenuItem("Human"); 

public Screen(Board board){ 

    //menuBar items 
    menu.add(random); 
    random.addActionListener(this); 
    menu.add(aggressive); 
    aggressive.addActionListener(this); 
    menu.add(human); 
    human.addActionListener(this); 

    .... 
    //sets up board of buttons and adds actionListener to each. 
    .... 
} 
public void actionPerformed(ActionEvent e) { 

     if(e.getSource() == random){ 

     } 
     if(e.getSource() == aggressive){ 

     } 
     if(e.getSource() == human){ 

     } 
    //code for the board buttons - nothing to do with the menu. 
    //But thought it might help 
    if (numClicks == 0){ 
     JButton piece = (JButton) e.getSource(); 
     String xy = piece.getName(); 
     String x = xy.substring(0,1); 
     String y = xy.substring(2,3); 
     FromXInt = Integer.parseInt(x); 
     FromYInt = Integer.parseInt(y); 
     System.out.println("From" + " " +FromXInt + "," + FromYInt); 
    } 
    else{ 
     JButton piece = (JButton) e.getSource(); 
     String xy = piece.getName(); 
     String x = xy.substring(0,1); 
     String y = xy.substring(2,3); 
     ToXInt = Integer.parseInt(x); 
     ToYInt = Integer.parseInt(y); 
     System.out.println("To" + " " + ToXInt + "," + ToYInt); 
    } 
    numClicks++; 
    if (numClicks >= 2){ 
     numClicks = 0;  
    } 
    return; 
} 
} 

我的類,它包含的主要方法:

public class Chess{ 

    public static void main(String [ ] args){ 

    Screen s = new Screen(board); 

    // my attempt but doesn't work 
    if (s.actionPerformed(e) == random){ 


    ..... 

注:我是新到Java仍然試圖讓我的頭多個類的鏈接。 --------------------如果點擊按鈕,ActionPerformed方法還包含事件,但我沒有添加該代碼,因爲它使事情變得複雜.--

+0

你不返回任何東西來自actionPerformed,所以你不能將它與主方法中的任何東西進行比較。如果你想在你的actionPerformed方法中添加return e.getSource(),你可以這樣做。雖然我不完全瞭解你的程序的功能。 – Juru

+0

您是否將ActionListener添加到任何onject? – Suspended

+0

你檢查了e.getSource()返回的內容嗎? – Suspended

回答

-3

你正在調用一個方法,看起來你想使用從該方法返回的東西,但該方法本身不返回任何內容,即「void」。我改變了你的Screen類,以便該方法返回一些東西。

public class Screen extends JFrame 
        implements ActionListener { 
    public Source actionPerformed(ActionEvent e) { 

    .... 

    if(e.getSource() == random){ 

    } 
    if(e.getSource() == aggressive){ 

    } 
    if(e.getSource() == human){ 

    } 

    return e.getSource() 
} 

主要方法現在將能夠從調用中接收結果並使用它。

+0

我得到的錯誤:返回類型與ActionListener.actionPerformed(ActionEvent) – ChatNoir

+1

不兼容@Juru它是錯誤的,重寫方法簽名必須是相同的 – Suspended

+0

@Suspended我不知道它是一個重寫消息,缺少[在]覆蓋註釋然後。 – Juru

0

此方法使用公共枚舉並設置樣式變量根據用戶菜單選擇:

package chess; 
//... 
public class Screen extends JFrame 
     implements ActionListener { 

    private JMenuItem random = new JMenuItem("Random"); 
    private JMenuItem aggressive = new JMenuItem("Aggressive"); 
    private JMenuItem human = new JMenuItem("Human"); 

    public enum PlayStyle {Random, Aggressive, Human}; 
    private PlayStyle style; 

    public Screen(Board board) { 

     //menuBar items 
     menu.add(random); 
     random.addActionListener(this); 
     menu.add(aggressive); 
     aggressive.addActionListener(this); 
     menu.add(human); 
     human.addActionListener(this); 
     //.... 
     //sets up board of buttons and adds actionListener to each. 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     if (e.getSource() == random) { 
      style=PlayStyle.Random; 
     } 
     if (e.getSource() == aggressive) { 
      style=PlayStyle.Aggressive; 
     } 
     if (e.getSource() == human) { 
      style=PlayStyle.Human; 
     } 

     //code for the board buttons - nothing to do with the menu. 
     //.... 
    } 
    public PlayStyle getStyle(){ 
     return style; 
    } 

} 

這是一個包含main方法的類:

package chess; 

import chess.Screen.PlayStyle; 

public class Chess{ 

    public static void main(String [ ] args){ 
     Screen s = new Screen(board); 

     // this attempt will work 
     if (s.getStyle()==PlayStyle.Random) { 
      ... 
     } else if (s.getStyle()==PlayStyle.Aggressive){ 
      ... 
相關問題