Adding many labels to JFrame - java

I have a class called Labels where I have like about 80 JLabels.
I created an object in my JFrame:
Labels labels;
and now I want to add all JLabels to my JFrame. Is there a way I can somehow do this?

public class AddMoreLablesToFrame{
private static void main(String args []){
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout()); // <-- you need this for now
for(int i=0;i<80;i++){
JLabel label = new JLabel("Label" +i);
frame.add(label);
}
frame.setVisible(true);
// optional, but nice to have.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
}
}
For more: Creating a GUI With JFC/Swing

Related

Java JFrame inner size

How can I make the inner size 500x500 pixel?
Or should I hardcode the 28px macOS top-bar for windows?
My simple code:
import javax.swing.JFrame;
public class Hello {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
}
}
Don't set the JFrame size. Use a JPanel and add that to the JFrame and set the size of the JPanel.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(500,500));
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null); // centers on screen.
frame.setVisible(true);
If you are extending JPanel it is best to set the size by overridding the following:
#Override
public Dimenison getPreferredSize() {
return new Dimension(500,500);
}
It is also considered best practice to do most layouts and especially painting inside JPanel(s) and not the JFrame.

pack() method of JFrame doesn't work (java,ubuntu)

i'm new in java .
i just learn JPanel and JFrame.
i got this note from java software solutions:
" The pack method of the frame sets its size appropriately based on
its contents—in this case the frame is sized to accommodate the size
of the panel it contains."
so i wrote this code :
public static void main (String [] args){
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
//frame.setSize(1000, 500);
frame.getContentPane().add(panel);
Color darkBlue = new Color(8,40,94);
panel.setSize(1000, 500);
panel.setBackground(darkBlue);
}
but it the result is a really tiny window that i should maximize it with mouse to see the content
but when i set frame size every thing work great!
and i use Ubuntu.
so what's the reason of this problem?
From the order of your code:
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
You did not add anything into the frame before you pack() it. pack() means let the frame decide its size based on the components being added to it.
Since you have no components added to it before you pack() it, you receive a small window with visually nothing inside (until you resize the window).
When the frame is being resized, paintManager will be consulted to paint the contentPane, hence if you add before pack(), not only the frame will be resized nicely for you, the components within it will be painted as well.
To see the components within the JFrame:
public static void main (String [] args){
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
panel.add(label1); //Add label to panel
frame.add(panel); //Add panel (with label) to frame
frame.pack(); //Let the frame adjust its size based on the added components
frame.setVisible(true);
}
public static void main (String [] args){
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
Color darkBlue = new Color(8,40,94);
panel.setPreferredSize(new Dimension(1000, 500));
panel.setBackground(darkBlue);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setSize(1000, 500);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
You should use pack() after setting the sizes.
Furthermore panel.setPreferredSize() works better than setSize() for you :)
call jframe.pack() before jframe.setVisible() method !
public static void main (String [] args){
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
panel.setSize(1000, 500);
frame.getContentPane().add(panel);
Color darkBlue = new Color(8,40,94);
panel.setBackground(darkBlue);
frame.pack() ;
frame.setVisible(true); }
You will also need to check the default layout of JFrame , which is flow layout !

Why not jframe shows button?

public class Benim extends JFrame {
Container contentArea = getContentPane ();
public Benim(){
JFrame frame=new JFrame("Concentration");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
setSize(800, 800);
JButton start=new JButton("Start");
JPanel pane=new JPanel();
pane.add(start);
setVisible(true);
frame.add(start);
frame.add(pane);
/* setContentPane(Container)
JRootPane createRootPane()*/
}
public static void main (String []args){
new Benim();
}
}
My code is that. I tried adding to panel first then adding panel to frame, adding to frame directly. Adding a rootpane but still my button doesnot appear. I am trying to learn for 2 days but i am still at same point.
The instance of JFrame that is shown does not have the JButton added.
Instead invoke setVisible on the JFrame directly
You almost never want to extend JFrame as no new functionality is added
Other points to note
Call setVisible after components have been added
setSize is unnecessary - let pack determine container size
This is the result
public class Benim extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Concentration");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton start = new JButton("Start");
JPanel pane = new JPanel();
pane.add(start);
pane.add(start);
frame.add(pane);
frame.pack();
frame.setVisible(true);
}
});
}
}
Why another instance of JFrame? You are extending it, so just call super().
public class Benim extends JFrame {
Container contentArea = getContentPane ();
public Benim(){
super("Concentration");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setSize(800, 800);
JButton start=new JButton("Start");
JPanel pane=new JPanel();
pane.add(start);
add(pane);
setVisible(true);
}
public static void main (String []args){
new Benim();
}
}
Reimeus also rightfully points out that you don't need to extend JFrame if you don't plan on extending functionality. See his example for an alternative implementation.

