0
public class Blackjack extends Applet implements ActionListener {

//Setting Up Global Variables
Button play = new Button("Play");
Button manual = new Button("User Manual");
Boolean First;
Boolean Manual;


public void init(){
//Initializing Global Variables 
    First=true;
    Manual = false;
    play.setBounds(150, 400, 50, 25);
    manual.setBounds(250, 400, 110, 25);

//Initializing Window
    setSize(new Dimension(500,500));
    setBackground(Color.WHITE);
    setLayout(null);

}

public void paint (Graphics g){
    //Wipe Past Graphics
    g.clearRect(0, 0, 500, 500);

    //Game Start
    if(First){

    //Display Opening Graphics
        if(!Manual){
        add(play);
        play.addActionListener(this);
        add(manual);
        manual.addActionListener(this);
        setLayout(null);
        g.drawImage(getImage(getDocumentBase(), "Welcome.png"), 40, 20, this);
        }

    //Display User Manual
        else {
            Button close = new Button("Close");
            close.setBounds(10, 10, 40, 15);
            close.addActionListener(this);

            remove(play);
            remove(manual);
            setLayout(null);                
        }

    }

    }

@Override
public void actionPerformed(ActionEvent e) {

//Find which button was pressed.
    if (e.getSource() == play) {
        First=false;
        repaint();
    }
    else if (e.getSource() == manual) {}
    }
}

I know this code is really brutish, but I've spend the last 3 hours looking over Button questions here and my teacher can't help (she tried and failed to find the problem).

The issue is that when I run the code the buttons show up, but can't be clicked. It isn't that they don't respond when clicked, they just won't interact with the mouse at all (using tab/space also doesn't highlight them as one would expect it to). The strange thing is that this worked earlier today, but then stopped working after I temporarily edited out the ActionListener Override (even though Immediately put it back in. My teacher is forcing us to use Applet, so changing formats isn't an option. I'm really stuck. Thanks for any help.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Why code an applet? If it is due to the teacher specifying it, please refer them to .. – Andrew Thompson May 09 '17 at 19:19
  • [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 3) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 4) **Don't add action listeners in the `paint(Graphics)` method! The programmer does not control when, or how often, it is called!** – Andrew Thompson May 09 '17 at 19:21

0 Answers0