Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help Jbuttons wont show... :'(

rentaw02rentaw02 Posts: 7Member
No matter what i do...the Jbuttons wont show...help pls... :'(
[code]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);
}
}
}


[/code]

Comments

  • silveredge52silveredge52 Posts: 198Member
    Hey,
    don't have good explanation, and haven't worked with applets much, but added a [code]cont.paintComponents(g);[/code] and got the buttons to show. Program was modified slightly
    [code]
    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);
    }
    }
    [/code]
    Regards, se52
Sign In or Register to comment.