问题
This question is also asked by me: How to integrate batch script multiple selections into JAVA GUI?
As I did not get a suitable answer for my question, that's why I decided to ask again in stackoverflow with a more short and sweet manner. Disclaimer: I edited the question in that site. That's why I couldn't find a suitable answer for my question.
I have this batch script which needs to be integrated into a java coding. But this is the first time I am doing integration so I am not familiar how it should be done.
It would be good if anyone can answer my question in that site. But if you aren't, it would also be good to provide me with an example of integrating multiple selection batch script into java.
回答1:
eading through your original post, i can conclude that your solution will be quite easy :
private static String cmdLine = "";
private static final String scriptFile = "MYSCRIPT.sh"
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) {
if(!cmdLine.contains("JOE"))
cmdLine += " JOE ";
}
});
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) {
if(!cmdLine.contains("AUDREY"))
cmdLine += " AUDREY ";
}
});
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) {
if(!cmdLine.contains("JERRY"))
cmdLine += " JERRY ";
}
});
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) {
if(!cmdLine.contains("JUNE"))
cmdLine += " JUNE ";
}
});
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) {
if(!cmdLine.contains("AWY"))
cmdLine += " AWY ";
}
});
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) {
cmdLine = "JOE AUDREY JERRY JUNE AWY";
}
});
chckbxAll.setBounds(45, 149, 97, 23);
contentPane.add(chckbxAll);
}
You will also need an event-listener for your OK-button, in which you can call:
Runtime.getRuntime().exec(scriptFile + cmdLine);
Mind you : this will only ADD parameters to your list, removal (via un-ticking the boxes) also needs to be handled ... i think you now know how. Consider using a list instead of one single string ... thats less messy and allows for dynamic lookup/removal/addition of parameters.
来源:https://stackoverflow.com/questions/24982948/integrating-batch-script-into-java-gui