[英]How to migrate console to swing application
我有一個基本的計算程序,已經開始構建GUI,並且有一個窗口。 我的問題是,如何將這兩件事聯系在一起? 我聽說我應該先制作GUI,但這使我更加困惑。
我想知道如何將后端連接到前端(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();
}
});
}
}
對於這樣一個簡單的任務,您將不需要“后端”和“前端”。 對於此用例 ,只需調用您的計算以及gui組件的相應操作方法即可。 動作方法意味着您向JButton
添加例如ActionListener
,然后執行相應的命令,例如執行加法。
然后,您可以將這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
值。 然后它調用您的計算方法。 有多種方法可以將偵聽器添加到多個按鈕並檢測按下了哪個按鈕(通過將事件的來源與組件進行比較),因此您無需重復所有“從文本字段獲取和設置值”代碼。
這是基本原則。 對於更復雜的應用程序或長時間運行的操作,可能不是這樣做的方式,因為在ActionListener
執行它們意味着它們正在阻止EDT,並且不會處理任何GUI事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.