0

I've tried many things to get this button bb or continue to output "Hey Buddy", yet it still does not work. It is displayed yet when i press it nothing happens. The code uses both java swing a awt.

package Game;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.util.concurrent.TimeUnit;

import javax.swing.JButton;

public class base extends java.applet.Applet implements ActionListener, TextListener {

    //Graphics
    //Graphics

    @SuppressWarnings("deprecation")
    public static JButton bb = new JButton("Continue");
    public TextArea ta = new TextArea(30, 140);
    TextArea tb = new TextArea(3, 130);
    public int counter = 0;

    //main class

    public static void main(String[] args) {
        Frame f = new Frame("---Quest---");
        base ex = new base();
        ex.init();
        f.add("Center", ex);
        f.pack();
        f.show(true);
        bb.addActionListener(ex);
    }

    public void actionPerformed1(ActionEvent Continue) {
        bb.addActionListener(this);
        counter++;
        if (Continue.getSource() == bb && counter == 1) {
            tb.append("Hey Buddy");
        }
    }

    //graphics

    public void init() {
        bb.addActionListener(this);
        Panel p;
        setLayout(new BorderLayout());
        p = new Panel();
        ta.append("Hey");
        bb.addActionListener(this);
        p.add(bb);
        p.add(ta);
        p.add(tb);
        p.setBackground(Color.blue);
        ta.setBackground(Color.cyan);
        ta.setEditable(false);
        add("Center", p);

        p.setVisible(true);
    }

    //time class
    public static int nap(int time) {
        try {
            TimeUnit.SECONDS.sleep(time);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;
    }

//end of code       
    @Override
    public void textValueChanged(TextEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
}
5
  • 1
    Please be precise. What is your question? Or is it a coding/debugging request ? Commented Jun 29, 2015 at 20:45
  • Debugging. Does it need to be posted elsewhere, sorry i'm new to Stack Over Flow. Commented Jun 29, 2015 at 20:47
  • 2
    For better help sooner, post an MCVE (Minimal Complete Verifiable Example) or SSCCE (Short, Self Contained, Correct Example). Welcome to StackOverflow. Please read: How do I ask a good question? Commented Jun 29, 2015 at 20:51
  • Thanks, When i do narrow it down into a simpler program its works so i feel as if,something is just not working with the multiple methods.. Commented Jun 29, 2015 at 20:55
  • Any reason why rye using AWT based components? They were superseded by Swing over 15 years ago Commented Jun 29, 2015 at 21:46

1 Answer 1

2

The whole code is buggy.Read the Comments inline.

1.Idk why you are adding actionListener to the button 4 times(Keep one)

2.You have to have to change the actionPerformed1 to actionPerfromed as you are implementing the ActionListener and assigning this to button's ActionListener

public TextArea ta = new TextArea(30, 140);
TextArea tb = new TextArea(3, 130);
public int counter = 0;

//main class

public static void main(String[] args) {

    f.show(true);//show is deprecated use setVisible(true) instead;
    bb.addActionListener(ex);//1
}

public void actionPerformed1(ActionEvent Continue) {//have to change the actionPerformed1 to actionPerfromed
    bb.addActionListener(this);//2 What is this assigning inside actionPerformed Need to be removed
    counter++;
    if (Continue.getSource() == bb && counter == 1) {
        tb.append("Hey Buddy");
    }
}

//graphics

public void init() {
    bb.addActionListener(this);//3
    Panel p;
    setLayout(new BorderLayout());
    p = new Panel();
    ta.append("Hey");
    bb.addActionListener(this);//4
    p.add(bb);

    p.setVisible(true);//already called a show for JFrame why you want to set Visible of Panel
}
Sign up to request clarification or add additional context in comments.

6 Comments

It's an applet, either they need to remove the main method or remove the extends Applet
You're second point then have the op with two actionPerformed methods, just so you know when they say it won't compile ;)
Sorry, forget to mention, you're on the right track ;)
Thanks I got it to work by renaming actionPreformed1 to action preformed. I think that was a typing error on my part. :) I did all of Madhan's changes accept changing f,show to setvisable, I can not get that too work. Can anyone expand on what MadProgrammer was saying... can i not have a main method in a applet?
@MadProgrammer Sorry, forget to mention, you're on the right track ;) I couldn't parse what you were implying
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.