I am using VS Code and my folder contains one Java file and one image only.
I was trying to set image in JLabel, so first I access the image by:
ImageIcon img = new ImageIcon(getClass().getResource("img_flag.png"));
Then I set this to JLabel but nothing appears on my screen.
JLabel label = new JLabel(img);
To confirm that I am accessing the correct image, I set it to the logo of the window and the logo was showing properly.
setIconImage(img.getImage());
I can't figure out why label can't show the image.
Here is my full code:
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Mylable extends JFrame{
Mylable(){
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
ImageIcon img = new ImageIcon(getClass().getResource("img_flag.png"));
setIconImage(img.getImage()); //working
JLabel label = new JLabel(img); //not working
add(label);
}
public static void main(String[] args) {
new Mylable();
}
}
Related
I'm fairly new to coding and was trying to make a simple GUI with JAVA Swing. I wanted to place an image in a label but for some reason does the image does not show when I run the Code. Are there some requirements I don't know of or did I do something wrong?
package Part2Lables;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
//Label = a GUI display area for a string of text, an image, or both
ImageIcon image = new ImageIcon("Kakashi.png");
JLabel label = new JLabel(); // create a label
label.setText("This is text in a label"); // set text to the label
label.setIcon(image);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
frame.add(label);
}
}
This is the code I used (I did use imports but they are not visible in the pic.)
I am having an issue with getting my image to display within the GUI window. The GUI initiates fine but no image is displayed window displays no image. I have tired various methods with no luck and I'm not sure where I'm going wrong. Any guidance would be greatly appreciated
package banksystem;
import javax.swing.*;
import java.awt.*;
public class Login extends JFrame {
Login(){
setTitle("Cash Machine");
ImageIcon img1 = new ImageIcon(ClassLoader.getSystemResource("bank-icon.jpg"));
Image img2 = img1.getImage().getScaledInstance(100,100, Image.SCALE_DEFAULT);
ImageIcon img3 = new ImageIcon(img2);
JLabel label = new JLabel(img3);
add(label);
setSize(800, 480); //Sets frame size
setVisible(true); //Frame will display
setLocation(350, 200); //Sets frame location
}
public static void main(String[] args) {
new Login();
}
}
folder setup within intellij
ImageIcon img1 = new ImageIcon(ClassLoader.getSystemResource("/images/bank-icon.jpg"));
Also, move resources directory outside of the sources root.
I am trying to display an image icon PNG, I have properly referred to the file called "logo.png" inside the project folder and set the icon as seen below. I'm not sure where I went wrong as I followed this tutorial starting around minute 14 (https://www.youtube.com/watch?v=Kmgo00avvEw&t=298s), I have made sure the code was correct but I'm still unable to get the image to appear beside the text when I run the code in Eclipse.
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class mainClass {
public static void main(String[] args) {
// JFrame is a GUI frame to add components to
//JLabel a GUI display area for a string of texts and image.
ImageIcon image = new ImageIcon("logo.png");
JLabel label = new JLabel(); //creates JLabel
label.setText("Welcome to Magic Shape"); //sets text for label
label.setIcon(image);
JFrame frame = new JFrame(); //creates JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close operations when...
frame.setSize(500, 500); //sets size of JFrame (can resize)
frame.setTitle("Magic Shape"); //title of Program
frame.setVisible(true); //makes frame visible
frame.add(label); //adds label
}
}
package be.project.jframe;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.IOException;
import java.io.InputStream;
public class Main {
public static void main(String[] args) throws IOException {
// JFrame is a GUI frame to add components to
//JLabel a GUI display area for a string of texts and image.
InputStream stream = Main.class.getResourceAsStream("/be/project/jframe/icons/400.png");
ImageIcon icon = new ImageIcon(ImageIO.read(stream));
JLabel jLabelObject = new JLabel("Welcome to Magic Shape");
jLabelObject.setIcon(icon);
JFrame frame = new JFrame(); //creates JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close operations when...
frame.setSize(800,600);
frame.setTitle("Magic Shape"); //title of Program
frame.setVisible(true); //makes frame visible
frame.add(jLabelObject); //adds label
frame.pack();
}
}
According to your code, Java searches for the file logo.png in the working directory. The working directory is the value returned by System.getProperty("user.dir"). If Java does not find the file, it essentially creates a null icon and that's why you aren't seeing the icon.
Just make sure you put the file in the correct directory.
Refer to the following.
Accessing Resources
Retrieving Resources
How to Use Icons
Make sure the png file is placed at current working directory:
System.out.println(System.getProperty("user.dir"));
I'm trying to find a way to replace all the contents of a JDialog to simply an image.
It's for the about page of a project I'm working on and I want when the user clicks on the About section, an image to popup in the style of a JDialog(and to disappear when focus is lost).
Example: http://www.tecmint.com/wp-content/uploads/2012/08/About-Skype.jpg
There Skype displays only an image they've created as their "About" page.
How can I make an "image dialog" in Java(swing)?
How can I make an "image dialog" in Java(swing)?
Use an undecorated JDialog with a JLabel containing an ImageIcon:
JDialog dialog = new JDialog();
dialog.setUndecorated(true);
JLabel label = new JLabel( new ImageIcon(...) );
dialog.add( label );
dialog.pack();
dialog.setVisible(true);
BufferedImage image = ImageIO.read(new File("myfile.png"));
JLabel picLabel = new JLabel(new ImageIcon(image));
JOptionPane.showMessageDialog(null, picLabel, "About", JOptionPane.PLAIN_MESSAGE, null);
Here you go, I have annotated the code for you
import javax.swing.JOptionPane; //imports
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import java.awt.Toolkit;
import java.awt.Dimension;
public class img{
public static void main(String[] args){
JFrame f = new JFrame(); //creates jframe f
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //this is your screen size
f.setUndecorated(true); //removes the surrounding border
ImageIcon image = new ImageIcon(diceGame.class.getResource("image.png")); //imports the image
JLabel lbl = new JLabel(image); //puts the image into a jlabel
f.getContentPane().add(lbl); //puts label inside the jframe
f.setSize(image.getIconWidth(), image.getIconHeight()); //gets h and w of image and sets jframe to the size
int x = (screenSize.width - f.getSize().width)/2; //These two lines are the dimensions
int y = (screenSize.height - f.getSize().height)/2;//of the center of the screen
f.setLocation(x, y); //sets the location of the jframe
f.setVisible(true); //makes the jframe visible
}
}
[[OLD]]
The code below will do what you're looking for.
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
public class img{
public static void main(String[] args){
JLabel lbl = new JLabel(new ImageIcon(diceGame.class.getResource("image.png")));
JOptionPane.showMessageDialog(null, lbl, "ImageDialog",
JOptionPane.PLAIN_MESSAGE, null);
}
}
I'm trying to add an image to one frame but it seems it does not working. The image created by an ImageIcon from the specified file. The image file is in the seam directory the java file exist.
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class image {
public static void main(String args[])
{
TimeFrame frame = new TimeFrame();
}
}
class TimeFrame extends JFrame
{
//Image icon = Toolkit.getDefaultToolkit().getImage("me.jpg");
ImageIcon icon = new ImageIcon("me.jpg");
JLabel label = new JLabel(icon);
public TimeFrame(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("My Frame");
setSize(500,400);
//this.setIconImage(icon);
add(label,BorderLayout.CENTER);
setVisible(true);
}
}
If your icon is beside the TimeFrame java file, you should use
java.net.URL imgUrl = getClass().getResource("me.jpg");
ImageIcon icon = new ImageIcon(imgUrl);
or
java.net.URL imgUrl = TimeFrame.class.getResource("me.jpg");
ImageIcon icon = new ImageIcon(imgUrl);
You are (probably) currently looking for it in your working directory which you can output via
System.out.println(System.getProperty("user.dir"));
Will u try this one?
ImageIcon ImageIcon = new ImageIcon("me.jpg");
Image Image = ImageIcon.getImage();
this.setIconImage(Image);
Simply change the directory to "src/me.jpg"