| | |
button help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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....
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)
<span class="ad_notxt"><code class="inlinecode"> 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 </code></span>
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....
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- Button to open a new browser window (ASP.NET)
- The name for the Resize Button to ad to Script (HTML and CSS)
- monitor power button not responding (Monitors, Displays and Video Cards)
- how can I change start page & recover the general page button to change (Web Browsers)
- [Tweak] Renaming your Start Button (Windows tips 'n' tweaks)
- Random Restarts (Windows NT / 2000 / XP)
- help please cannot use search button on ie6 (Web Browsers)
Other Threads in the Java Forum
- Previous Thread: how do i set background as an image with Jframe? ... and other stuff
- Next Thread: Asterisks Pattern Help!
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arc arguments array arrays automation binary bluetooth c++ chat class classes client code codesnippet component csv database doctype draw ebook eclipse error event exception fractal freeze game givemetehcodez graphics gui html ide image input integer intellij iphone j2me java java.xls javaprojects jmf jni jpanel julia linux list loop loops mac map method methods mobile netbeans newbie number online oracle page parameter plazmic print problem program programming project recursion reporting rotatetext scanner screen sell server set size sms socket sort sourcelabs sql string superclass swing system template test testautomation threads time title tree tutorial-sample windows working






