0

我在 JDialog 中的 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 中制作按钮以取消按下按钮?

4

1 回答 1

2

可能是因为您使用的是 JToggleButton。

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

于 2013-06-03T13:41:50.997 回答