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);.
Related
I am trying to make two buttons in JPanel side by side. I have the following code as of now:
JFrame frame = new JFrame("Display Shapes");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JButton button = new JButton();
button.setBounds(50,100,80,30);
button.setText("Load Shapes");
panel.add(button);
JButton button2 = new JButton();
button2.setBounds(100,100,80,30);
button.setText("Sort Shapes");
panel.add(button2);
frame.add(panel);
frame.setSize(600, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
When I run the code, below is the output I see. I am not sure why the second button is not appearing correctly. What could be the reason for this?
The place where you are writing button.setText("Sort Shapes");, it will be button2.setText("Sort Shapes");
It is changing the text of the first button whose text had been set earlier
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
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?
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).
I have tried to use a Jlist but I have not been able to get it to show up in my JFrame.
Here is my code:
private static void list(){
JFrame frame = new JFrame();
frame.setTitle("Menu");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLayout(new FlowLayout());
listModel = new DefaultListModel();
listModel.addElement("Add Member");
listModel.addElement("Add Meeting");
listModel.addElement("Record Attendance");
list = new JList(listModel);
list.setVisibleRowCount(3);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
JScrollPane listScrollPane = new JScrollPane(list);
frame.add(listScrollPane, BorderLayout.CENTER);
}
Currently when I run the program the frame will open, but it is blank. Any help?
Make frame.setVisible(true); the last line of the function.
You have set the layout as frame.setLayout(new FlowLayout()); but you have used
frame.add(listScrollPane, BorderLayout.CENTER); try changing the layout to border layout eg: frame.setLayout(new BorderLayout());