2015-01-15 184 views
0

我有一個基本的計算程序,我已經開始構建一個GUI,並且我有一個窗口。我的問題是,我如何將這兩件事聯繫在一起?我聽說我應該首先製作GUI,但這讓我更加困惑。如何將控制檯遷移到swing應用程序

我希望能夠知道如何我的後端連接到前端(GUI)

public class Calc_functions { 

    //declaring subtraction feild 
    public int Sub (int num1, int num2) { 
     //returns the value num1 subtract num2 
     return num1 - num2; 
    } 

    //declaring addition field 
    public int Add (int fnum, int snum) { 
     //returns the value num1 add num2 
     return fnum + snum; 
    } 

    //declaring division field 
    public int Div (int fnum, int snum) { 
     //returns the value num1 divided by num2 
     return fnum/snum; 
    } 

    //declaring multiplication field 
    public int Mult (int fnum, int snum) { 
     //returns the value num1 multiplied by num2 
     return fnum * snum; 
    } 

} 

import java.util.Scanner; 

public class calc_main { 

    public static void main(String[] args) { 
     // calls for the Calc_functions class 
     Calc_functions math = new Calc_functions(); 
     //waits for user imputs and then store it as a variable 
     Scanner numbers = new Scanner(System.in); 
     //prints out too interface 
     System.out.println("Calulator : Enter two numbers and choose a mathmatic symbol + - x /"); 
     System.out.println("_____________________"); 
     //prints out too interface 
     System.out.print("First number:"); 
     int num1 = numbers.nextInt(); 
     //prints out too interface 
     System.out.print("Second number:"); 
     int num2= numbers.nextInt(); 
     //prints out too interface 
     System.out.print("Enter symbol + - x/of the calculation you would like to perform :"); 
     String operation= numbers.next(); 

     // if the user has inputted +, it will carry out the addition of the two variables the user has unputted. 
     if (operation.equals("+")) 
      System.out.println(math.Add(num1, num2)); 
     // if the user has inputted -, it will carry out the addition of the two variables the user has unputted. 
     else if (operation.equals("-")) 
      System.out.println(math.Sub(num1, num2)); 
     // if the user has inputted x, it will carry out the addition of the two variables the user has unputted. 
     else if (operation.equals("x")) 
      System.out.println(math.Mult(num1, num2)); 
     // if the user has inputted /, it will carry out the addition of the two variables the user has unputted. 
     else if (operation.equals("/")) 
      System.out.println(math.Div(num1, num2)); 
     else 
      System.out.println("The operation is not valid."); 

     numbers.close(); 
     System.exit(0); 
    } 

} 

import javax.swing.*; 

// some code used from docs.oracle.com 
public class Calc_gui { 

    private static void GUI(){ 
     JFrame createshowGUI = new JFrame("Calc_gui"); 
     createshowGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Add the ubiquitous "Hello World" label. 
     JLabel label = new JLabel("calcgui"); 
     createshowGUI.getContentPane().add(label); 

     //Display the window. 
     createshowGUI.pack(); 
     createshowGUI.setVisible(true); 
    } 

    public static void main(String[] args) { 
     //Schedule a job for the event-dispatching thread: 
     //creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       GUI(); 
      } 
     }); 
    } 

} 
+0

您可能想詳細說明您想要做什麼,否則這將被關閉得太寬泛。 – 2015-01-15 18:01:54

+0

另外:就我所見,Calc_Functions類中的函數不能是靜態的。 (這樣可以節省你必須傳遞另一個對象的麻煩。) – 2015-01-15 18:02:59

+0

嗨,我想知道在GUI完成後,我將如何開始將我的後端與前端(GUI)連接起來我想要的功能。 – lolcahol123 2015-01-15 18:05:10

回答

1

對於這樣一個簡單的任務,你將不需要「後端」和「前端」。 對於此用例,只需調用您的計算和貴組件的相應操作方法即可。操作方法意味着您可以添加例如ActionListenerJButton然後執行相應的命令,例如執行加法。

然後,您可以提取需求爲4例爲聽衆要執行的代碼,例如(僞代碼,沒有編譯!):

void actionPerformed(ActionEvent e) 
{ //listener for add-button 
    int num1 = Integer.parse(textfield1.getText()); 
    int num2 = Interger.parse(textfield2.getText()); 
    textField3.setText(String.valueOf(math.add(num1, num2))); 
} 

...,然後將它們連接通過addActionListener的按鈕。

上面的代碼從兩個文本字段中獲取兩個值,並嘗試將它們轉換爲int值。然後它調用你的計算方法。有多種方法可以將偵聽器添加到多個按鈕並檢測按下了哪個按鈕(通過比較事件的來源與組件),因此您不需要重複所有「獲取和設置值爲textfields」的代碼。

這是基本原理。它可能不是應該爲更復雜的應用程序或長時間運行的操作完成的方式,因爲在ActionListener中執行它們意味着它們阻止了EDT,並且不會處理GUI事件。

相關問題