Why am I not getting the label on the screen. This is my code :
class Guide extends JComponent {
public static void main(String[] args) {
Guide guide = new Guide();
JFrame frame = new JFrame("Pong Game's Guide");
frame.add(guide);
frame.pack();
frame.setSize(600,500);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
JLabel guideLabel = new JLabel();
guideLabel.setText("GUIDE");
guideLabel.setFont(new Font("Serif",Font.BOLD, 20));
guideLabel.setForeground(new Color(255,0,0));
guideLabel.setBounds(290,25,50,20);
frame.add(guideLabel);
}
public void paintComponent(Graphics g) {
g.setColor(new Color(150,255,150));
g.fillRect(0,0,690,590);
}
}
What am I doing wrong here? Please help.
You’ve made the window visible before adding the label. You will need to revalidate and repaint the container
JFrame by default uses a BorderLayout, which by default only allows a single component to be laid out in each of 5 available positions, which likely to cause issues. Probably better to add it to guide
Try adding the label to the frame, then setting the visibility of the frame to true.
Related
I'm trying to add an image to a JFrame and set its location, I don't know why it just does not add into it, maybe I don't understand how the JFrame class works since a normal text JLabel adds into the JFrame simply without any trouble and a JLabel containing an image simply does not add in.
I would appreciate if someone would explain the error in the code, and maybe even give me a short explanation of why my way does not work. Thanks!
import java.awt.*;
import javax.swing.*;
public class Walk {
public static void main(String[] args) {
JFrame f = new JFrame("Study");
f.setSize(3000,1000);
f.getContentPane().setBackground(Color.white);
f.getContentPane().add(new JLabel("test", JLabel.CENTER) );
JLabel l = new JLabel(new ImageIcon("C:\\Users\\leguy\\OneDrive\\Desktop\\Stuff\\stillsp"));
l.setBounds(100, 100, 100, 100);
l.setVisible(true);
f.add(l);
f.setVisible(true);
}
}
are you using a gui class or you are writing its code into a main class
what is in your code is that you are writing its code so easy way is to just drag and drop try this link for normal jframes gui eclipse gui
about the picture into jframe is easy one all what you have to do is
1. create a label by setting its size as you want on the jframe by dragging and dropping only
2. follow the pictures
then you browser for your picture you want
select the picture and its done
Hope it helps
Make sure the path to your image is valid. All I did was point to a valid image on my PC and the code practically worked. There were a few things added and organized below.
import java.awt.Color;
import javax.swing.*;
public class Walk {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() { // Safety first...
#Override
public void run() {
String path = "C:\\Path\\To\\Image.png"; // Make sure it's correct
JFrame frame = new JFrame("Study");
JLabel label = new JLabel(new ImageIcon(path));
frame.setSize(3000, 1000);
frame.getContentPane().setBackground(Color.white);
frame.getContentPane().add(new JLabel("test", JLabel.CENTER));
label.setBounds(100, 100, 100, 100);
label.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(label);
frame.pack(); // Pack the frame's components.
frame.setVisible(true);
}
});
}
}
To make sure both labels show up, provide a layout and add them accordingly.
import java.awt.*;
import javax.swing.*;
public class Walk {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
String path = "C:\\Path\\To\\Image.png"; // Make sure it's correct
JFrame frame = new JFrame("Study");
Container container = frame.getContentPane();
JLabel imageLbl = new JLabel(new ImageIcon(path));
JLabel textLbl = new JLabel("test");
frame.setLayout(new BorderLayout());
frame.setSize(3000, 1000);
imageLbl.setBounds(100, 100, 100, 100);
imageLbl.setVisible(true);
container.setBackground(Color.WHITE);
container.add(textLbl, BorderLayout.NORTH);
container.add(imageLbl, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
}
Why is paintComponent(Graphics) not called when adding a custom JComponent?
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame("Paint Component Example");
frame.setPreferredSize(new Dimension(750, 750));
frame.setLocationByPlatform(true);
JPanel panel = new JPanel();
panel.add(new CustomComponent());
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
public class CustomComponent extends JComponent {
public CustomComponent() {
super();
}
#Override
protected void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(10, 10, 10, 10);
}
}
I know there is no reason for creating a custom component in this instance of but it is an extremely simplified version of another issue I can't figure out.
JPanel panel = new JPanel();
panel.add(new CustomComponent());
The default layout manager of a JPanel is a FlowLayout. A FlowLayout will respect the preferred size of any component added to it. By default the preferred size of a JComponent is (0, 0) so there is nothing to paint so the paintComponent() method never gets called.
Override the getPreferredSize() method of your CustomComponent class to return the preferred size of your component.
Also, don't forget to invoke super.paintComponent(...) at the start of the method.
Read the section from the Swing tutorial on Custom Painting for more information and working examples.
When I set a look and feel through the UIManager.setLookAndFeel the frame's content changes its look and feel as expected. E.g.
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(new JButton("Some Button"));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(200, 80);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
But the frame is still painted using the OS default (Windows in my case).
How can I make the frame look and feel aware so that it looks like the e.g. the nimbus look and feel?
Usually look and feel is implemented using the ComponentUI, but a JFrame is not a JComponent so I can't implement an own ComponentUI and set it.
1. Solution
My first thought was to use an undecorated JFrame with a JInternalFrame as its main component.
public class LAFAwareJFrame extends JFrame {
private JInternalFrame lafFrame = new JInternalFrame("", true, true, true, true);
private JDesktopPane desktopPane = new JDesktopPane();
public LAFAwareJFrame() {
super.setUndecorated(true);
desktopPane.add(lafFrame);
lafFrame.setVisible(true);
Container contentPane = super.getContentPane();
contentPane.add(desktopPane);
}
#Override
public void setUndecorated(boolean undecorated) {
throw new UnsupportedOperationException("Can't change the undecorated property for a LAFAwareJFrame");
}
#Override
public void setSize(int width, int height) {
super.setSize(width, height);
lafFrame.setSize(width, height);
}
#Override
public Container getContentPane() {
return lafFrame.getContentPane();
}
#Override
public void setTitle(String title) {
lafFrame.setTitle(title);
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
JFrame frame = new LAFAwareJFrame();
Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(new JButton("Some Button"));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(200, 80);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
But then I have a lot to do with event handling and delegation. E.g when the JInternalFrame gets moved I don't really want to move the internal frame. Instead I want to move the undecorated frame.
2. Solution
Use the JInternalFrame only as a renderer. Like a ListCellRender.
Since all solutions require a lot of work I want to ask you first if there is a better solution. E.g. a library or maybe it is already possible with standard Java and I missed something.
EDIT
I tried to use setDefaultLookAndFeelDecorated but it doesn't work with nimbus and not with motif.
I've been programming in Java for about six months now and have done many work with Graphics in JPanels and JFrames. But recently I got a problem. All the JFrames I've been making before were always setUndecorated(false), but I needed to make one that was setUndecorated(true). So I tried, like usual, just putting the frame.setUndecorated(true) above all the frame's specs, so it looked something like this:
public static void main(String[] args){
JFrame frame = new JFrame("Frame");
frame.setUndecorated(true);
frame.setSize(600, 800);
frame.setVisible(true);
frame.add(new custompanel());
}
And the custompanel class:
public class custompanel{
public void paintComponent(Graphics g){
g.fillRect(100, 100, 100, 100);
}
}
The g in custompanel doesn't paint anything.
However, if I remove the frame.setUndecorated(true) or change it to frame.setUndecorated(false) it will paint a rectangle.
Any thoughts?
Assuming CustomPanel extends JComponent or JPanel, make the frame visible after adding CustomPanel. Calling super.paintComponent(g) is a good idea to paint any background components.
frame.setVisible(true);
I'm trying to set the the location of an image in a JFrame. I thought label.setLocation(100, 100); would set the location of the image to 100, 100 (top left corner), but it doesn't seem to do anything no matter were I put it. I even tried panal.setLocation(100, 100). Both do nothing, I get no errors and the image does appears but at 0, 0. What am I doning wrong? Here's my code:
import javax.swing.*;
public class DisplayImage {
public DisplayImage() {
JFrame frame = new JFrame("Display Image");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = (JPanel)frame.getContentPane();
JLabel label = new JLabel();
label.setIcon(new ImageIcon("src/img/school.png"));
label.setLocation(100, 100);
panel.add(label);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public static void main (String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new DisplayImage();
}
});
}
}
By default a JFrame uses a BorderLayout. When you add your label to the frame the label is added to the CENTER of the BorderLayout. The layout manager will override the location.
One simple solution is to add an EmptyBorder to the label with the top/left insets being 100. Then instead of add the label to the center you would add the label to the NORTH. The code would be something like:
label.setBorder( new EmptyBorder(...) );
panel.add(label, BorderLayout.SOUTH);
As a general rule you should not be trying to specify exact location of a component. Let the layout managers to their jobs.
The JFrame, by default, uses a BorderLayout as it's layout manager. This will override any settings you supply to the setLocation method.
You have a number of options...
Use something like JLayeredPane, which does not, but default, have layout manager of it's own set by default. See How to use layered panes for more details
Create a custom component capable of renderering the image where you want it. Check out Performing Custom Painting for more details
Create your own layout manager that performs the operations you want...
This works for me:
import java.awt.Dimension;
import javax.swing.*;
public class DisplayImage {
public DisplayImage() {
JFrame frame = new JFrame("Display Image");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = (JPanel)frame.getContentPane();
panel.setLayout(null);
JLabel label = new JLabel();
label.setIcon(new ImageIcon("rails.png"));
panel.add(label);
Dimension size = label.getPreferredSize();
label.setBounds(100, 100, size.width, size.height);
frame.setSize(300, 200);
frame.setVisible(true);
}
public static void main (String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new DisplayImage();
}
});
}
}
But you should read this:
http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html