Hey I'm working with Java and cant seem to get my calculater working, Im trying to use my actionListener to make it all work.. Please help:)

import java.awt.FlowLayout;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SimpleCalculator extends JFrame implements ActionListener{

	JPanel p1=new JPanel(new FlowLayout(FlowLayout.LEFT,2,2));


	private JTextField jtfNum1=new JTextField(4);
	private JTextField jtfNum2=new JTextField(4);
	private JTextField jtResult=new JTextField(4);
	
	JPanel p2=new JPanel(new FlowLayout(FlowLayout.LEFT,2,2));


		JButton jbtadd=new JButton("Add");
		JButton jbtsubtract=new JButton("Subtract");
		JButton jbtmultiply=new JButton("Multiply");
		JButton jbtdivide=new JButton("Divide");
	
		

	public static void main(String[] args) {
		SimpleCalculator frame= new SimpleCalculator();
		frame.setTitle("Exercise16_4");
		frame.setSize(325,150);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	public SimpleCalculator(){
		
		p1.add (new JLabel("Number 1"));
		p1.add(jtfNum1);
		
		p1.add(new JLabel("Number 2"));
		p1.add(jtfNum2);
		
		p1.add(new JLabel("Result"));
		p1.add(jtResult);
		jtResult.setEditable(false);
		
		
		
		
		p2.add(jbtadd); 
		p2.add(jbtsubtract);
		p2.add(jbtmultiply);
		p2.add(jbtdivide);
		
		jbtadd.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				//messsagePanel.moveLeft();
			}
		});
		jbtsubtract.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				//messsagePanel.moveLeft();
			}
		});
		jbtmultiply.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				//messsagePanel.moveLeft();
			}
		});
		
		jbtdivide.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				//messsagePanel.moveLeft();
			}
		});
		setLayout(new FlowLayout(FlowLayout.LEFT,10 ,20));
		add (p1);
		add(p2);
		
		
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}

Edited 6 Years Ago by peter_budo: Keep it Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

edited thought you didnt have a panel i see you do. I havent used your method of nesting main within the frame class and creating a frame within the frame class. maybe someone can comment if there are any issues there.

Edited 6 Years Ago by adams161: misread program

i did a class with a key listener which should work like an action listener. when i added the key listener directly to the text field, i.e. the key listener class is nested in the textfield control, neathier my panel or frame should implement KeyListener. it complains my panel is not overriding abstract method ( in your case probably action listener). and its not. only my textfield is. i dont need to implement anything.


this compiles

import javax.swing.*;

import java.awt.event.*;

public class keypress
{


public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("keypress");
frame.setSize(325,150);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mypanel panel = new mypanel();
frame.add(panel);

frame.setVisible(true);
}
}

class mypanel extends JPanel
{
JTextField box;
JLabel alabel;
mypanel()
{

alabel = new JLabel("hi there");
box = new JTextField(30);
//box.setFocusable(true);
box.addKeyListener(new KeyListener() {public void keyPressed(KeyEvent e) {
	int a=e.getKeyCode();
	if(a == 10)
		box.setText("enter pressed");
    }

   public void keyTyped(KeyEvent e) {;
	
    }

  
    /** Handle the key-released event from the text field. */
    public void keyReleased(KeyEvent e) {;
	
    }

}

);
add(box);
add(alabel);

}

}

Edited 6 Years Ago by adams161: n/a

This article has been dead for over six months. Start a new discussion instead.