gpt4 book ai didi

java - 如何将控制台迁移到 Swing 应用程序

转载 作者:行者123 更新时间:2023-12-01 12:01:16 25 4
gpt4 key购买 nike

我有一个基本的计算程序,我已经开始构建一个 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 组件的相应操作方法就足够了。操作方法意味着您添加例如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 值。然后它调用你的计算方法。有多种方法可以向多个按钮添加监听器并检测按下了哪个按钮(通过将事件源与组件进行比较),因此您不需要复制所有“从文本字段获取和设置值”代码。

这是基本原则。对于更复杂的应用程序或长时间运行的操作,这可能不应该这样做,因为在 ActionListener 中执行它们意味着它们会阻止 EDT 并且不会处理任何 GUI 事件。

关于java - 如何将控制台迁移到 Swing 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27969602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com