button help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2007
Posts: 19
Reputation: talablink is an unknown quantity at this point 
Solved Threads: 1
talablink's Avatar
talablink talablink is offline Offline
Newbie Poster

button help

 
0
  #1
Mar 12th, 2007
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....

  1. <span class="ad_notxt"><code class="inlinecode">
  2.  
  3.  
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import javax.swing.*;
  7. import javax.swing.event.*;
  8.  
  9.  
  10. class Button{
  11.  
  12. public static void main(String[] args) {
  13. JFrame window = new JFrame("Sleepy");
  14. window.getContentPane().add(new SleepyPanel());
  15. window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. window.pack();
  17. window.show();
  18. }
  19. }
  20. class SleepyPanel extends JPanel {
  21. private Sleeper face = new Sleeper();
  22.  
  23. SleepyPanel() {
  24. //--- Create some buttons
  25. Button awakeButton = new Button("Awake");
  26. awakeButton.addActionListener(
  27. new ActionListener() {
  28.  
  29. public void actionPerformed(ActionEvent e) {
  30. face.setAwake(true);
  31. }
  32.  
  33. }
  34. );
  35. JButton asleepButton = new JButton("Asleep");
  36. asleepButton.addActionListener(
  37. new ActionListener() {
  38. public void actionPerformed(ActionEvent e) {
  39. face.setAwake(false);
  40. }
  41.  
  42. }
  43. );
  44. JButton evilButton= new JButton("Evil");
  45. evilButton.addActionListener(
  46. new ActionListener()
  47.  
  48. {
  49. public void actionPerformed(ActionEvent e){
  50. face.setAwake(false);
  51. }
  52. }
  53. );
  54. JPanel buttonPanel = new JPanel();
  55. buttonPanel.setLayout(new FlowLayout());
  56. buttonPanel.add(awakeButton);
  57. buttonPanel.add(asleepButton);
  58. buttonPanel.add(evilButton);
  59.  
  60. this.setLayout(new BorderLayout());
  61. this.add(buttonPanel, BorderLayout.NORTH);
  62. this.add(face, BorderLayout.CENTER);
  63. }
  64.  
  65. }
  66.  
  67. class Sleeper extends JPanel {
  68.  
  69.  
  70.  
  71. private boolean awake = true;
  72. private boolean evil = true;
  73. private boolean asleep = true;
  74.  
  75. Sleeper() {
  76. this.setBackground(Color.lightGray);
  77. this.setPreferredSize(new Dimension(500, 500));
  78. }
  79.  
  80.  
  81. public void setAwake(boolean awakeAsleep) {
  82. awake = awakeAsleep;
  83. repaint();
  84. }
  85.  
  86. public void setEvil(boolean asleepEvil){
  87. evil = asleepEvil;
  88. repaint();
  89. }
  90. public void setAsleep(boolean asleepAwake){
  91. asleep = asleepAwake;
  92. repaint();
  93. }
  94.  
  95. public void paintComponent(Graphics g) {
  96. super.paintComponent(g); // MUST be first line
  97.  
  98. //--- draw head
  99. g.setColor(Color.yellow);
  100. g.fillOval(50, 50, 380, 380);
  101.  
  102. //--- draw eyes*/
  103. g.setColor(Color.black);
  104. if (awake) {
  105. g.fillOval(180 , 100, 50, 70); // left eye
  106. g.fillOval(279, 100, 50, 70); // right eye
  107. }
  108. else if(evil){
  109. g.setColor(Color.red);
  110. g.fillOval(180 , 100, 50, 70);
  111. g.fillOval(279, 100, 50, 70);
  112. }
  113. else if(asleep){
  114. g.fillRect(160 , 100, 90, 44); // left eye
  115. g.fillRect(259, 100, 90, 44); // left eye
  116. }
  117. else{
  118. g.fillRect(0 , 450, 500, 50);
  119.  
  120. }
  121. }//end paintComponent
  122.  
  123. }//endclass Sleeper
  124. </code></span>

Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,241
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 491
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: button help

 
0
  #2
Mar 12th, 2007
You are mixing AWT with SWING, that is the problem
Originally Posted by talablink View Post
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....

        JButton awakeButton = new JButton("Awake");
        awakeButton.addActionListener(
            new ActionListener() {

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

            }
        );
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC