STILL Can't make buttons WORK!!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: STILL Can't make buttons WORK!!

Hybrid View

  1. #1
    superwebmonkey Guest

    STILL Can't make buttons WORK!!


    Yeah, I can find this kind of info. I can find classes, and how to make instantiate,
    and all that...but just now how to make it DO SOMETHING.


    b.addActionListener(new ActionListener()
    { public void actionPerformed(ActionEvent evt)
    {
    //button code here
    }
    });

    Where this code says button code here...that's what I need.
    I know, it depends on what you want it to do.....I just want it to do ANYTHING.
    If I can get the button to do anything, I can figure out how to make it
    do more complex things eventually...but all I can find are complex actions.

    I just want, "If button is pushed change the text."
    OR"If button is pushed replace the button."

    I appreciate all of the help!!!

  2. #2
    Chris L. Guest

    Re: STILL Can't make buttons WORK!!


    Well what you're wanting to do is all very basic stuff. If you want to mess
    with using the newer swing classes look up JButton, JLabel, JTextfield, contentPane,
    JTextArea, JScrollPane in the documentation. Normal classes lookup: Label,
    TextField, TextArea, Button. Labels are of course good for output and Textfields
    are good for input. The textarea is essentially just a listbox. (Like in
    VB or web form)

    So in your main code if you declared & assigned the textfield and label like
    so:
    //outsite of void init
    private static Label outputLabel;
    private static TextField termYrs;
    //in specific class

    outputLabel = new Label("This is output to label")
    add(outputLabel);
    termYrs = new TextField(12);
    add(termYrs);

    Then you can input or output to the fields or labels in your button event.
    (Keep in mind that 'p' and 'termYrs' need to be private static variables
    (or global).
    So this for example for the //button code here:

    double numYears;
    String numYearsString;
    numYears = Double.parseDouble(termYrs.getText()); //for double
    numYearsString = termYrs.getText(); //for String

    outputLabel.setText("You typed " & numYearsString) //output

    That's a brief overview, if you really want to learn how to use these well,
    look up the classes listed above in the java doc.


    Hope this helps,
    Chris L.


    "superwebmonkey" <superwebmonkey@hotmail.com> wrote:
    >
    >Yeah, I can find this kind of info. I can find classes, and how to make

    instantiate,
    >and all that...but just now how to make it DO SOMETHING.
    >
    >
    > b.addActionListener(new ActionListener()
    > { public void actionPerformed(ActionEvent evt)
    > {
    > //button code here
    > }
    > });
    >
    >Where this code says button code here...that's what I need.
    >I know, it depends on what you want it to do.....I just want it to do ANYTHING.
    > If I can get the button to do anything, I can figure out how to make it
    >do more complex things eventually...but all I can find are complex actions.
    >
    >I just want, "If button is pushed change the text."
    >OR"If button is pushed replace the button."
    >
    >I appreciate all of the help!!!



  3. #3
    Brent Guest

    Re: STILL Can't make buttons WORK!!


    Hi-

    Maybe just a simple example will help-

    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;

    public class MyApplet extends Applet implements ActionListener{
    Button myButton;
    boolean myFlag = false;

    public void init(){
    myButton = new Button("Click Me");
    myButton.addActionListener(this);
    add(myButton);
    }

    public void actionPerformed(ActionEvent e){
    if(e.getSource() == myButton){
    myFlag = true;
    repaint();
    }
    }

    public void paint(Graphics g){
    if(myFlag)
    g.drawString("You did it!", 30,50);

    }
    }


    Of course this is just your basic AWT kind of applet. It doesn't use Swing
    so you don't have mess with layout
    managers and all that just to practice what you're doing. Maybe see if you
    can get ahold of a book called
    "Java 1.2 in Record time" by Steven Holzner. He's pretty much goes through
    the basic GUI skills and it's
    quick and easy to read. True, inner classes are the way to go because ActionListener
    (and all the other listening
    classes) are actually interfaces. An interface requires you to implement
    all of it's methods and there are a lot
    of "adapters" that you can use in inner classes which will save you a ton
    of typing. However, this is just a simple
    example so you can see what's going on.

    Hope it's helpful,
    Brent



    "superwebmonkey" <superwebmonkey@hotmail.com> wrote:
    >
    >Yeah, I can find this kind of info. I can find classes, and how to make

    instantiate,
    >and all that...but just now how to make it DO SOMETHING.
    >
    >
    > b.addActionListener(new ActionListener()
    > { public void actionPerformed(ActionEvent evt)
    > {
    > //button code here
    > }
    > });
    >
    >Where this code says button code here...that's what I need.
    >I know, it depends on what you want it to do.....I just want it to do ANYTHING.
    > If I can get the button to do anything, I can figure out how to make it
    >do more complex things eventually...but all I can find are complex actions.
    >
    >I just want, "If button is pushed change the text."
    >OR"If button is pushed replace the button."
    >
    >I appreciate all of the help!!!



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center