微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在每个文本框值的更改事件中自动在摄氏、华氏和开尔文之间转换?

如何解决在每个文本框值的更改事件中自动在摄氏、华氏和开尔文之间转换?

我需要一些帮助来编写一个简单的 Java GUI 程序(用于自学和练习),用于在摄氏、华氏和开尔文之间转换温度单位。

图片

IMAGE

请求:每当我在任何文本框中写入一个数字值(即 123 摄氏度)时,我都希望该值在所有其他文本框中自动转换,而无需使用按钮。

import javax.swing.JFrame;

/*
 * To change this license header,choose License Headers in Project Properties.
 * To change this template file,choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author mohammedh
 */
public class NewJFrame extends javax.swing.JFrame {
    

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        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() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jClear = new javax.swing.JButton();
        jExit = new javax.swing.JButton();
        jC = new javax.swing.JTextField();
        jF = new javax.swing.JTextField();
        jK = new javax.swing.JTextField();

        setDefaultCloSEOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Temperature Converter");

        jLabel1.setFont(new java.awt.Font("Tahoma",1,11)); // NOI18N
        jLabel1.setText("Celsius");

        jLabel2.setFont(new java.awt.Font("Tahoma",11)); // NOI18N
        jLabel2.setText("Fahrenheit");

        jLabel3.setFont(new java.awt.Font("Tahoma",11)); // NOI18N
        jLabel3.setText("Kelvin");

        jClear.setText("Clear");
        jClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jClearactionPerformed(evt);
            }
        });

        jExit.setText("Exit");
        jExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jExitactionPerformed(evt);
            }
        });

        jC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCActionPerformed(evt);
            }
        });

        jF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFActionPerformed(evt);
            }
        });

        jK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jKActionPerformed(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()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(89,89,89)
                        .addComponent(jClear)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE)
                        .addComponent(jExit))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(28,28,28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,false)
                            .addComponent(jF,200,Short.MAX_VALUE)
                            .addComponent(jK)
                            .addComponent(jC))
                        .addGap(0,Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jC,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18,18,18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jF,18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jK,18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jClear)
                    .addComponent(jExit))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE))
        );

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

    private void jCActionPerformed(java.awt.event.ActionEvent evt) {                                   
        
        
    }                                  

    private void jExitactionPerformed(java.awt.event.ActionEvent evt) {                                      
        System.exit(0);
    }                                     

    private void jKActionPerformed(java.awt.event.ActionEvent evt) {                                   
        // Todo add your handling code here:
    }                                  

    private void jClearactionPerformed(java.awt.event.ActionEvent evt) {                                       
        jC.setText(null);
        jK.setText(null);
        jF.setText(null);
    }                                      

    private void jFActionPerformed(java.awt.event.ActionEvent evt) {                                   
        
    }                                  

    /**
     * @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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,ex);
        } catch (illegalaccessexception ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokelater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    public javax.swing.JTextField jC;
    public javax.swing.JButton jClear;
    public javax.swing.JButton jExit;
    public javax.swing.JTextField jF;
    public javax.swing.JTextField jK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration                   
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。