Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[help]problem with action listener
04-02-2013, 10:30 AM,
Post: #1
[help]problem with action listener
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
Angry

sharing your knowledge to grow your knowledge...

and the knowledge is life not death
Reply
05-02-2013, 01:30 AM,
Post: #2
RE: [help]problem with action listener
Aku rasa kau boleh refer sini...
http://www.dreamincode.net/forums/topic/...t-preform/

ActionListener eh..
Kalo saya buat salah janganlah marah mcm GayFace
Reply
21-12-2013, 09:42 PM,
Post: #3
RE: [help]problem with action listener
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("");
    }

[Image: Untitled-1.png]
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  [problem]with java tcp client and server chat pron3t 1 467 26-02-2013, 01:21 PM
Last Post: muhaznar
  array problem in GUI th3phantom 1 732 03-05-2012, 09:01 PM
Last Post: cupin06
  [user input char]java if else problem pron3t 1 510 30-03-2012, 04:55 AM
Last Post: s3m00t
  Java Problem? Ujang 3 721 14-03-2012, 01:44 AM
Last Post: 127001
  [help]java error buffered problem and array pron3t 3 794 02-03-2012, 05:25 PM
Last Post: s3m00t
  java problem compile error pron3t 2 716 16-02-2012, 01:52 AM
Last Post: pron3t

Forum Jump: