我有jbutton1,当用户单击应用程序运行的jbutton1时,其文本为“ Connect”,并且jbutton1的文本从“ Connect”更改为“ Disconnect”。现在,当用户单击jbutton1且文本为“ Disconnect”时,再次关闭该应用程序。
我在这里给出的代码不会检查任何内容,它会自动关闭不需要的应用程序。我想检查按钮的文本是否连接,我应该执行其他过程,如果断开连接,则单击按钮时应关闭我的应用程序。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(jComboBox2.getSelectedItem()==null)
{
System.out.println("Select one name");
}
else
{
try {
Runtime r = Runtime.getRuntime();
p = Runtime.getRuntime().exec("C:\\Program Files");
AS a4sObj = new AS(new String[]{jComboBox2.getSelectedItem().toString()});
} catch (IOException ex) {
Logger.getLogger(serialportselection.class.getName()).log(Level.SEVERE, null, ex);
}
jButton1.setText("Disconnect"); //This sets connect button to disconnect
if(jButton1.getText()== "Disconnect") // I wanted to change this line of code
{
System.exit(0);
}
最佳答案
只需将setText
移至末尾即可:
if(jButton1.getText()== "Disconnect") // I wanted to change this line of code
{
System.exit(0);
}
jButton1.setText("Disconnect"); //This sets connect button to disconnect