hey..im new to JAVA...i wanna use an Icon for a JButton..i did it already but unable to do so in the project that im making....

the back.gif is in the same folder as the .java file...it compiles well...

just shows a blank white icon....

can you please find the problem in this? ? ?

package Prj;


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class aboutme extends JFrame implements ActionListener
{
JMenuBar mbr;
JMenu file;
JMenuItem exit;
JPanel p1,p2;
JLabel l1,l2,l3,l4,l5,l6,l7,l8;
JButton b1,b2;
//Color c1,c2;
Font f1,f2,f3;


public aboutme()
{
setVisible(true);
setSize(600,600);

mbr=new JMenuBar();
setJMenuBar(mbr);
file=new JMenu("File");
exit=new JMenuItem("exit");
p1=new JPanel();
p2=new JPanel();

l1=new JLabel("About Me");
l2=new JLabel("Online Testing! ! !");
l3=new JLabel("Project made for conducting Online tests");
l4=new JLabel("Coding and Designing by:-");
l5=new JLabel("Ashish Rai (SG 8302)");
l6=new JLabel("Karanbir Singh (SG 8321)");
l7=new JLabel("Nippon Sahore (SG 8334)");
l8=new JLabel("Varun Joshi (SG 8353)");

ImageIcon back=new ImageIcon("back.gif");
b1=new JButton("Exit");
b2=new JButton("Back");
//c1=new Color(123,72,91);
//c2=new Color(64,128,200);
f1=new Font("arial",Font.BOLD,22);
f2=new Font("arial",Font.BOLD|Font.ITALIC,55);
f3=new Font("arial",Font.BOLD,18);
b1.addActionListener(this);
b2.addActionListener(this);
exit.addActionListener(this);

    p1.setLayout(new BorderLayout());
    l1.setBounds(50,50,250,250);
    l2.setBounds(50,50,2500,75);
    l3.setBounds(50,100,2500,250);
    l4.setBounds(50,150,250,250);
    l5.setBounds(50,200,250,250);
    l6.setBounds(50,250,250,250);
    l7.setBounds(50,300,250,250);
    l8.setBounds(50,350,250,250);
    b1.setBounds(400,450,100,50);
    b2.setBounds(500,450,100,50);


    //b1.setBackground(Color.blue);
    //b2.setBackground(Color.blue);
    b1.setFont(f3);
    b2.setFont(f3);

    p2.setLayout(null);
    p2.add(l2);
    p2.add(l3);
    p2.add(l4);
    p2.add(l5);
    p2.add(l6);
    p2.add(l7);
    p2.add(l8);




    p2.add(b1);
    p2.add(b2);

    mbr.add(file);
    file.add(exit);

    //p2.setBackground(c2);

    p1.add(l1,BorderLayout.NORTH);
    p1.add(p2,BorderLayout.CENTER);
    //p1.setBackground(c1);
    //l1.setForeground(Color.red);
    l1.setFont(f1);
    l2.setFont(f2);
    l3.setFont(f3);
    l4.setFont(f3);
    l5.setFont(f3);
    l6.setFont(f3);
    l7.setFont(f3);
    l8.setFont(f3);
    add(p1);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
System.exit(0);
}
});


}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
System.exit(0);
}
else if(ae.getSource()==exit)
{
System.exit(1);
}
else if(ae.getSource()==b2)
{
Login o2=new Login();
o2.setSize(300,200);
setVisible(false);


}
}


public static void main(String x[])
{
new aboutme();


}



}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

The constructor as indicated in API:
JButton(String text, Icon icon)
创建一个带初始文本和图标的按钮。
There fore your code line:
b2=new JButton("Back"); // b2 is a JButton with ImageIcon?
should be replaced with
b2=new JButton("Back",back);// back if the instance of ImageIcon

The constructor as indicated in API:
JButton(String text, Icon icon)
Create a JButton with an Icon
Therefore your code line:
b2=new JButton("Back"); // b2 is a JButton with ImageIcon
should be replaced with
b2=new JButton("Back",back);// back if the instance of ImageIcon

