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.
Related
I am working on a program for my school to use. It is like match.com, but for a public school. Yes there has been permission from the school to do this.
This is the first time I have ever used swing and I am having an issue with adding my JPanel to my JFrame so that I can see and use the button.
private void Framing()
{
JPanel Panel = new JPanel();
Panel.setLayout(new BorderLayout());
JFrame Frame = new JFrame("Warning");
Frame.setUndecorated(true);
JButton OK = new JButton("EXIT");
OK.addActionListener((ActionEvent event) -> {System.exit(0);});
OK.setBounds(100,100,100,100);
Panel.add(OK, BorderLayout.CENTER);
Frame.getContentPane().add(Panel, BorderLayout.CENTER);
Panel.setLocation((Frame.getWidth()-Panel.getWidth())/2,0);
Frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
Frame.setLocation(600, 300);
Frame.setResizable(false);
Frame.setLayout(null);
Frame.setVisible(true);
}
What is the fastest way to fix the issue with the panel not even showing up?
Any solutions are welcomed.
Since you are a new Swing programmer, I'll try to explain with below sample code. Here I have taken your code and done few changes. See my comments in the code.
With these changes, now the program works and shows a window with a big button. When user clicks the button program exits.
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
public class SwingTest
{
private void Framing() //Better method name would be "showFrame()"
{
JPanel Panel = new JPanel(); //Better variable name would be "panel"
Panel.setLayout(new BorderLayout());
JFrame Frame = new JFrame("Warning"); //Better variable name would be "frame"
Frame.setUndecorated(true);
JButton OK = new JButton("EXIT"); //Better variable name would be "exitButton"
OK.addActionListener((ActionEvent event) -> {System.exit(0);});
//Not necessary. Layout manager will handle this.
//OK.setBounds(100,100,100,100);
Panel.add(OK, BorderLayout.CENTER);
Frame.getContentPane().add(Panel, BorderLayout.CENTER);
//Not necessary. Layout manager will handle this.
//Panel.setLocation((Frame.getWidth()-Panel.getWidth())/2,0);
Frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
Frame.setLocation(600, 300);
Frame.setResizable(false);
//This is the main problem. You should avoid this.
//Frame.setLayout(null);
Frame.setVisible(true);
}
public static void main(String[] args)
{
new SwingTest().Framing();
}
}
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.)
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?
the code is used to create an application which shows picture and if your anwser is correct your supposed to see the next picture but the pictureCount does not go up. all of the variables ar declared after the main class and i created an Actionlistener to check if the awnser is correct.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
public static void main(String[] args) {new Main().test();}
public int pictureCount = 1;
JFrame frame = new JFrame();
JButton button1 = new JButton("Submit");
JTextField text = new JTextField();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JLabel label = new JLabel(new ImageIcon("C:\\Users\\Admin\\Desktop\\practicum 3\\" + pictureCount + ".jpg"));
void test(){
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(text.getText().equals("5")){
pictureCount++;
new Main().test();
}
}
});
panel1.add(button1);
panel2.add(text);
panel3.add(label);
text.setPreferredSize(new Dimension(100,50));
panel1.setPreferredSize(new Dimension(1000, 200));
panel2.setPreferredSize(new Dimension(1000, 100));
panel3.setPreferredSize(new Dimension(1000, 450));
frame.getContentPane().add(BorderLayout.SOUTH, panel1);
frame.getContentPane().add(BorderLayout.CENTER, panel2);
frame.getContentPane().add(BorderLayout.NORTH, panel3);
frame.setSize(1000,750);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Operation Screen");
frame.setLocationRelativeTo(null);
}
}
You need to read in all the pictures as ImageIcons into an array or ArrayList, say called imageIconArray and then display imageIconArray[0] in your JLabel when you start.
When the button is pressed, increment pictureCount, and then reset the JLabel's icon via its setIcon(...) method:
// in the ActionListener code:
pictureCount++;
label.setIcon(imageIconArray[pictureCount];
Whatever you do, don't create a new Main object, despite what others might say. Why create a new GUI when all you need to do is swap displayed images?
This is the information for what I think is a correct basic GUI, but only the frame is showing up. I don't know why this is. I have everything initialized and set to visible and added, but it acts like nothing is added. Thanks for the help!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class basicButtonPress
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton button = new JButton();
JLabel label = new JLabel();
frame = new JFrame("Test Pop - Up");
frame.setVisible(true);
frame.setSize(600,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel = new JPanel();
panel.setBackground(Color.YELLOW);
panel.add(button);
panel.add(label);
button = new JButton("Test");
label = new JLabel("This is test label");
}
}
You're calling new JFrame() twice; same with JPanel, JButton and JLabel. Remove the duplicates and you'll be closer to fixing the problem.