Results 1 to 3 of 3
  1. #1
    Peter is offline Senior Member
    Join Date
    Jun 2007
    Posts
    119
    Rep Power
    0

    Default JavaBean Problem

    This code doesn't work, can you check it and then tell me the problem?

    Java Code:
    import java.util.*;
    public class NumberEvent extends EventObject{
    public int number1, number2;
    public NumberEvent(Object o, int number1, int number2) {
    super(o);
    this.number1=number1;
    this.number2=number2;
    }
    }
    
    2/
    import java.util.*;
    public interface NumberEnteredListener extends EventListener{
    public void arithmeticPerformed(NumberEvent mec);
    }
    
    3/ this code I used NetBeans IDE 5.0 to make
    import java.io.Serializable;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    public class NumberBean extends javax.swing.JPanel implements ActionListener, Serializable{
    public NumberBean() {
    initComponents();
    }
    NumberEnteredListener me1;
    public void addNumberListener(NumberEnteredListener me1){
    this.me1=me1;
    }
    NumberEvent mec;
    public void fireNumberEvent(NumberEvent mec){
    me1.arithmeticPerformed(mec);
    }
    public void actionPerformed(ActionEvent ae){
    if(ae.getSource()==ok){
    int f1=Integer.parseInt(tf1.getText());
    int f2=Integer.parseInt(tf2.getText());
    mec=new NumberEvent(ok, f1, f2);
    fireNumberEvent(mec);
    }
    }
    
    4/
    import javax.swing.*;
    public class ArithmeticPerformer extends JFrame implements NumberEnteredListener{
    NumberBean meb;
    
    /** Creates a new instance of ArithmeticPerformer */
    public ArithmeticPerformer() {
    super("Arithmetic Caculations");
    meb=new NumberBean();
    getContentPane().add(meb);
    meb.addNumberListener(this);
    }
    
    public void arithmeticPerformed(NumberEvent mec){
    int result=mec.number1+mec.number2;
    String temp="The result of adding the numbers is : " + String.valueOf(result);
    JOptionPane.showMessageDialog(this, temp);
    }
    
    public static void main(String arg[]){
    ArithmeticPerformer mec=new ArithmeticPerformer();
    mec.setSize(300, 300);
    mec.setVisible(true);
    }
    }
    I can start the UI but the calculation can not operate (The JOptionPane does not appear.).

    Thanks.

  2. #2
    Ed
    Ed is offline Senior Member
    Join Date
    Jun 2007
    Posts
    110
    Rep Power
    0

    Default

    Peter:
    you forgot to call ok.addActionListener

  3. #3
    Ed
    Ed is offline Senior Member
    Join Date
    Jun 2007
    Posts
    110
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •