我有一个基本的计算程序,我已经开始构建一个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();
}
});
}
}
您可能想详细说明您想要做什么,否则这将被关闭得太宽泛。 – 2015-01-15 18:01:54
另外:就我所见,Calc_Functions类中的函数不能是静态的。 (这样可以节省你必须传递另一个对象的麻烦。) – 2015-01-15 18:02:59
嗨,我想知道在GUI完成后,我将如何开始将我的后端与前端(GUI)连接起来我想要的功能。 – lolcahol123 2015-01-15 18:05:10