Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help Jbuttons wont show... :'( Posted by rentaw02 on 27 Mar 2011 at 8:45 AM
No matter what i do...the Jbuttons wont show...help pls... :'(
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class sampleimage extends JApplet implements ActionListener{

JButton b1;
JButton b2;
JButton b3;



 boolean home=false;
 boolean start=false;
 boolean credits=false;

 boolean a = false;
 boolean b = false;
 boolean c = false;

 Image welcome;
 MediaTracker mt;


public void init(){


 mt=new MediaTracker(this);

 welcome=this.getImage(this.getCodeBase(),"welcome.gif");
 



 b1= new JButton("START");
 b1.setActionCommand("b1");
 
 b2= new JButton("CREDITS");
 b2.setActionCommand("b2");

 b3 = new JButton("BACK");
 b3.setActionCommand("b3");

 Container c= getContentPane();
 c.setLayout(null);
 c.add(b1);
 c.add(b2);
 c.add(b3);
 b1.setBounds(150,100,75,50);
 b1.setBounds(150,200,75,50);
b1.setBounds(150,300,75,50);

 b1.addActionListener(this);
 b2.addActionListener(this);
 b3.addActionListener(this);


}   //Init end

 public void actionPerformed(ActionEvent ae){

 String x = ae.getActionCommand();
 if(x.equals("b1")) {
 start = true;
 credits = false;
 home = false;
 b1.setVisible(false);
 b2.setVisible(false);
 b3.setVisible(true);
 }
 if(x.equals("b2")) {
 start = false;
 credits = true;
 home = false;
b1.setVisible(false);
 b2.setVisible(false);
 b3.setVisible(true);
 }
 if(x.equals("b3")) {
 start = false;
 credits = false;
 home = true;
 b1.setVisible(true);
 b2.setVisible(true);
 b3.setVisible(false);
 }
     repaint();
}

public void paint(Graphics g){



 if(home){
  g.drawImage(welcome,85,50,this);
  
}
  if(start){


  Color car = new Color(244,244,244);
  g.setColor(car);
  g.fillRect(0,0,500,500);
  g.clearRect(0,0,500,500);
}
  if(credits){


  Color car = new Color(244,244,244);
  g.setColor(car);
  g.fillRect(0,0,500,500);
  g.clearRect(0,0,500,500); 
  }
}
}




Report
Re: Help Jbuttons wont show... :'( Posted by silveredge52 on 28 Mar 2011 at 7:06 PM
Hey,
don't have good explanation, and haven't worked with applets much, but added a
cont.paintComponents(g);
and got the buttons to show. Program was modified slightly
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class sampleimage extends JApplet implements ActionListener {

	JButton b1;
	JButton b2;
	JButton b3;

	Container cont;

	boolean home = false;
	boolean start = false;
	boolean credits = false;

	boolean a = false;
	boolean b = false;
	boolean c = false;

	Image welcome;
	MediaTracker mt;

	public void init() {

		mt = new MediaTracker(this);

		// System.out.println(this.getCodeBase());

		welcome = this.getImage(this.getCodeBase(), "Penguins.jpg");

		b1 = new JButton("START");
		b1.setActionCommand("b1");

		b2 = new JButton("CREDITS");
		b2.setActionCommand("b2");

		b3 = new JButton("BACK");
		b3.setActionCommand("b3");

		cont = getContentPane();
		cont.setLayout(null);
		cont.add(b1);
		b1.setVisible(true);
		cont.add(b2);
		cont.add(b3);
		
		cont.setVisible(true);

		b1.setBounds(150, 100, 75, 50);
		b2.setBounds(150, 200, 75, 50);
		b3.setBounds(150, 300, 75, 50);

		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);

	} // Init end

	public boolean isOptimizedDrawingEnabled() {
		return false;
	}

	public void actionPerformed(ActionEvent ae) {

		String x = ae.getActionCommand();
		if (x.equals("b1")) {
			start = true;
			credits = false;
			home = false;
			b1.setVisible(false);
			b2.setVisible(false);
			b3.setVisible(true);
		}
		if (x.equals("b2")) {
			start = false;
			credits = true;
			home = false;
			b1.setVisible(false);
			b2.setVisible(false);
			b3.setVisible(true);
		}
		if (x.equals("b3")) {
			start = false;
			credits = false;
			home = true;
			b1.setVisible(true);
			b2.setVisible(true);
			b3.setVisible(false);
		}
		repaint();
	}

	public void paint(Graphics g) {
		
		home = true;

		if (home) {
			
			// g.drawImage(welcome, 85, 50, this);
			g.drawImage(welcome, 300, 100, this);
						
		}
		if (start) {

			Color car = new Color(244, 244, 244);
			g.setColor(car);
			g.fillRect(0, 0, 500, 500);
			g.clearRect(0, 0, 500, 500);
		}
		if (credits) {

			Color car = new Color(244, 244, 244);
			g.setColor(car);
			g.fillRect(0, 0, 500, 500);
			g.clearRect(0, 0, 500, 500);
		}
		cont.paintComponents(g);
	}
}

Regards, se52



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.