Im trying to add an image to a JLabel and i have encountered a problem where the image is to big for the JLabel so I'm looking for a way to make the image automatically scale to the size of the JLabel.
Here is my main body of code where i call up my GUImain class:
public class Main
{
public static void main(String[] args)
{
int i = 0;
int t = 0;
int st = 0;
int h = 0;
Texts textObject = new Texts();
textObject.TextList();
Commands commandObject = new Commands();
commandObject.commands();
GUImain guiObject = new GUImain();
guiObject.displayGUI();
}
}
And here is the code for my GUImain class, i will only include one image button because there are allot of objects in my code.
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JScrollBar;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class GUImain
{
private JFrame frame;
private JTextField textField;
private ImageIcon image1;
public void displayGUI()
{
this.frame.setVisible(true);
}
//Launch the application.
public static void main(String[] args)
{
GUImain window = new GUImain();
}
//Create the application.
public GUImain()
{
frame = new JFrame();
frame.setBounds(100, 100, 611, 471);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
...
JLabel lblHunger = new JLabel();
Image img2 = new ImageIcon(this.getClass().getResource("/Food.png")).getImage();
lblHunger.setIcon(new ImageIcon(img2));
lblHunger.setBounds(211, 58, 50, 50);
panel.add(lblHunger);
}
}
}