Edited 6 Years Ago by tong1: n/a

hey i tried but its not working......

i just want the icon to show up and not the text.....also,strange thing is,when i run it separately,ie when im NOT running it in a package,it runs fine....

also,while speicifying the complete location of the icon
ImageIcon back=new ImageIcon("C:\ActPrj\Prj\back.gif");
i get two errors at compile time....Illegal escape character pointing to '\'

really confusing.....

Edited 6 Years Ago by neo_31591: n/a

If you want the icon only. The code will be:
b2=new JButton(back);
I have made a back.gif and modified your code:
e.g. insert
Container container=getContentPane();
.....
container.add(p1);
.....
Attached please find the code modified and the image file I made. Hope it helps.
I did not add (p2). I delete the lines for login since I have no definition for it. It works

Attachments
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class aboutme extends JFrame implements ActionListener
{
JMenuBar mbr;
JMenu file;
JMenuItem exit;
JPanel p1,p2;
JLabel l1,l2,l3,l4,l5,l6,l7,l8;
JButton b1,b2;
//Color c1,c2;
Font f1,f2,f3;


public aboutme()
{
	Container container=getContentPane();
setVisible(true);
setSize(600,600);

mbr=new JMenuBar();
setJMenuBar(mbr);
file=new JMenu("File");
exit=new JMenuItem("exit");
p1=new JPanel();
p2=new JPanel();

l1=new JLabel("About Me");
l2=new JLabel("Online Testing! ! !");
l3=new JLabel("Project made for conducting Online tests");
l4=new JLabel("Coding and Designing by:-");
l5=new JLabel("Ashish Rai (SG 8302)");
l6=new JLabel("Karanbir Singh (SG 8321)");
l7=new JLabel("Nippon Sahore (SG 8334)");
l8=new JLabel("Varun Joshi (SG 8353)");

ImageIcon back=new ImageIcon("back.gif");
b1=new JButton("Exit");
b2=new JButton(back);
//c1=new Color(123,72,91);
//c2=new Color(64,128,200);
f1=new Font("arial",Font.BOLD,22);
f2=new Font("arial",Font.BOLD|Font.ITALIC,55);
f3=new Font("arial",Font.BOLD,18);
b1.addActionListener(this);
b2.addActionListener(this);
exit.addActionListener(this);

p1.setLayout(new BorderLayout());
l1.setBounds(50,50,250,250);
l2.setBounds(50,50,2500,75);
l3.setBounds(50,100,2500,250);
l4.setBounds(50,150,250,250);
l5.setBounds(50,200,250,250);
l6.setBounds(50,250,250,250);
l7.setBounds(50,300,250,250);
l8.setBounds(50,350,250,250);
b1.setBounds(400,450,100,50);
b2.setBounds(500,450,100,50);


//b1.setBackground(Color.blue);
//b2.setBackground(Color.blue);
b1.setFont(f3);
b2.setFont(f3);

p2.setLayout(null);
p2.add(l2);
p2.add(l3);
p2.add(l4);
p2.add(l5);
p2.add(l6);
p2.add(l7);
p2.add(l8);




p2.add(b1);
p2.add(b2);

mbr.add(file);
file.add(exit);

//p2.setBackground(c2);

p1.add(l1,BorderLayout.NORTH);
p1.add(p2,BorderLayout.CENTER);

//p1.setBackground(c1);
//l1.setForeground(Color.red);
l1.setFont(f1);
l2.setFont(f2);
l3.setFont(f3);
l4.setFont(f3);
l5.setFont(f3);
l6.setFont(f3);
l7.setFont(f3);
l8.setFont(f3);
container.add(p1);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
System.exit(0);
}
});


}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
System.exit(0);
}
else if(ae.getSource()==exit)
{
System.exit(1);
}
else if(ae.getSource()==b2)
{
//Login o2=new Login();
//o2.setSize(300,200);
//setVisible(false);


}
}


public static void main(String x[])
{
new aboutme();


}



}
back.gif 2 KB
This article has been dead for over six months. Start a new discussion instead.