运行时,我在组合框中显示一些com端口值。我需要选择一个com端口值,然后如果我按下连接按钮,我的进程应该发生,否则它应该显示一条错误消息。这种情况我该怎么办?
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Runtime r = Runtime.getRuntime();
p = Runtime.getRuntime().exec("C:\Program Files\Scratch 2\Scratch 2.exe C:\Program Files\Robotix\fwdbckpwm12.sb2");
A4S a4sObj = new A4S(new String[]{jComboBox2.getSelectedItem().toString()});
} catch (IOException ex) {
Logger.getLogger(serialportselection.class.getName()).log(Level.SEVERE, null, ex);
}
}
这个按钮1是"连接"按钮,当我按下这个按钮时,我的文件将打开,但只有当我的组合框值被选中时。
public void addData()
{
ListSerialPorts listSerPortObj = new ListSerialPorts();
listData = listSerPortObj.LoadComPorts2();
for (int index = 0; index < listData.size(); index++)
{
str = (String) listData.get(index);
System.out.println(str);
jComboBox2.addItem(str);
}
这就是我从另一个java类向组合框添加数据的方式。帮助我为我的连接按钮提供条件。
您可以执行以下操作:
if (jComboBox2.getSelectedItem() == null) { //port is not selected
System.out.println("Port is not selected");
} else { //port is selected.
try {
Runtime r = Runtime.getRuntime();
p = Runtime.getRuntime().exec("C:\Program Files\Scratch 2\Scratch 2.exe C:\Program Files\Robotix\fwdbckpwm12.sb2");
A4S a4sObj = new A4S(new String[]{jComboBox2.getSelectedItem().toString()});
} catch (IOException ex) {
Logger.getLogger(serialportselection.class.getName()).log(Level.SEVERE, null, ex);
}
}