2015-09-26 23 views
5

我只有6 - 7周的時間學習Java,所以如果我的代碼是草率或術語關閉,我會事先致歉。我試圖創建一個程序來創建一個隨機數,並允許用戶猜測,直到他們得到正確的數字。除了爲我學習的經驗之外,它沒有真正的目的。Java - 數字遊戲 - 同一類中的多個ActionListener

我有基本的程序工作,我只是想添加其他元素來改善它,並獲得經驗。

該程序在JFrame中運行,並具有供用戶輸入其猜測的JTextField。我爲JTextField設置了ActionListener。我想添加一個在遊戲開始時顯示的開始按鈕。當用戶單擊開始按鈕時,JTextField應該變爲活動狀態。另外,當用戶點擊猜測正確答案時,我想使用開始按鈕來重置程序。我已經嘗試了幾種方法來做到這一點,但沒有成功。我相信這將需要同一個類中的多個ActionListener。我甚至不確定這是否可能?

這是我的代碼。預先感謝您的幫助。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.Random; 

public class JMyFrame2 extends JFrame implements ActionListener { 
    Random num = new Random(); 
    int computerGenerated = num.nextInt(1000); 
    public int userSelection; 
    JTextField numberField = new JTextField(10); 
    JLabel label1 = new JLabel(); 
    Container con = getContentPane(); 
    int previousGuess; 

    // constructor for JMyFrame 
    public JMyFrame2(String title) { 
     super(title); 
     setSize(750, 200); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     label1 = new JLabel(
       "I have a number between 1 and 1000 can you guess my number?" + "Please enter a number for your first guess and then hit Enter."); 

     setLayout(new FlowLayout()); 
     add(numberField); 
     add(label1); 
     System.out.println(computerGenerated); 

     numberField.addActionListener(this); 
    } 


    public void actionPerformed(ActionEvent e) { 
     userSelection = Integer.parseInt(numberField.getText()); 
     con.setBackground(Color.red); 

     if (userSelection == computerGenerated) { 
      label1.setText("You are correct"); 
      con.setBackground(Color.GREEN); 
     } else if (userSelection > computerGenerated) { 
      label1.setText("You are too high"); 
     } else if (userSelection < computerGenerated) { 
      label1.setText("You are too low"); 
     } 
    } 
} 


public class JavaProgram5 { 

    public static void main(String[] args) { 


     JMyFrame2 frame2 = new JMyFrame2("Assignment 5 - Number Guessing Game"); 
     frame2.setVisible(true); 
    } 
} 
+0

在程序中實現「重啓」選項有幾種不同的方法。你能解釋一下你在哪一部分遇到麻煩嗎? –

回答

5

當然你可以有多個動作監聽器。 事實上,你的班級不應該實現它。

開始通過移除actionPerformed方法, 並替換該行:

numberField.addActionListener(this); 

有了這個:

numberField.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      userSelection = Integer.parseInt(numberField.getText()); 
      con.setBackground(Color.red); 

      if (userSelection == computerGenerated) { 
       label1.setText("You are correct"); 
       con.setBackground(Color.GREEN); 
      } else if (userSelection > computerGenerated) { 
       label1.setText("You are too high"); 
      } else if (userSelection < computerGenerated) { 
       label1.setText("You are too low"); 
      } 
     } 
    }); 

您可以添加另一個動作監聽計劃啓動按鈕按照這種模式添加 ,使用匿名類。 (它不一定是匿名類, 這只是簡單的演示。)

+0

感謝您的幫助! – Brent

4

正如janos所說,爲每個按鈕添加一個動作偵聽器可以很好地完成這項工作,但在大型代碼中需要增添了不少按鈕,它不看太整齊,我建議你使用setActionCommand()JButton是你們等創建,使用很簡單,你實現ActionListenerJFrame像你一樣,但每個按鈕後添加

button.addActionListener(this); 
button.setActionCommand("commandname") 

而且你可以像按你想的那樣做按鈕,現在可以正確地觸發這些命令功能應該是這樣的:

@Override 
public void actionPerformed (ActionEvent e) { 
String cmd = e.getActionCommand(); 
switch(cmd) { 
    case "action1": 
     // Do something 
     break; 
    case "action2": 
     // Do something else 
     break; 
    case "potato": 
     // Give Mr. chips a high five 
     break; 
    default: 
     // Handle other cases 
     break; 
    } 
} 

等等,再其他的解決方案工作完全正常,我個人認爲這是一個有很多整潔特別是在你有多個動作監聽器代碼。

+0

我喜歡這個解決方案。這很簡單,即使像我這樣的新手也可以遵循它。謝謝! – Brent

+0

我甚至會用'switch-case'來更改所有那些'if'語句。 – Frakcool