Click to See Complete Forum and Search --> : Keylistener?


Entertainer
04-20-2005, 03:00 AM
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);
}
}
}

Khalid Ali
04-20-2005, 01:00 PM
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