Just imported my image, Need a sub panel on the right

Just imported an image, but need a subPanel on the right with the dimensions (200,700) I've tried using the imported image as a panel but it just spans me with errors, any ideas?
package dodge;
import java.awt.*;
import javax.swing.*;
public class Dodge extends JFrame {
private ImageIcon image;
private JLabel label;
Dodge(){
JFrame frame = new JFrame();
frame.setResizable(false);
frame.pack();
setLayout(new FlowLayout());
JPanel image = new JPanel();
image = new ImageIcon(getClass().getResource("Road.jpg"));
label = new JLabel (image);
add(label);
}
public static void main(String[] args) {
//
JFrame frame = new JFrame();
frame.setResizable(false);
frame.pack();
Dodge gui = new Dodge();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.pack();
gui.setTitle("Dodge The Cars");
JPanel subPanel1= new JPanel();
subPanel1.setBackground(Color.DARK_GRAY);
subPanel1.setPreferredSize(new Dimension (250,700));
JLabel label = new JLabel ("Menu");
subPanel1.add(label);
You have 3 JFrames in your code. You create a frame in the main method. Then you create a Dodge class which is a JFrame. Finally in the constructor of the Dodge class you create another frame.
I suggest your read the Swing tutorial on How to Use Icons for working examples that will show you how to better structure your program. Then it should be easier to solve your problem.

need to init the JFrame

I dont know how to init the JFrame windows. What I need to write to init im?
I have created at the main this:
Panel Panel=new Panel();
Panel.init();
JFrame frame = new JFrame("Shape Project");
frame.add(Panel);
frame.setResizable(false);
frame.setSize(new Dimension(1200, 650));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
and in the JPanel class I have write this:
public class Panel extends JFrame
{
public void init()
{
}
}
But when I active the frame it's does not active. What I need to write at the init func that the windows will open?
Try pack(); method of JFrame. If you are planning to develop with Swing, I recommend you to follow this tutorial:
http://download.oracle.com/javase/tutorial/uiswing/index.html
You already have a JFrame (frame). so now you should add components for your panel (you may do it in the main class as well). Such components are JTextField, JButton, etc. (and even another JPanel) each component you can add to the panel using panel.add(component_name); it is also recommended to follow the tutorial as Erkan mentioned.
Your panel class should extend JPanel, not JFrame.
You can add components to JPanel such as JButton, JList, etc
This is an example code you need to init a JFrame if you don't create own classes:
public class LogMain
{
public static void main(String[] args)
{
JFrame window = new JFrame("Log");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(300,300);
window.setResizable(false);
JPanel panel = new JPanel();
JButton openFile = new JButton("Btn1");
JButton openDir = new JButton("Btn2");
panel.add(openFile);
panel.add(openDir);
window.add(panel);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}
this is my example about the init of JFrame :
public class Windows{
public static void main(String args[]){
SJFrame window = new SJFrame("NEWNEWNEW");
window.init();
}
}
public class SJFrame extends JFrame(){
public SJFrame(String s){
super(s);
}
void init(){
Container panel = this.getContentPane();
panel.setBackground(Color.green);
panel.setLayout(new GridLayout(5,1));
JLabel jl1 = new JLabel("UserName");
JLabel jl2 = new JLabel("PassWord");
this.add(jl1);
this.add(jl2);
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE) ;
this.setSize(300,100);
this.pack();
this.setVisible(true);
}
}

Categories

Resources