【问题标题】:How to run two c++ file on cmd using java code for gui如何使用 gui 的 java 代码在 cmd 上运行两个 c++ 文件
【发布时间】:2015-10-30 06:10:16
【问题描述】:

我对使用 java 及其支持库/类制作 Gui 非常陌生。但我制作了计算器。我习惯用 c++ 编写代码。所以我发现很难在 java 代码中进行系统调用来运行 c++ 文件。这是使用一些库/类在 Eclipse 编辑器上实现的计算代码。

    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);
        }

    }

我的 gui 界面如下。

我只想在单击添加按钮时运行一个 c++ 文件(比如 add.cpp )。 我也在谷歌上搜索,也得到了很多解决方案,但很长的解决方案偏离了我的情况。

【问题讨论】:

    标签: java c++ user-interface


    【解决方案1】:

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

    要真正尝试按原样回答这个问题,我认为您基本上可以选择三个选项,我将按照从最不疯狂到最疯狂的顺序进行介绍。

    1. 将 C++ 代码编译为 DLL,然后从 Java 调用 DLL 中的函数。 SO上这个问题的答案可能有助于这条路线:How to call external dll function from java code
    2. 将 C++ 代码编译为接受命令行参数的可执行文件,然后从 Java 调用该程序。例如创建一个可以用“add.exe 5 10”调用的程序“add.exe”,它会打印出“15”。然后,您的 Java 程序使用适当的参数运行它,并捕获输出以将其显示给用户。您还可以使用文件在程序、套接字或任何其他进程间通信方法之间传递参数和结果。您可以查看这个问题,了解如何运行外部程序并捕获其输出:Capture the output of an external program in JAVA
    3. 打包一个 C++ 编译器和编译你的帮助程序所需的所有必需的包含文件和其他库。让 Java 程序自动将 C++ 源代码编译成程序,然后执行 #2。这将允许您拥有程序的源代码,并实际使用对该源代码的更改。但是,它也需要整个工具链来编译和链接程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-16
      • 2021-07-19
      • 1970-01-01
      相关资源
      最近更新 更多