I am trying to add image in JLabel but it's not working. The second label is working but the first JLabel is not working.
Here is code.
Thanks in advance.
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MainLabel {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jframe;
jframe = createFrame();
ImageIcon ii = new ImageIcon("images.jpeg");
JLabel label = new JLabel(ii);
jframe.add(label);
Label label123 = new Label("Be Nice to World!!");
jframe.add(label123);
jframe.setVisible(true);
}
static JFrame createFrame() {
JFrame guiFrame = new JFrame();
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("BorderLayout Example");
guiFrame.setSize(700, 300);
return guiFrame;
}
}
Your label which contains the image is being replaced with label123 in the BorderLayout.CENTER position, which doesnt have any image attached. You could use:
label123.setIcon(ii);
If you want the 2 labels to be shown, you could place the text-based label123 in the SOUTH location:
jframe.add(label123, BorderLayout.SOUTH);
Note: Use JLabel instead of Label.
Related
I'm trying to set a Icon on the top left corner for my Window. but don't seem to work.. what do you think seems to be the problem?
I also tried setting an Icon and Text on an instance of JLabel but only the text came out.
The link is a screenshot for the output of the JFrame I made.
public class Window extends JFrame{
private final int WIDTH = 600;
public Window(){
// Setting the Window
setTitle("Module Organizer");
ImageIcon img = new ImageIcon("checkmark-square.png");
setIconImage(img.getImage());
setSize(WIDTH,(WIDTH*3)/4);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// adding components
add(new NavPane(), BorderLayout.WEST);
setVisible(true);
}
}
Here is the code
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class Window{
JFrame f=new JFrame("Test");
public Window(){
JPanel j=new JPanel();
f.setSize(1000,500);
ImageIcon img = new ImageIcon("Image.jpg");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel l=new JLabel(img);
j.add(l);
f.add(l);
f.setVisible(true);
}
public static void main(String[] args) {
new Window();
}
}
I am trying to display a background image on the JFrame using a JLabel. The code runs and the buttons appear, but the image does not. I have researched for solutions, yet I have not found one for my code specifically. Any help would be greatly appreciated.
/**
* Adds details to interface and programs buttons
*
* Imani Davis
* Final Project
*/
import java.awt.*;
import java.awt.GridBagLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.border.EmptyBorder;
public class Use_PF_Interface extends JFrame implements Pet_Fish_Interface
{
// instance variables - replace the example below with your own
private JFrame window;
private JPanel panel1, panel2, panel3;
private JLabel lblBackgroundImage = new JLabel();
private JButton feedButton = new JButton("Feed Fish");
private JButton playGamesButton = new JButton("Play Game");
/**
* Constructor for objects of class Use_PF_Interface
*/
public Use_PF_Interface()
{
setTitle("Virtual Pet Fish");
setSize(650, 650);
//initializes panels and panel layout
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel1.setLayout(new FlowLayout());
panel2.setLayout(new FlowLayout());
panel3.setLayout(new FlowLayout());
lblBackgroundImage.setLayout(new FlowLayout());
//sets background image of panel
lblBackgroundImage.setIcon(new ImageIcon("C:\\Users\\This PC\\Desktop\\OCEAN2.JPEG"));
panel1.add(lblBackgroundImage);
validate();
//adds button to panels
panel2.add(feedButton);
panel2.add(playGamesButton);
//add panels to frame
add(panel1);
add(panel2);
}
}
JFrame uses a BorderLayout by default, a BorderLayout can only manage a single component within any of the five available positions it provides, this means that panel2 is most likely the only component getting shown.
An alternative is to add you components to the JLabel, but remember, JLabel doesn't have a default layout manager. Also, remember, JLabel only uses the icon and text properties to calculate its preferred size, so if the contents require more space, they will be clipped.
Start by having a look at How to Use BorderLayout for more details
Also, remember, most Swing components are opaque generally, so you need to set them transparent when you want to do something like this
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Use_PF_Interface extends JFrame {
// instance variables - replace the example below with your own
private JPanel panel2;
private JLabel lblBackgroundImage = new JLabel();
private JButton feedButton = new JButton("Feed Fish");
private JButton playGamesButton = new JButton("Play Game");
/**
* Constructor for objects of class Use_PF_Interface
*/
public Use_PF_Interface() {
setTitle("Virtual Pet Fish");
setSize(650, 650);
//initializes panels and panel layout
panel2 = new JPanel();
panel2.setOpaque(false);
panel2.setLayout(new FlowLayout());
lblBackgroundImage.setLayout(new FlowLayout());
//sets background image of panel
lblBackgroundImage.setIcon(new ImageIcon("..."));
lblBackgroundImage.setLayout(new BorderLayout());
//adds button to panels
panel2.add(feedButton);
panel2.add(playGamesButton);
lblBackgroundImage.add(panel2);
add(lblBackgroundImage);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new Use_PF_Interface();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Try this,
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImageInFrame {
public static void main(String[] args) throws IOException {
String path = "Image1.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel label = new JLabel(new ImageIcon(image));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(label);
f.pack();
f.setLocation(200,200);
f.setVisible(true);
}
}
I have a JFrame with a JButton , this button open a new JFrame where there should be a text box ( JTextField ) that I will use for a search , the problem is that I don't know how to insert it . I came up with this :
N.B I'm a beginner, sorry in advance for the easy question :)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MainWindow {
// Seconda Finestra
public static void NuovaFinestra (JPanel panel) {
panel.setLayout(null);
JButton Ricerca = new JButton("Ricerca");
Ricerca.setBounds(100, 100, 200, 50);
panel.add(Ricerca);
Ricerca.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JFrame FinestradiRicerca = new JFrame("Finestra di Ricerca");
FinestradiRicerca.setBounds(300, 300, 500, 500);
FinestradiRicerca.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel riquadroRicerca = new JPanel();
FinestradiRicerca.add(riquadroRicerca);
FinestradiRicerca.setVisible(true);
JTextField ciao;
ciao = new JTextField ();
}
});
}
//Main
public static void main(String[] args) {
//Finestra Principale
JFrame finestra = new JFrame("Finestra principale");
finestra.setSize(500, 500);
finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JPanel della finestra principale
JPanel riquadro = new JPanel();
finestra.add(riquadro);
finestra.setVisible(true);
NuovaFinestra(riquadro);
}
}
You needed to add your new elements to riquadroRicerca BEFORE adding the Panel to FinestradiRicerca, I recommend you NOT to use null layout but a Layout Manager or combinations of them. If you insist on keeping null layout then see below example. But for this kind of app I'd suggest a CardLayout.
I also suggest not using multiple JFrames since they will open multiple windows on task bar which is user unfriendly. See: Use of multiple JFrames, Good / Bad Practice
As an aside note, follow Java naming conventions. For example you called a JFrame as FinestradiRicerca instead rename it to: finestradiRicerca (1st letter of a variable in lowercase).
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MainWindow {
// Seconda Finestra
public static void NuovaFinestra (JPanel panel) {
panel.setLayout(null);
JButton Ricerca = new JButton("Ricerca");
Ricerca.setBounds(100, 100, 200, 50);
panel.add(Ricerca);
Ricerca.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JFrame FinestradiRicerca = new JFrame("Finestra di Ricerca");
FinestradiRicerca.setBounds(300, 300, 500, 500);
//If you don't want to close whole app when closing this windo change it to: JFrame.DISPOSE_ON_CLOSE
FinestradiRicerca.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel riquadroRicerca = new JPanel();
JTextField ciao;
JLabel myLabel = new JLabel("Here goes your label text");
ciao = new JTextField ();
ciao.setColumns(20);
riquadroRicerca.add(myLabel);
riquadroRicerca.add(ciao);
FinestradiRicerca.add(riquadroRicerca);
FinestradiRicerca.setVisible(true);
}
});
}
//Main
public static void main(String[] args) {
//Finestra Principale
JFrame finestra = new JFrame("Finestra principale");
finestra.setSize(500, 500);
finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JPanel della finestra principale
JPanel riquadro = new JPanel();
finestra.add(riquadro);
finestra.setVisible(true);
NuovaFinestra(riquadro);
}
}
So, your code after a few modifications, to make JLabel and JTextField visible gives the following output:
However, please follow my above recomendations.
Add the JTextField to your new JFrame like this. You also have to initialize your textfield. It is basically the same as you have done with the initial JFrame.
JTextField ciao = new JTextField();
FinestradiRicerca.add(ciao);
I want the racket to be positioned by using the getWidth method for the gamePanel, but it returns 0. It works fine for the frame, and it worked for the panel as well, but I decided to rewrite alot of the code and now I cant get it to work. Help is appreciated :)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Oppgave1 extends JFrame {
public static void main(String[] args) {
JFrame frame = new Oppgave1();
frame.setTitle("Oppgave 1");
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private JLabel jlbBallCount = new JLabel("Ballcount");
private JLabel jlbTimer = new JLabel("Timer");
private JButton jbtNewBall = new JButton("New Ball");
private Racket racket;
private Ball ball;
public Oppgave1() {
JPanel buttonPanel = new JPanel();
buttonPanel.add(jlbBallCount);
buttonPanel.add(jlbTimer);
buttonPanel.add(jbtNewBall);
JPanel gamePanel = new JPanel();
gamePanel.add(racket = new Racket (100, 60));
gamePanel.add(ball = new Ball(10, 3, racket));
this.add(gamePanel, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
}
public void paint(Graphics g){
racket.draw(g);
ball.draw(g);
}
}
Don't override the paint() method of a JFrame.
You added the racket and ball to the game panel. The game panel will now paint these components automatically.
If you want to be able to move these components then you must set the layout to null and initially set the bounds of the components. Then when you want to move the component you just invoke the setLocation() method and Swing will paint the component in its new position.
i want to add images or labels with image in it to a panel at any location which will be decided by the user clicking on the panel(add image where user clicks in the panel).
how to do this.
thanks
try (and tweak) this sscce:
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class JLabelOnClick extends JFrame {
public static void main(String[] args) {
final JFrame frame = new JLabelOnClick();
final JPanel panel = new JPanel();
panel.setLayout(null);
frame.setContentPane(panel);
frame.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
// if you want an image instead, use the JLabel(Icon image)
// constructor
JLabel label = new JLabel("test");
label.setBounds(e.getX(), e.getY(), label.getPreferredSize().width, label
.getPreferredSize().height);
panel.add(label);
panel.validate();
frame.repaint();
}
});
frame.setSize(new Dimension(200, 200));
frame.setVisible(true);
}
}
set null as LayoutManager for that panel and then manually set position for each image.