Image not showing in a JFrame - java

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.

Related

JLabel does not show up [duplicate]

This question already has answers here:
Why are my items not showing up in JFrame?
(6 answers)
Closed last year.
I have been following the tutorial of a youtube video, but although my code is basically a carbon copy of this person's, my JLabel did not show:
Note: I will use a Layout later, I'd like to do this without first.
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import java.awt.Color;
public class Main {
public static void main(String[] args) {
ImageIcon icon = new ImageIcon("blusq.png");
JLabel label = new JLabel();
label.setText("Hi");
label.setIcon(icon);
JPanel redPanel = new JPanel();
redPanel.setBackground(Color.red);
redPanel.setBounds(0,0,250,250);
JPanel bluePanel = new JPanel();
bluePanel.setBackground(Color.blue);
bluePanel.setBounds(250,0,250,250);
JPanel greenPanel = new JPanel();
greenPanel.setBackground(Color.green);
greenPanel.setBounds(0,250,500,250);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setSize(750,750);
frame.setVisible(true);
bluePanel.add(label);
frame.add(redPanel);
frame.add(bluePanel);
frame.add(greenPanel);
}
}
I have been able to make the label show up by putting the "frame.setVisible" line to the end of code, but I'd like to know why this works for the youtube guy but not for me?
Also, I can't seem to add the label to both the frame and a label at the same time, which I don't quite understand either:
JFrame frame = new JFrame();
bluePanel.add(label);
// frame.add(label);
frame.add(redPanel);
frame.add(bluePanel);
frame.add(greenPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setSize(750,750);
frame.setVisible(true);
Works
JFrame frame = new JFrame();
bluePanel.add(label);
frame.add(label);
frame.add(redPanel);
frame.add(bluePanel);
frame.add(greenPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setSize(750,750);
frame.setVisible(true);
Does not work
You should add 'label.setVisible(true);' to your code.

ImageIcon will not display image

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.

How do I change this JButton to not take up the whole width?

I used the internet as a resource to help me make a JButton but for some reason I cannot get it to a normal size. I have changed the dimensions and messes around a lot but it is not working.
Here is my code:
import javax.swing.*;
import java.awt.*;
public class ScreenSaver {
public static void main(String[] args) {
JFrame frame = new JFrame();
JButton button = new JButton();
JPanel panel = new JPanel();
button.setSize(100, 100);
panel.add(button);
frame = new JFrame ("Screen Saver");
frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.setBackground(new Color(1.0f,1.0f,1.0f,0.1f));
frame.setResizable(true);
frame.setVisible(true);
}
}
Try to use
frame.setLayout(null);
then you can
button.setLocation(x,y);
It will help you to place Java components in the locations you want.
Otherwise default Java Layout is applied to your Frame and then you cannot fully customise design!
Remember then you need to customise all components you are adding to JFrame. (setSize, setLocation to JPanel as well.)

Java Sizing JPanel & JPanel Components

I'm trying to build a GUI in Java Swing. I have alot of trouble sizing components. I managed to size a JPanel by setting the dimensions with setPreferredSize(), but I can't get the components within that JPanel to size properly.
This is my screen with only the JPanel visible.
When I add the button to my JPanel the following happens:
As you can see the button is taking up the whole lenght and width of my JPanel/JFrame. Why is this happening? How can I fix it?
Here is my code:
Application.java
public void start() {
ControllerObserveer observeer = new ControllerObserveer();
frame = new JFrame("-");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setLayout(new BorderLayout());
frame.add(observeer.getView(), BorderLayout.CENTER);
frame.setResizable(false);
}
ControllerObserveer.java
public class ControllerObserveer {
private ModelObserveer model;
private ViewObserveer view;
public JPanel getView(){
return this.view.p;
}
}
ViewObserveer.java
public class ViewObserveer {
public JPanel p;
public ViewObserveer(){
this.p = new JPanel(new BorderLayout(), false);
p.setPreferredSize(new Dimension(500, 500));
p.setBackground(Color.red);
JButton b = new JButton("Hello World!");
b.setPreferredSize(new Dimension(40, 40));
p.add(b);
}
}
At last I would like to ask what the diffrences are between the diffrent layouts, like BorderLayout() or BoxLayout() for example.
Thank you for your time!
Install Java WindowBuilder on Eclipse. So, you can get things done.. "but I can't get the components within that JPanel to size properly."

Java: Having Multiple Components on a JFrame

I want to have multiple components on a JFrame. I don't want any UI formatting. I just want multiple components to be drawn. My full code is to large to post so here is my JFrame set up.
JPanel jPanel = new JPanel();
jPanel.setLayout(new FlowLayout());
jPanel.add(board.getPieceAt(0,0));
jPanel.add(board);
frame.add(jPanel);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
board and board.getPieceAt(0,0); are JComponents
No combination of frame.add() or adding panels seems to get both to render. It's always the last one added that gets drawn.
This example renders two components. I updated my code above to mimic this example as much as possible and it still results in an empty frame. To me it seems that I'm doing exactly what the example is doing yet I get a different result.
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import javax.swing.*;
public class TestFrameExample {
public static void main(){
JFrame frame = new JFrame("JFrame Example");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel("This is a label!");
JButton button = new JButton();
button.setText("Press me");
panel.add(label);
panel.add(button);
frame.add(panel);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
How do I get both components to render on the JFrame?

Categories