ImageIcon will not display image - java

I cannot display an image. I have tried both ImageIO and ImageIcon.
This is my code:
public TestGUImain()
{
JFrame frame = new JFrame("Basic Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,600);
frame.setVisible(true);
JPanel panel = new JPanel();
panel.setLayout(null);
frame.add(panel);
JLabel lblText = new JLabel("Hello World", JLabel.CENTER);
lblText.setBounds(10, 10, 100, 200);
panel.add(lblText);
ImageIcon image = new ImageIcon(getClass().getResource("150_leading_side_prep.jpg"));
JLabel lblImage = new JLabel(image, JLabel.CENTER);
lblImage.setBounds(0, 0, 800, 600);
panel.add(lblImage);
}
P.S. if the JLabel is changed to have text in it, it will display. The image is stored in a source folder called resource at the same level as the src folder.

You need to store the image in the src folder, not at the same level as it. You also need to do frame.setVisible(true) at the end of your constructor, or use revalidate(); and repaint(); to refresh your window.

Related

Jpanel elements stuck in the center

I have added a text field to the mainPanel inside the frame, but when I try to change it's location the text field is stuck in the middle of the window.
This is my code:
`
JFrame frame = new JFrame();
JPanel mainPanel = new JPanel();
JTextField textField = new JTextField("This is a text field", 20);
mainPanel.setBorder(BorderFactory.createEmptyBorder(top, left, bottom, right));
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
mainPanel.setBackground(new Color(10, 10, 10));
mainPanel.add(textField);
textField.setLocation(10, 10);
frame.add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("CSGO translator app");
frame.pack();
frame.setVisible(true);
`
I have tried to change the layout and to use the setLocation() method, but neither of these work.
I am expecting to be able to move the text field to the top of the window.

Image not showing in a JFrame

I have just started learning java GUI and I have a lot of problems with the images. I looked through multiple topics on this site and others too, but for some reason I cannot get this to work (though I am probably making a lot of mistakes and I just don't realise it). I just want to start with showing an image on the screen. To add some information - I am using IntelliJ; the image is stored in a resource folder that I have marked as a "library root" (also, the image is pretty small - 16x16, but I have also tried with a bigger image and it doesn't help me).
import javax.swing.*;
import java.awt.*;
public class Frame {
public static final int WIDTH = 1024;
public static final int HEIGHT = 768;
public Frame()
{
JFrame frame = new JFrame();
frame.setTitle("Shady Path");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.pack();
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null);
frame.getContentPane().setBackground(Color.BLACK);
frame.setResizable(false);
//Font font = new Font(Font.MONOSPACED, Font.PLAIN, 10);
JLabel human = new JLabel(new ImageIcon(getClass().getResource("/human.jpg")));
Dimension humanDimension = new Dimension(150, 150);
human.setMinimumSize(humanDimension);
human.setPreferredSize(humanDimension);
human.setMaximumSize(humanDimension);
human.setLocation(100, 100);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.add(human);
frame.add(panel);
frame.setVisible(true);
}
}
Do not set your layout to null.

Image displaying with file directory, but not URL

Program works when I link to an image on my program (current code). when I replace that with a URL ("http://www.digitalphotoartistry.com/rose1.jpg"), the program will run without the image being displayed. I've tried a lot of variations with no luck. Can anyone see why it's not working?
public class ImageViewer extends JFrame {
public ImageViewer() {
//create panel of actions
JPanel actionPanel = new JPanel();
actionPanel.setLayout(new GridLayout(1, 4));
actionPanel.add(new JButton("Prev"));
actionPanel.add(new JButton("Add"));
actionPanel.add(new JButton("Del"));
actionPanel.add(new JButton("Next"));
//Create panel to hold pictures
JLabel label= new JLabel(new ImageIcon("C:/Users/Madison/Desktop/capture.png"), JLabel.CENTER);
JPanel imagePanel = new JPanel(new BorderLayout());
imagePanel.add(label, BorderLayout.CENTER );
//Add contents to frame
add(imagePanel, BorderLayout.NORTH);
add(actionPanel, BorderLayout.SOUTH);
}
public static void main (String args []){
ImageViewer frame = new ImageViewer();
frame.setTitle("Title");
frame.setSize(1000, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Are you editing your code accordingly?
You can't just replace the path with the url.
Try this:
URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg"); //set url
ImageIcon image = new ImageIcon(ImageIO.read(url)); //read image and create ImageIcon
JLabel label = new JLabel(image, JLabel.CENTER);
Remember to check for IO and MalformedURL Exceptions.

Picture and text in same window

This program is supposed to open a window, add a picture, and then add the text "hello world" above the picture. The text appears when i do frame.add(label) and then try to add the picture (like the code shows), but even when I do the opposite and add the picture first I only get a gray schreen. Can anybody show me how I can get both the picture and the text?
public window(){
JFrame frame = new JFrame("name");
JLabel label = new JLabel ("hello world", JLabel.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(600, 400);
frame.setVisible(true);
label.setAlignmentX(0);
label.setAlignmentY(0);
frame.add(label);
frame.add(new JLabel(new ImageIcon("file")));;
}
}
You should use overlay layout, but it is applicable on JPanel.
So add a JPanel to your frame then apply the layout, finally add the components.
Your code may be like that:
public window(){
JFrame frame = new JFrame("name");
JLabel label = new JLabel ("hello world", JLabel.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
public boolean isOptimizedDrawingEnabled() {
return false;
}
};
LayoutManager overlay = new OverlayLayout(panel);
panel.setLayout(overlay);
frame.setResizable(false);
frame.setSize(600, 400);
frame.setVisible(true);
label.setAlignmentX(0);
label.setAlignmentY(0);
panel.add(label);
panel.add(new JLabel(new ImageIcon("file")));
frame.add(panel, BorderLayout.CENTER);
}
}
A label can have both text and icon, and the relative position can be customized.
JLabel label = new JLabel ("hello world", new ImageIcon("file"), JLabel.CENTER);
label.setVerticalTextPosition(SwingConstants.TOP);
frame.add(label);
//frame.add(new JLabel(new ImageIcon("file")));;
The default layout is BorderLayout, and add(label, BorderLayout.CENTER).

How i can change JButton size in java?

I am learning java GUI programming and I would like to create button. Problem is when button is created it is huge. How I can resize button?
JFrame frame = new JFrame("Ikkuna <3");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel teksti = new JLabel("assdad", SwingConstants.CENTER);
teksti.setFont(new Font("Eras Demi ITC", Font.PLAIN, 32));
teksti.setForeground(Color.BLUE);//tekstin väri
ImageIcon img = new ImageIcon("C:/Users/account/Documents/NetBeansProjects/eka/src/eka/Kuvat/Trollface.PNG"); //Tämä on kuvake(icon).
frame.setIconImage(img.getImage());
JButton nappula = new JButton("Start");
frame.getContentPane().add(nappula);
nappula.setPreferredSize(new Dimension(83, 291));
nappula.setLocation(500, 350);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
teksti.setPreferredSize(new Dimension(300, 100));
frame.getContentPane().add(teksti, BorderLayout.PAGE_START);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
I also tried this:
nappula.setPreferredSize(new Dimension(83, 291));
You Can set the fonts to the button and change its size
Font f = new Font("Times New Roman",Font.BOLD,20);
nappula.setFont(f);
Set it of any size change 20 to any value......
Try it....
You're using default layout from the looks of it. Try using
nappula.setSize(new Dimension(x, y));

Categories