i don't know how to add buttons to the code , it already has two buttons on it but i don't know how to add more...i just editted this one

i've tried adding one more but it just won't work..it still
does two things..

here's the code if you can fix it please do....

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;


class Button{

    public static void main(String[] args) {
        JFrame window = new JFrame("Sleepy");
        window.getContentPane().add(new SleepyPanel());
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.pack();
        window.show();
    }
}
class SleepyPanel extends JPanel {
    private Sleeper face = new Sleeper();

    SleepyPanel() {
        //--- Create some buttons
        Button awakeButton = new Button("Awake");
        awakeButton.addActionListener(
            new ActionListener() {

              public void actionPerformed(ActionEvent e) {
                    face.setAwake(true);
                }

            }
        );
        JButton asleepButton = new JButton("Asleep");
        asleepButton.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    face.setAwake(false);
                }

            }
        );
        JButton evilButton= new JButton("Evil");
        evilButton.addActionListener(
            new ActionListener()

            {
                public void actionPerformed(ActionEvent e){
                    face.setAwake(false);
                }
            }
            );      
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        buttonPanel.add(awakeButton);
        buttonPanel.add(asleepButton);
        buttonPanel.add(evilButton);

        this.setLayout(new BorderLayout());
        this.add(buttonPanel, BorderLayout.NORTH);
        this.add(face, BorderLayout.CENTER);
    }

}

class Sleeper extends JPanel {



    private boolean awake = true; 
    private boolean evil = true;
    private boolean asleep = true;

    Sleeper() {
        this.setBackground(Color.lightGray);
        this.setPreferredSize(new Dimension(500, 500));  
    }


    public void setAwake(boolean awakeAsleep) {
        awake = awakeAsleep; 
        repaint();           
    }

    public void setEvil(boolean asleepEvil){
        evil = asleepEvil;
        repaint();
    }
    public void setAsleep(boolean asleepAwake){
        asleep = asleepAwake;
        repaint();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);  // MUST be first line

        //--- draw head
        g.setColor(Color.yellow);
        g.fillOval(50, 50, 380, 380);

        //--- draw eyes*/
        g.setColor(Color.black);
        if (awake) {
            g.fillOval(180 , 100, 50, 70);  // left eye
            g.fillOval(279, 100, 50, 70);  // right eye
        }
        else if(evil){
            g.setColor(Color.red);
            g.fillOval(180 , 100, 50, 70);
            g.fillOval(279, 100, 50, 70);
        }
        else if(asleep){
           g.fillRect(160 , 100, 90, 44); // left eye
           g.fillRect(259, 100, 90, 44); // left eye
        }
        else{
            g.fillRect(0 , 450, 500, 50);

            }
    }//end paintComponent

}//endclass Sleeper

Edited 3 Years Ago by Dani: Formatting fixed

You are mixing AWT with SWING, that is the problem

i don't know how to add buttons to the code , it already has two buttons on it but i don't know how to add more...i just editted this one

i've tried adding one more but it just won't work..it still
does two things..

here's the code if you can fix it please do....

[B]J[/B]Button awakeButton = new [B]J[/B]Button("Awake");
        awakeButton.addActionListener(
            new ActionListener() {

              public void actionPerformed(ActionEvent e) {
                    face.setAwake(true);
                }

            }
        );
This article has been dead for over six months. Start a new discussion instead.