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



EDITED 27/7/2014

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

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

我从这个网站得到了我的部分答案:批量脚本多选择

下面是我现在在批处理文件[EXAMPLE]中的内容:

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框#2框#3时,或者它可能会按顺序勾选复选框,但是当用户单击"确定"时。它将把选中框的值传递给批处理文件(它将变成1,2,3或1,3,2或2,3,1),然后它将在命令提示符中运行。

下面是我现在在java文件[EXAMPLE]中的内容:

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中??

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

Scanner in = new Scanner(System.in);

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

@ECHO OFF

%JAVA_HOME%binjava MyClass

给了你指点,剩下的留给你自己去琢磨。

干杯! !

改变这一行:

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

:

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

我也建议你使用数组。

EDIT: 添加示例

@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:

EDIT:我显然犯了在替换中也包含6的错误,但是你明白了!

最新更新