数值即时改变
比如我要在TEXT1中输入50, 在 TEXT2 中输入100,那么text3的值会自动变为150这要如何写代码.是什么事件.
import javax.swing.UIManager; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; /* * Main.java * * Created on 2007年12月7日, 下午1:14 */ /** * * @author Eastsun */ public class Main extends javax.swing.JFrame { /** Creates new form Main */ public Main() { initComponents(); } private void initComponents() { bField = new javax.swing.JTextField(); aField = new javax.swing.JTextField(); summerField = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("加法计算器"); setResizable(false); summerField.setEditable(false); summerField.setFont(new java.awt.Font("宋体", 1, 12)); jLabel1.setText(" + "); jLabel2.setText(" = "); 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() .addComponent(bField, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.) .addComponent(jLabel1) .addPreferredGap(javax.swing.) .addComponent(aField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.) .addComponent(jLabel2) .addPreferredGap(javax.swing.) .addComponent(summerField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.[] {aField, bField, summerField}); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(aField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(summerField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1) .addComponent(jLabel2) .addComponent(bField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); bField.getDocument().addDocumentListener(listener); aField.getDocument().addDocumentListener(listener); pack(); }// </editor-fold>//GEN-END:initComponents private void updateSummer() { try { double a = Double.parseDouble(aField.getText()); double b = Double.parseDouble(bField.getText()); summerField.setText(String.valueOf(a + b)); } catch (NumberFormatException ex) { summerField.setText(""); } } /** * @param args the command line arguments */ public static void main(String args[]) { try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }catch(Exception ex){ //do nothing } java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Main().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTextField aField; private javax.swing.JTextField bField; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JTextField summerField; // End of variables declaration//GEN-END:variables private DocumentListener listener = new DocumentListener() { public void insertUpdate(DocumentEvent e) { updateSummer(); } public void removeUpdate(DocumentEvent e) { updateSummer(); } public void changedUpdate(DocumentEvent e) { updateSummer(); } }; }
import javax.swing.UIManager; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; /** * * @author Eastsun */ public class Main extends javax.swing.JFrame { public Main() { bField = new javax.swing.JTextField(16); aField = new javax.swing.JTextField(16); summerField = new javax.swing.JTextField(16); javax.swing.JLabel jLabel1 = new javax.swing.JLabel(" + "); javax.swing.JLabel jLabel2 = new javax.swing.JLabel(" = "); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("加法计算器"); summerField.setEditable(false); bField.getDocument().addDocumentListener(listener); aField.getDocument().addDocumentListener(listener); javax.swing.JPanel panel =new javax.swing.JPanel(new java.awt.BorderLayout()); panel.add(aField,java.awt.BorderLayout.WEST); panel.add(jLabel1,java.awt.BorderLayout.CENTER); panel.add(bField,java.awt.BorderLayout.EAST); javax.swing.JPanel content =new javax.swing.JPanel(new java.awt.BorderLayout()); content.add(panel,java.awt.BorderLayout.WEST); content.add(jLabel2,java.awt.BorderLayout.CENTER); content.add(summerField,java.awt.BorderLayout.EAST); content.setBorder(new javax.swing.border.EmptyBorder(4,16,4,16)); getContentPane().add(content); pack(); setResizable(false); } private void updateSummer() { try { double a = Double.parseDouble(aField.getText()); double b = Double.parseDouble(bField.getText()); summerField.setText(String.valueOf(a + b)); } catch (NumberFormatException ex) { summerField.setText(""); } } public static void main(String args[]) { try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }catch(Exception ex){ //do nothing } new Main().setVisible(true); } private javax.swing.JTextField aField; private javax.swing.JTextField bField; private javax.swing.JTextField summerField; private DocumentListener listener = new DocumentListener() { public void insertUpdate(DocumentEvent e) { updateSummer(); } public void removeUpdate(DocumentEvent e) { updateSummer(); } public void changedUpdate(DocumentEvent e){ } }; }