2015-05-20 48 views
0

你好我在想如何將我的邏輯類鏈接到我的gui類? 我已經寫了一個邏輯類,這是結構,然後不得不給它一個接口;所以我寫了另一個類是gui類,但是idk如何使GUI類從邏輯類中獲取變量。 P.S:它是一個數字猜測遊戲,用戶必須猜測麻木btw 1-10。將邏輯類鏈接到我的gui類java

LOGIC CLASS

import java.util.InputMismatchException; 
import java.util.Random; 
import java.util.Scanner; 

public class GG { 


    public static void main(String[] args) 
    { 
     //random number 
     Random rand = new Random(); 
     int answer = rand.nextInt(10) +1; 
     int guess = 0; 
     int attempts = 0; 
     public 

     //user's guess 

     Scanner keyboard = new Scanner(System.in); 


     GuessingGameGui gui = new GuessingGameGui(); 
     gui.setVisible(true); 


     while(answer != guess) 
     { 
      try 
      { 

       System.out.print("Guess a number between 1 and 10: "); 
       attempts++; 
       guess = keyboard.nextInt(); 
       if (guess < 1 || guess > 10) 
        //throw new BadGuessException() 
        throw new BadGuessException("invalid entry (" + attempts + " attempts so far)"); 
      } 
      catch (BadGuessException e) 
      { 
       System.out.println(e.getMessage()); 
      } 
      catch (InputMismatchException e) 
      { 
       System.out.println("Please enter integers only, and try again"); 
       keyboard.next(); //to get rid of infinite loop issue 
      } 
     } 

     System.out.println("YOU GOT IT (" + attempts + "attempts)"); 

    } 

} 

GUI CLASS

public class GuessingGameGui extends JFrame { 

    public GuessingGameGui() { 

     final int WINDOW_WIDTH = 650; // Window width in pixels 
     final int WINDOW_HEIGHT = 250; // Window height in pixels 

     setTitle("Guessing Game"); 

     setSize(WINDOW_WIDTH, WINDOW_HEIGHT); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


     JPanel north = new JPanel(); 
     JLabel lab1 = new JLabel("Guess a number between 1 and 10?"); 
     setLayout(new FlowLayout()); 
     north.add(lab1); 
     add(north); 

     JPanel center = new JPanel(); 

     final JTextField titleText = new JTextField(); 
     titleText.setPreferredSize(new Dimension(200, 24)); 
     setLayout(new FlowLayout()); 

     JButton button = new JButton("Guess"); 
     Action action = new AbstractAction() 
     { 
      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       String typed = titleText.getText(); 
       int guess = Integer.parseInt(typed); 
      } 
     }; 

     titleText.addActionListener(action); 

     button.addActionListener(action); 
     center.add(titleText); 
     center.add(button); 
     add(center); 

     JPanel south = new JPanel(); 
     JLabel lab2 = new JLabel("YOU GOT IT " + attempts); 
     south.add(lab2); 
     add(south); 

     } 

} 
+0

你是什麼意思的鏈接?有一千種方法可以做到這一點。請更具體一些。 – Alexander

+0

@亞歷山大基本上我想當我鍵入:JLabel lab2 =新JLabel(「你得到它」+嘗試);抓住嘗試的次數並顯示它。我在網上擡頭,我想我必須爲它做吸氣劑? –

+0

結帳模型視圖控制器設計模式http://www.newthinktank.com/2013/02/mvc-java-tutorial/這個傢伙的教程非常適合初學者 – Snickers3192

回答

1

一種常見的方法是通過構造的邏輯對象添加到圖形用戶界面,例如。然後,無論何時GUI上發生什麼情況,都需要在Logic對象上調用正確的方法,並根據需要更新顯示。