944,149 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1210
  • Java RSS
Mar 12th, 2007
0

button help

Expand 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....

Java Syntax (Toggle Plain Text)
  1. <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>

Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
talablink is offline Offline
21 posts
since Feb 2007
Mar 12th, 2007
0

Re: button help

You are mixing AWT with SWING, that is the problem
Click to Expand / Collapse  Quote originally posted by talablink ...
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);
                }

            }
        );
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: how do i set background as an image with Jframe? ... and other stuff
Next Thread in Java Forum Timeline: Asterisks Pattern Help!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC