Picture and text in same window - java

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).

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.

Java Swing GUI JLabel Not Showing

I am trying to write a Title for the main menu of my program, by using a JLabel, but it doesn't seem to appear on my screen.
import javax.swing.*;
public class GUI {
public GUI() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
panel.setLayout(new GridLayout());
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Title");
frame.pack();
frame.setSize(854,560);
frame.setVisible(true);
JLabel title = new JLabel();
title.setText("Title");
//title.setSize();
title.setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
}
What am I doing wrong and how could I change the position of the Text if I manage to make it visible?
And I also want to add a button to go to the next page so if you could tell me how to do that too that would be great.
I would quickly and untested say that you are adding the label after you set the frame visible.
Do it before. Else you would have to revalidate and repaint the frame
As I can see in your code you are not adding title in panel. As a quick solution put panel.add(title); after title.setVisible(true); line in your code, it will display the label.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
panel.setLayout(new GridLayout());
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Title");
frame.pack();
frame.setSize(854,560);
frame.setVisible(true);
JLabel title = new JLabel();
title.setText("Title");
//title.setSize();
title.setVisible(true);
panel.add(title); //<---- this one line will diaplay label in GUI

JPanel won't show in JFrame

I'm trying to create a JPanel (non-resizable) showing a grid of buttons but when I try to add the JPanel to a JFrame it won't show.
JFrame frame = new JFrame("frame");
JPanel panel = new JPanel();
frame.setSize(681,920);
frame.setResizable(true);
JLabel label = new JLabel();
label.setLayout(new FlowLayout(FlowLayout.LEADING,0,0));
JButton btn = new JButton();
btn.setContentAreaFilled( false );
btn.setBorder( null );
btn.setBounds(214,210,0,0);
label.add(btn);
panel.add(label);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
The output should be a resizable frame with inside a 3x4 grid of buttons.
If I don't use the panel and I put the line frame.setResizable(false) it works as expected but I need to add more stuff to the frame so I need to put the buttons safe in a panel.
Both panel and label are added to your frame, to make sure they are added write
JLabel label = new JLabel("JLABEL");
and
panel.setBackground(Color.BLUE);

JFrame not displaying panel content

I just got started learning JFrame and is trying to create a frame containing JLabels and JTextFields frame using grouplayout, but the contents inside my panel aren't appearing when I run the program.
All help is appreciated.
package practice;
import java.awt.*;
import javax.swing.*;
public class Boxc {
public static void main(String[] args){
JFrame frame = new JFrame("---------------------(-_-)---------------------");
JLabel headText = new JLabel("Teach Me");
//head text
headText.setVerticalAlignment(JLabel.TOP);
headText.setHorizontalAlignment(JLabel.CENTER);
headText.setFont(headText.getFont().deriveFont(20f));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 500);
frame.setResizable(false);
frame.setLocationRelativeTo(null); //Center start position
frame.add(headText);
//panel 1
JPanel panel1 = new JPanel();
GroupLayout layout = new GroupLayout(panel1);
panel1.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
JLabel uInput = new JLabel("When You Type:");
JTextField uText = new JTextField("Enter Here");
JLabel iReply = new JLabel("I Reply:");
JTextField iText = new JTextField("Enter Here");
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
hGroup.addGroup(layout.createParallelGroup().
addComponent(uInput).addComponent(iText));
hGroup.addGroup(layout.createParallelGroup().
addComponent(uText).addComponent(iText));
layout.setHorizontalGroup(hGroup);
frame.add(panel1);
}}
Java is weird.
It all has to do with the ordering of your JFrame methods. You must ALWAYS do frame.setVisible(true); at the end, preferably as the last line of code in whatever you're using to initiate the JFrame. In that case,
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 500);
frame.setResizable(false);
frame.setLocationRelativeTo(null); //Center start position
frame.add(headText);
would be changed to
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 500);
frame.setResizable(false);
frame.setLocationRelativeTo(null); //Center start position
frame.add(headText);
frame.setVisible(true);
Hope this helps.
EDIT: ignore the changing from/to. Put the frame.setVisible(true); after frame.add(panel1);.

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.

Categories