2013-06-03 33 views
0

我有一个问题,在一个JDialog一个JButton,代码:的JButton保持按下

package asdasd; 

import java.awt.Dialog; 
import javax.swing.JDialog; 
import javax.swing.SwingWorker; 

class F extends javax.swing.JFrame { 

/** 
* Creates new form F 
*/ 
public F() { 
    initComponents(); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">        
private void initComponents() { 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setLocationRelativeTo(null); 
    java.awt.GridBagConstraints gridBagConstraints; 

    dial = new javax.swing.JDialog(); 
    jLabel2 = new javax.swing.JLabel(); 
    jToggleButton2 = new javax.swing.JToggleButton(); 
    jLabel1 = new javax.swing.JLabel(); 
    jToggleButton1 = new javax.swing.JToggleButton(); 

    dial.getContentPane().setLayout(new java.awt.GridBagLayout()); 

    jLabel2.setText("jLabel2"); 
    dial.getContentPane().add(jLabel2, new java.awt.GridBagConstraints()); 

    jToggleButton2.setText("jToggleButton2"); 
    gridBagConstraints = new java.awt.GridBagConstraints(); 
    gridBagConstraints.gridx = 0; 
    gridBagConstraints.gridy = 1; 
    dial.getContentPane().add(jToggleButton2, gridBagConstraints); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    jLabel1.setText("Label"); 

    jToggleButton1.setText("Push"); 
    jToggleButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jToggleButton1ActionPerformed(evt); 
     } 
    }); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup( 
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(161, 161, 161) 
        .addComponent(jLabel1)) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(153, 153, 153) 
        .addComponent(jToggleButton1))) 
      .addContainerGap(184, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup( 
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(69, 69, 69) 
      .addComponent(jLabel1) 
      .addGap(44, 44, 44) 
      .addComponent(jToggleButton1) 
      .addContainerGap(150, Short.MAX_VALUE)) 
    ); 

    pack(); 
}// </editor-fold>       

private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {             
    // TODO add your handling code here: 
    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() { 

     @Override 
     protected Void doInBackground() throws Exception { 

      dial.setVisible(true); 
      dial.setSize(300, 200); 
      dial.setDefaultCloseOperation(EXIT_ON_CLOSE); 
      return null; 
     } 
    }; 

    worker.execute(); 

}             

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* Set the Nimbus look and feel */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(F.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(F.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(F.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(F.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new F().setVisible(true); 
     } 
    }); 
} 
// Variables declaration - do not modify      
private javax.swing.JDialog dial; 
private javax.swing.JLabel jLabel1; 
private javax.swing.JLabel jLabel2; 
private javax.swing.JToggleButton jToggleButton1; 
private javax.swing.JToggleButton jToggleButton2; 
// End of variables declaration      
} 

我有什么,以使的JDialog里面的按钮做的,我松开后鼠标按钮,以解除按钮?

+0

嗯请给它与CodeRanch上的相同问题 – mKorbel

+1

您正在使用'JToggleButton'对象,它们将保持按下状态,除非您再次单击它们或调用'setSelected(false)'。你确定这是你需要的对象吗?否则,'JButton'可能就是你需要的。 – Laf

+0

由于它是一个切换按钮,因此您可以在执行操作时选择和取消选择。 ((JToggleButton)evt.getSource())。setSelected(false); – dosdebug

回答

2

也许是因为您正在使用JToggleButton。

切换按钮允许用户在两​​种状态之间更改设置。 您应该尝试使用JButton。

+0

谢谢你,我没有看到我从Gui builder的调色板中选择了什么 –