|
||
|
|||
|
caN ANYONE HELP ME IN RUNNING MY CODE SO THAT WHENVER I PRESS ANY BUTTON IT DISPLAYS MESSSAGE IN TEXT FIELD...
Java Code
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
class NewClass1{
public static void main(String args[]){
JFrame f =new JFrame("faria");
JTextField t=new JTextField(10);
JLabel l=new JLabel("");
JButton j= new JButton("a");
JButton j1= new JButton("b");
JButton j2= new JButton("c");
l.setLayout(new FlowLayout());
f.add(l);
l.add(t);
l.add(j);
l.add(j1);
l.add(j2);
j.addActionListener(new A());
j1.addActionListener(new B());
j2.addActionListener(new C());
f.setVisible(true);
f.setSize(400,400);
}
}
class A implements ActionListener{
public void actionPerformed(ActionEvent e) {
// counter++;
System.out.println("hiii" );
}
}
class B implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("hello" );
}
}
class C implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("how r u" );
}
}
|
|
||||
|
Hello fari,
Welcome to the Java Programming Forums ![]() Please take a look at this link, im sure it will help. How to Add ActionListener to a JButton. Java Swing
__________________
Don't forget to add syntax highlighted code tags around your code: [highlight=Java] code here [/highlight] Forum Tip: Add to peoples reputation ( ) by clicking the button on their useful posts.
|
|
||||
|
Java Code
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Component;
import java.awt.Container;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.AbstractButton;
import java.util.*;
class NewClass1 extends JFrame implements ActionListener{
JTextField t;
JLabel l;
private JButton j, j1, j2;
JFrame f;
public NewClass1()
{
// JFrame f =new JFrame("faria");
JTextField t= new JTextField(15);
JLabel l=new JLabel("Label");
JButton j= new JButton("a");
j.addActionListener(this);
JButton j1= new JButton("b");
j1.addActionListener(this);
JButton j2= new JButton("c");
j2.addActionListener(this);
t.setText("5");
// l.setLayout(new FlowLayout());
// f.add(l);
// l.add(t);
// l.add(j);
// l.add(j1);
// l.add(j2);
setTitle("farris");
Container pane = getContentPane();
pane.add(l);
pane.add(t);
pane.add(j);
pane.add(j1);
pane.add(j2);
// j.addActionListener(new A());
// j1.addActionListener(new B());
// j2.addActionListener(new C());
pane.setLayout(new GridLayout(4,4));
setVisible(true);
setSize(400,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[]){
NewClass1 refVar = new NewClass1();
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("a"))
{
System.out.println("hiii" );
t.setText("hi");
}
else if (e.getActionCommand().equals("b"))
{
System.out.println("hello" );
t.setText("hello");
}
else if (e.getActionCommand().equals("c"))
{
System.out.println("how r u" );
t.setText("how r u");
}
}
}
at NewClass1.actionPerformed(NewClass1.java:98) which is t.setText("how r u"); |
|
||||
|
What is the scope of the t variable assigned a value in the Constructor?
|
| The Following User Says Thank You to Norm For This Useful Post: | ||
javapenguin (12-06-2010) | ||
|
||||
|
What text book are you using?
I bet you could get a definition using Google. Nothing to do with coordinates. |
| The Following User Says Thank You to Norm For This Useful Post: | ||
javapenguin (12-06-2010) | ||
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Action Listener | Suzanne | What's Wrong With My Code? | 7 | 29-05-2010 03:50 PM |
| Beginner Help (Action Listener) | gradstudent | AWT / Java Swing | 2 | 30-04-2010 03:26 PM |
| Action Listener | kray66 | AWT / Java Swing | 2 | 19-04-2010 08:26 PM |
| Problem with Action Listener | JonoScho | AWT / Java Swing | 4 | 19-03-2010 05:03 AM |
| Need Help Action Listener.... | vhizent23 | AWT / Java Swing | 2 | 09-10-2009 06:46 PM |
|
100 most searched terms
Search Cloud
|
| action listener in java actionlistener actionlistener in java addactionlistener addactionlister adding elements to two dimensional arraylist arraylist value into hash cannot find symbol method create an abstract class called shape with abstract methods+java double format java double to integer in java double to integer java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread main java.lang.outofmemoryerror java heap space in eclipse exception in thread main java.lang.reflect.invocationtargetexception format double java get mouse position java how to convert list to map in java how to make a calculator in jframe using jcreator http://www.javaprogrammingforums.com/object-oriented-programming/3713-limiting-decimal-places-double.html iphone java java actionlistener java coding - user entered input java double format java double to int java font attributes java format double java forum java forums java get mouse position java grect center java jbutton java programming codes using astirisks java programming forum java programming forums java.lang.outofmemoryerror: java heap space java.lang.reflect.invocationtargetexception jbutton actionlistener jbutton with key enter jtable questions in java jtext bold jtextfield output from formula jxl.read.biff.biffexception: unable to recognize ole stream mean value decimal double java programmer forum scanner char smack api messagelistener transaction using gui and 2 dimensional array code in java two dimensional arraylist in java |