2013年9月4日星期三

Neighborhoods : java ImageIcon not show up

With eclipse write the following code:
package logframe;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class log extends JFrame implements ActionListener {

/ **
* @ param args
* /
ImageIcon bj = new ImageIcon ("images / log.jpg"); / / define the icon and use this path cut into the picture does not show up
/ / / / define the icon and cut into the picture D :/ workspace / logtest / src / images / log.jpg ( use this path can be displayed )

JLabel lbimg = new JLabel (bj); / / define labels and loading icon
JButton btnlog = new JButton (" Login " ) ;
JPanel jp = new JPanel (); / / define a panel
public log () {/ / class constructor
setTitle (" login screen " ) ;
this.setVisible (true);
this.setSize (600, 400);
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
lbimg.setBounds (0, 0, bj.getIconWidth (), bj.getIconHeight ()); / / label fill the entire panel
this.getLayeredPane (). add (lbimg, new Integer (Integer.MIN_VALUE)); / / frame the second layer to add tags
}
private void buju () {
/ / TODO Auto-generated method stub
jp = (JPanel) this.getContentPane (); / / the contents pane into a JPanel, or can not use the method setOpaque () to make the content pane transparent
jp.setOpaque (false); / / set the panel transparent , so as to display a label layer below picture
jp.setLayout (null); / / no layout

btnlog.setSize (120,36); / / button size
btnlog.setLocation (100,50); / / button position
jp.add (btnlog); / / panel Add button
}

@ Override
public void actionPerformed (ActionEvent e) {
/ / TODO Auto-generated method stub
}
public static void main (String [] args) {
/ / TODO Auto-generated method stub
log a = new log ();
a.buju ();
a.show ();
}

}

The following is the folder path is on the images under the picture .



display pictures with an absolute path is no problem , but with the relative path does not show up , and ask the great God .




------ Solution ------------------------------------ --------
user.dir relative path is relative to this directory is concerned, is when you start the Java program directory in the eclipse is the project 's root directory.

You can use the ClassLoader getResource method .
new ImageIcon (getClass (). getClassLoader (). getResource ("/ images / log.jpg"));
------ For reference only -------- -------------------------------
relative path is relative to the current path , that path means that your images folder in current similar to a folder , add a slash before the images
/ images / log.jpg
------ For reference only ---------------------------- -----------


tried, no ImageIcon bj = new ImageIcon ("/ images / log.jpg");
fact, the relative phase does not matter , that is, if I have moved the location of the entire source code can also be used , and that was it .
------ For reference only -------------------------------------- -

  
tried, no ImageIcon bj = new ImageIcon ("/ images / log.jpg");   
fact, the relative phase does not matter , that is, if I have moved the location of the entire source code can also be used , and that was it .  

plus two points in front of it
ImageIcon bj = new ImageIcon (".. / images / log.jpg");
------ For reference only -------- -------------------------------

    
tried, no ImageIcon bj = new ImageIcon ("/ images / log.jpg");     
fact, the relative phase does not matter , that is, if I have moved the location of the entire source code can also be used , and that was it .    
  
  
plus two points in front of it   
ImageIcon bj = new ImageIcon (".. / images / log.jpg");  


not oh
ImageIcon bj = new ImageIcon (".. / images / log.jpg");
------ For reference only ---------- -----------------------------

      
tried, no ImageIcon bj = new ImageIcon ("/ images / log.jpg");       
fact, the relative phase does not matter , that is, if I have moved the location of the entire source code can also be used , and that was it .      
    
    
plus two points in front of it     
ImageIcon bj = new ImageIcon (".. / images / log.jpg");    
  
  
  
not oh   
ImageIcon bj = new ImageIcon (".. / images / log.jpg");  

------ For reference only ---------------------------------------


the first " / " removed, read:
ImageIcon bj = new ImageIcon (getClass (). getClassLoader (). getResource ("images / log.jpg"));

run , OK!

没有评论:

发表评论