如何运行两个c++;使用java gui代码在cmd上创建文件 我很新的使用java及其支持库/类制作GUI,但我制作了计算器。我用C++来编写代码。因此,我发现用java代码调用系统C++来运行C++文件是困难的。这是在Eclipse编辑器上使用一些库/类实现的CALC代码。 package calc; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JLabel; import javax.swing.JOptionPane; import java.awt.Font; public class calc_app { private JFrame frame; private JTextField textField; private JTextField textField_1; private JTextField textField_2; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { calc_app window = new calc_app(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public calc_app() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 455, 297); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); textField = new JTextField(); textField.setBounds(50, 52, 86, 20); frame.getContentPane().add(textField); textField.setColumns(10); textField_1 = new JTextField(); textField_1.setBounds(226, 52, 86, 20); frame.getContentPane().add(textField_1); textField_1.setColumns(10); JButton btnNewButton = new JButton("ADD"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { int num1,num2,ans; num1=Integer.parseInt(textField.getText()); num2=Integer.parseInt(textField_1.getText()); ans = num1+num2; textField_2.setText(Integer.toString(ans)); }catch(Exception arg2){ JOptionPane.showMessageDialog(null, "Please Enter valid Number"); } } }); btnNewButton.setBounds(50, 105, 89, 23); frame.getContentPane().add(btnNewButton); JButton btnNewButton_1 = new JButton("SUBtract"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { int num1,num2,ans; num1=Integer.parseInt(textField.getText()); num2=Integer.parseInt(textField_1.getText()); ans = num1-num2; textField_2.setText(Integer.toString(ans)); }catch(Exception arg1){ JOptionPane.showMessageDialog(null, "Please Enter valid Number"); } } }); btnNewButton_1.setBounds(223, 105, 89, 23); frame.getContentPane().add(btnNewButton_1); JLabel lblNewLabel = new JLabel("The Answer is "); lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 15)); lblNewLabel.setBounds(50, 152, 142, 35); frame.getContentPane().add(lblNewLabel); textField_2 = new JTextField(); textField_2.setBounds(226, 159, 86, 20); frame.getContentPane().add(textField_2); textField_2.setColumns(10); } }

如何运行两个c++;使用java gui代码在cmd上创建文件 我很新的使用java及其支持库/类制作GUI,但我制作了计算器。我用C++来编写代码。因此,我发现用java代码调用系统C++来运行C++文件是困难的。这是在Eclipse编辑器上使用一些库/类实现的CALC代码。 package calc; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JLabel; import javax.swing.JOptionPane; import java.awt.Font; public class calc_app { private JFrame frame; private JTextField textField; private JTextField textField_1; private JTextField textField_2; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { calc_app window = new calc_app(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public calc_app() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 455, 297); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); textField = new JTextField(); textField.setBounds(50, 52, 86, 20); frame.getContentPane().add(textField); textField.setColumns(10); textField_1 = new JTextField(); textField_1.setBounds(226, 52, 86, 20); frame.getContentPane().add(textField_1); textField_1.setColumns(10); JButton btnNewButton = new JButton("ADD"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { int num1,num2,ans; num1=Integer.parseInt(textField.getText()); num2=Integer.parseInt(textField_1.getText()); ans = num1+num2; textField_2.setText(Integer.toString(ans)); }catch(Exception arg2){ JOptionPane.showMessageDialog(null, "Please Enter valid Number"); } } }); btnNewButton.setBounds(50, 105, 89, 23); frame.getContentPane().add(btnNewButton); JButton btnNewButton_1 = new JButton("SUBtract"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { int num1,num2,ans; num1=Integer.parseInt(textField.getText()); num2=Integer.parseInt(textField_1.getText()); ans = num1-num2; textField_2.setText(Integer.toString(ans)); }catch(Exception arg1){ JOptionPane.showMessageDialog(null, "Please Enter valid Number"); } } }); btnNewButton_1.setBounds(223, 105, 89, 23); frame.getContentPane().add(btnNewButton_1); JLabel lblNewLabel = new JLabel("The Answer is "); lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 15)); lblNewLabel.setBounds(50, 152, 142, 35); frame.getContentPane().add(lblNewLabel); textField_2 = new JTextField(); textField_2.setBounds(226, 159, 86, 20); frame.getContentPane().add(textField_2); textField_2.setColumns(10); } },java,c++,user-interface,Java,C++,User Interface,我的gui界面如下。 我只想在单击“添加”按钮时运行一个C++文件(如AD.CPP)。 我也在谷歌上搜索过,也得到了很多解决方案,但是我的解决方案却很长。 < p>你不运行C++代码或.cpp文件;这些代码被编译成机器代码,并由计算机处理器直接执行。我认为,编辑您的问题来解释为什么您认为要从Java程序运行.cpp文件可能会对您更有帮助。对于这个特定的例子,简单地用Java重写计算是最有意义的 为了实际回答这个问题,我认为基本上有三种选择,我将按照从最不疯狂到最疯狂的顺序来介绍 编译C++代码

我的gui界面如下。

我只想在单击“添加”按钮时运行一个C++文件(如AD.CPP)。 我也在谷歌上搜索过,也得到了很多解决方案,但是我的解决方案却很长。

< p>你不运行C++代码或.cpp文件;这些代码被编译成机器代码,并由计算机处理器直接执行。我认为,编辑您的问题来解释为什么您认为要从Java程序运行.cpp文件可能会对您更有帮助。对于这个特定的例子,简单地用Java重写计算是最有意义的

为了实际回答这个问题,我认为基本上有三种选择,我将按照从最不疯狂到最疯狂的顺序来介绍

编译C++代码作为DLL,然后从java调用DLL中的函数。下面这个问题的答案可能对这条路线有所帮助: 编译C++代码作为可接受命令行参数的可执行文件,然后调用java程序。e、 g.创建一个程序“add.exe”,可以用“add.exe 5 10”调用,它将打印出“15”。然后,Java程序使用适当的参数运行它,并捕获输出以将其显示给用户。您还可以使用文件在程序、套接字或任何其他进程间通信方法之间传递参数和结果。您可以看看这个问题,了解如何运行外部程序并捕获其输出: 打包C++编译器和编译辅助程序所需的所有包含文件和附加库。有java程序自动编译C++源代码到程序,然后做2。这将允许您在程序中使用源代码,并允许实际使用对该源代码的更改。但是,它还需要编译和链接程序所需的整个工具链