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

编辑27/7/2014

请仔细阅读,因为我的问题是一个复杂的

嗨,我想做一个编码,它涉及到一个JAVA GUI,batch file和命令提示符。

我从本网站获得了部分答案: 批量脚本多选

这是我现在在我的batch file[示例]:

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 (eg 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 

接下来,我还想把这个batch file包含到一个Java GUI中,这样用户就可以select一些checkbox,当用户勾选了#1#2#3盒子,或者也许会按顺序勾选checkbox,然后当用户单击“确定”时。 它会将勾号框的值传递给batch file(它将变成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是有点困难的我。 我已经开始在batch file中编写更多的代码。 这就是为什么我来这里寻求帮助。 我希望我的解释清楚。 如果你对我的问题不清楚,请随时问我。 任何帮助将不胜感激!

所以…我的问题是如何将批处理脚本整合到JAVA GUI?

Solutions Collecting From Web of "如何将批处理脚本多选选项集成到JAVA GUI中?"

你可以编写一个Java程序,并使用扫描器从用户输入:

Scanner in = new Scanner(System.in);

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

 @ECHO OFF 

%JAVA_HOME%\bin\java MyClass

给了你指针,给你休息一下,弄清楚。

干杯!

改变这一行:

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

由这一个:

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

我也建议你使用数组 。

编辑添加示例

 @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更换,但你明白了!