www.webdeveloper.com
Results 1 to 2 of 2

Thread: Keylistener?

  1. #1
    Join Date
    Oct 2004
    Location
    Behind you
    Posts
    32

    Post Keylistener?

    Hey,

    I'm writing an small program in Java to get to know the language, a calculator that only dus plus :P but i want to get it so that you can't type anything but numbers.

    Should i do this with a keylistener? and if yes how?

    i'll inlcude some code


    -----------------------------------------------------------------------
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import javax.swing.JLabel;

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.ActionListener;

    /*
    * Created on Feb 23, 2005
    * @author R. Knijntje
    *
    *
    *
    * This code was designed by R. knijnenburg
    * Copyright @ R. Knijnenburg, http://entertainer.no-ip.com, Mondriaan onderwijsgroep
    */

    public class RekenMachineView extends JFrame
    {

    private JTextField invoer1, invoer2, uitkomst;
    private JButton knop1;

    public RekenMachineView()
    {
    init();
    }

    private void init()
    {

    knop1 = new JButton("=");

    invoer1 = new JTextField();
    invoer1.setBackground(Color.BLUE);
    invoer1.setPreferredSize(new Dimension(150 , 30));

    invoer2 = new JTextField();
    invoer2.setBackground(Color.RED);
    invoer2.setPreferredSize(new Dimension(150 , 30));

    uitkomst = new JTextField("0");
    uitkomst.setBackground(Color.GREEN);
    uitkomst.setPreferredSize(new Dimension(320 , 30));
    uitkomst.setEditable(false);

    JPanel panelCenter = new JPanel();
    JLabel label1 = new JLabel("+");
    panelCenter.add(knop1);

    JPanel panelNoord = new JPanel();
    panelNoord.add(invoer1);
    panelNoord.add(label1);
    panelNoord.add(invoer2);

    JPanel panelZuid = new JPanel();
    panelZuid.add(uitkomst);

    getContentPane().add(panelNoord,BorderLayout.NORTH);
    getContentPane().add(panelZuid,BorderLayout.SOUTH);
    getContentPane().add(panelCenter,BorderLayout.CENTER);

    setTitle("Rekenmachine");
    setIconImage(new ImageIcon("icon.jpg").getImage());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setLocation(300,175);
    setVisible(true);
    }

    public String geefInvoer1()
    {
    String waarde1 = invoer1.getText();
    return waarde1;
    }

    public String geefInvoer2()
    {
    String waarde2 = invoer2.getText();
    return waarde2;
    }

    public void toonResultaat(int uitkomst)
    {
    String stringUitkomst = Integer.toString(uitkomst);
    this.uitkomst.setText(stringUitkomst);
    }

    public void voegLuisteraarToe(ActionListener l)
    {
    knop1.addActionListener(l);
    }

    }

    ------------------------------------------------------------------------

    import javax.swing.JFrame;

    /*
    * Created on Feb 24, 2005
    * @author R. Knijntje
    *
    *
    *
    *
    * Created by R. Knijnenburg
    * Copyright @ R. Knijnenburg
    */

    public class RekenMachineStarter
    {
    public static void main(String[] args)
    {
    JFrame.setDefaultLookAndFeelDecorated(true);
    RekenMachineView mijnView = new RekenMachineView();
    RekenMachineModel mijnModel = new RekenMachineModel();
    RekenMachineLuisteraar l = new RekenMachineLuisteraar(mijnModel, mijnView);
    mijnView.voegLuisteraarToe(l);
    }
    }

    ------------------------------------------------------------------------

    /*
    * Created on Feb 23, 2005
    * @author R. Knijntje
    *
    *
    *
    * This code was designed by R. knijnenburg
    * Copyright @ R. Knijnenburg, http://entertainer.no-ip.com, Mondriaan onderwijsgroep
    */

    public class RekenMachineModel
    {

    private int getal1, getal2;

    public void setGetal1(int waarde1)
    {
    getal1 = waarde1;
    }

    public void setGetal2(int waarde2)
    {
    getal2 = waarde2;
    }

    public int geefOptelling()
    {
    int som = getal1 + getal2;
    return som;
    }
    }

    -------------------------------------------------------------------------

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JOptionPane;
    /*
    * Created on 21-mrt-2005
    * @author R. Knijntje
    *
    *
    *
    *
    * Created by R. Knijnenburg
    * Copyright @ R. Knijnenburg
    */

    public class RekenMachineLuisteraar implements ActionListener
    {
    public class RekenMachineValidator
    {





    }

    private RekenMachineModel m;
    private RekenMachineView v;


    public RekenMachineLuisteraar(RekenMachineModel m , RekenMachineView v)
    {
    this.m = m;
    this.v = v;
    }

    public void actionPerformed(ActionEvent arg0)
    {
    String stringGetal1 = v.geefInvoer1();
    String stringGetal2 = v.geefInvoer2();

    int intGetal1;
    int intGetal2;

    try
    {
    intGetal1 = Integer.parseInt(stringGetal1);
    intGetal2 = Integer.parseInt(stringGetal2);

    m.setGetal1(intGetal1);
    m.setGetal2(intGetal2);

    v.toonResultaat(m.geefOptelling());
    }

    catch(NumberFormatException invoerFout)
    {
    JOptionPane.showMessageDialog(null, "Dude, kan je niet eens iets goeds invullen?", "Error", JOptionPane.ERROR_MESSAGE);
    }
    }
    }
    Last edited by Entertainer; 04-20-2005 at 03:22 AM.

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,123
    to capture keyEvents you will need to implement KeyListener interface. This will have I think 4 methods that you will need to implement. and thats it. If you need to further help onthis let me know
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles