Java JFrame inner size - java

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.

Related

Adding many labels to JFrame

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

How to call the JFrame from another class?

I have a class that creates a frame.
public class GameDisplay{
....
public void createDisplay(){
frame=new JFrame(title);
canvas=new Canvas();
canvas.setPreferredSize(new Dimension(width,height));
canvas.setFocusable(false);
frame.setSize(width,height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
frame.add(canvas);
frame.pack();
}
public Canvas getCanvas(){
return this.canvas;
}
public JFrame getFrame(){
return frame;
}
If I have another class that would add Panels and Buttons to the frame, how can I add them?
I have tried:
GameDisplay g;
Container c;
c = g.getFrame().getContentPane();
But it returns NullPointer Error. Thus, I can't seem to add panels to it.
Attach your JFrame made in createDisplay() to a static variable. Then access that static variable from another class.
Like this
public static JFrame frame1;
Then in createDisplay()
GameDisplay.frame1 = frame;
In another class to get the content pane just do
c = GameDisplay.frame1.getContentPane();
Hope this helped!

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 !

Java Canvas doesn't resize correctly

I have to programm a game with the exact resolution of 128x128 but the Canvas dont want to match.
public class Window extends Canvas{
private static final long serialVersionUID = 1L;
private JFrame frame;
public Window(BufferedImage icon){
this.setMinimumSize(new Dimension(128, 128));
this.setMaximumSize(new Dimension(128, 128));
this.setPreferredSize(new Dimension(128, 128));
this.setSize(128, 128);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(this, BorderLayout.CENTER);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
if(icon != null){
frame.setIconImage(icon);
}
}}
The size of the Canvas (getWidth(), getHeight) is 134*128 instad of 128*128..
I would think getHeight would return the 134. That would. Be the title of the window. Is the Jframe obviously the bigger portion?
Don't call your class Window. There is an AWT component using that name so it is very confusing. Class names should be more descriptive.
Don't extend Canvas. When using Swing your would extend JComponent or JPanel for custom painting.
Don't create the frame in your class. The frame is not a property of the class and doesn't belong there.
I have to programm a game with the exact resolution of 128x128
Works fine for me on Windows.
Its definetly the getWidth(),
There is a minimum width of the frame. The frame must be able to paint all the buttons in the title bar and the left/right borders.
So I would guess that because you are using a BorderLayout, the frame is being resized to its minimum and then your custom class is resized based on the rules of the layout manager.
So if you want the preferred size of the panel to be respected, try a different layout manager on the frame. For example use a FlowLayout, it will respect the size of any component added to it.
Simple example:
import java.awt.*;
import javax.swing.*;
public class GamePanel extends Canvas
{
public GamePanel()
{
setBackground( Color.RED );
}
#Override
public Dimension getPreferredSize()
{
return new Dimension(128, 128);
}
private static void createAndShowGUI()
{
GamePanel panel = new GamePanel();
JFrame frame = new JFrame("GamePanel");
frame.setLayout( new FlowLayout() );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
System.out.println(panel.getSize());
System.out.println(frame.getSize());
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}

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