TBD.my

Full Version: [help]problem with action listener
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Graphic implements ActionListener
{
String name;
JTextArea jt;
JButton bt;
JTextField jf;

Graphic(String name){
this.name=name;
init();
}
//frame
public void init()
{
JFrame frame = new JFrame(name);
frame.setLayout(new BorderLayout());
frame.setSize(500,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

//panel center

JPanel p_c = new JPanel();
jt = new JTextArea(15,35);
p_c.add(jt);
frame.add(p_c,BorderLayout.CENTER);

//panel south
JPanel p_s = new JPanel();
bt = new JButton("click");
jf = new JTextField(30);
jf.addKeyListener(new KeyAdapter()
        {
            public void keyPressed(KeyEvent evt)
            {
                if(evt.getKeyCode() == KeyEvent.VK_ENTER)
                {
                    jt.append(jf.getText()+"\n");
                    jf.setText("");
                }
            }
        });
p_s.add(jf);
p_s.add(bt);
bt.addActionListener(this);
frame.add(p_s,BorderLayout.SOUTH);
}

public void actionPerformed(Event evt){
    if(evt.getActionCommand().equals("click"))
        {
            jt.append(jf.getText()+"\n");
            jf.setText("");
    }
}

public static void main(String[] args)
{
Graphic gui = new Graphic("graphic");
}
}
error output
D:\java\Graphic.java:5: error: Graphic is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
public class Graphic implements ActionListener
^
D:\java\Graphic.java:54: error: cannot find symbol
if(evt.getActionCommand().equals("click"))
^
symbol: method getActionCommand()
location: variable evt of type Event
2 errors

Tool completed with exit code 1
Aku rasa kau boleh refer sini...
http://www.dreamincode.net/forums/topic/...t-preform/

ActionListener eh..
Error kt dalam coding tu kt bahagian method actionPerformed

Ni contoh yang btul utk coding ko yg kt atas.

PHP Code:
public void actionPerformed(ActionEvent ae){
    if(
ae.getSource()==bt)
        {
            
jt.append(jf.getText()+"\n");
            
jf.setText("");
    }