如何将批处理脚本多个选择集成到JAVA GUI中?

时间:2014-07-25 04:21:05

标签: java windows batch-file user-interface cmd

EDITED 27/7/2014

请仔细阅读,因为我的问题很复杂

嗨,我想做一个编码,它涉及JAVA GUI,批处理文件和命令提示符。

我从本网站获得了部分答案:Batch scripting multiple selection

这是我现在在批处理文件中的内容[例如]:

echo.
echo Selection time!
echo.
echo 1. My father is Joe
echo 2. My mother is Audrey
echo 3. My brother is Jerry
echo 4. My elder sister is June
echo 5. My youngest sister is Awy
echo 6. Include All
echo.

:getOptions
set /p "choices=Type the number without spacing (e.g. 1,2,3): "

if not defined choices ( 
    echo Please enter a valid option
    goto getOptions
    )

for %%a in (%choices%) do if %%a EQU 6 set choices=1,2,3,4,5
for %%i in (%choices%) do call :option-%%i

echo.
echo Done
pause
exit

:option-1
echo My father is Joe > Family.txt
exit /B

:option-2
echo My mother is Audrey > Family.txt
exit /B

:option-3
echo My brother is Jerry > Family.txt
exit /B

:option-4
echo My elder sister is June > Family.txt
exit /B

:option-5
echo My youngest sister is Awy > Family.txt
exit /B

接下来,我还希望将这个批处理文件包含到一个java GUI中,这样就会有一些复选框供用户选择,当用户勾选方框#1 时,< strong>框#2 和框#3 或者它可能会按顺序勾选复选框,但是当用户点击“确定”时。它会将勾选的框值传递给批处理文件(它将变为 1,2,3或1,3,2或2,3,1 ),然后它将在命令提示符下运行。

以下是我在java文件中的内容[例如]:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JCheckBox;
import java.awt.Window.Type;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class GUI extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
           public void run() {
                try {
                   GUI frame = new GUI();
                   frame.setVisible(true);
               } catch (Exception e) {
                    e.printStackTrace();
                }
            }
       });
    }

    /**
    * Create the frame.
    */
   public GUI() {
        setTitle("FAMILY");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JCheckBox chckbxMyFatherIs = new JCheckBox("My Father is Joe");
        chckbxMyFatherIs.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        chckbxMyFatherIs.setBounds(45, 48, 137, 23);
        contentPane.add(chckbxMyFatherIs);

        JCheckBox chckbxNewCheckBox = new JCheckBox("My Mother is Audrey");
        chckbxNewCheckBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        chckbxNewCheckBox.setBounds(196, 48, 198, 23);
        contentPane.add(chckbxNewCheckBox);

        JCheckBox chckbxNewCheckBox_1 = new JCheckBox("My Bother is Jerry");
        chckbxNewCheckBox_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        chckbxNewCheckBox_1.setBounds(45, 97, 137, 23);
        contentPane.add(chckbxNewCheckBox_1);

        JCheckBox chckbxNewCheckBox_2 = new JCheckBox("My eldest Sister is June ");
        chckbxNewCheckBox_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        chckbxNewCheckBox_2.setBounds(196, 97, 198, 23);
        contentPane.add(chckbxNewCheckBox_2);

        JCheckBox chckbxNewCheckBox_3 = new JCheckBox("My youngest sister is Awy");
        chckbxNewCheckBox_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
           }
        });
        chckbxNewCheckBox_3.setBounds(196, 149, 198, 23);
        contentPane.add(chckbxNewCheckBox_3);

        JCheckBox chckbxAll = new JCheckBox("All");
        chckbxAll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        chckbxAll.setBounds(45, 149, 97, 23);
        contentPane.add(chckbxAll);
    }
}

我刚学会编码java但编写java GUI对我来说有点困难。我开始编写的代码不仅仅是批处理文件中的上述编码。这就是我来这里寻求帮助的原因。我希望我的解释足够清楚。如果您对我的问题不清楚,请随时问我任何问题。任何帮助将不胜感激!

所以......我的问题是如何将批处理脚本集成到JAVA GUI中?

2 个答案:

答案 0 :(得分:0)

您可以编写Java程序并使用Scanner从用户那里获取输入:

Scanner in = new Scanner(System.in);

您可以使用以下命令从批处理文件调用此程序:

@ECHO OFF

%JAVA_HOME%\bin\java MyClass

给了你一些指示,让你休息一下。

干杯!!

答案 1 :(得分:0)

更改此行:

if %choices% equ 6 set choices=1,2,3,4,5

这一个:

if "%choices:6=%" neq "%choices%" set choices=1,2,3,4,5

我还建议您使用arrays

编辑已添加示例

@echo off

:getOptions
set "choices="
set /P "choices=Choices: "
if not defined choices goto :EOF
if "%choices:6=%" neq "%choices%" set choices=1,2,3,4,5,6
echo Execute: %choices%
goto getOptions

示例输出:

C:\> test.bat
Choices: 1,3,5
Execute: 1,3,5
Choices: 1,2,4,6
Execute: 1,2,3,4,5,6
Choices: 1,6
Execute: 1,2,3,4,5,6
Choices:

编辑:我显然在替换中也包含了6个错误,但是你明白了!