質問

i have a problem with a JButton in a JDialog, the code is :

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                     
} 

What i have to do in order to make the button inside the JDialog, after i release the mouse button, to unpress the button ?

役に立ちましたか?

解決

Maybe because you are using a JToggleButton.

A toggle button allows the user to change a setting between two states. You should try with a JButton instead.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top