JFrame:setSize(int width, int height)

JFrame:setSize(int width, int height)

JFrame:setSize(int width, int height)
JFrame:setSize(int width, int height)  is a method that is used to set JFrame size.This method takes two integers which represent height and width.Other JFrame methods are setLocation,setJMenuBar etc.
Also JFrame window can implement WindowListener interface.

JFrame:setSize(int width, int height) Example

[
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Font;

import javax.swing.JPanel;
import javax.swing.JFrame;


public class JFrameSetSize  extends JPanel{

    public void paint(Graphics g)
    {
        Dimension d=this.getPreferredSize();
        int fontSize=15;
        Font f=new Font("TmesRoman",Font.BOLD,fontSize);
        g.setFont(f);
        g.setColor(Color.BLUE);
        g.drawString("I love Programming in Java", 20, 20);

    }

    public static void main(String[] args) {
        JFrame f = new JFrame("JFrame:setSize(int width, int height)");
        f.getContentPane().add(new JFrameSetSize());

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(300,300);
        f.setLocationRelativeTo(null);
        f.setVisible(true);

    }

}
]

Code Explanation

  • Code [JFrame f = new JFrame("JFrame:setSize(int width, int height)");] creates a new JFrame window with a tittle.
  • [ f.getContentPane().add(new JFrameSetSize());]-adds JPanel contents to the JFrame.
  • [ f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);]-sets Default close operation when the close button is clicked.
  • [f.setSize(300,300);]-sets JFrame window size.
  • [f.setLocationRelativeTo(null);]-positions JFrame window at the center of the screen.
  • [f.setVisible(true);]-makes the window visible.

Setting JFrame Window size using Dimension class.

[
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Font;

import javax.swing.JPanel;
import javax.swing.JFrame;


public class JFrameSetSize  extends JPanel{

    public void paint(Graphics g)
    {
        Dimension d=this.getPreferredSize();
        int fontSize=15;
        Font f=new Font("TmesRoman",Font.BOLD,fontSize);
        g.setFont(f);
        g.setColor(Color.BLUE);
        g.draw3DRect(100, 100,100, 100, false);
    }

    public static void main(String[] args) {
        JFrame f = new JFrame("JFrame:setSize(int width, int height)");
        f.getContentPane().add(new JFrameSetSize());

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(new Dimension(400,400));
        f.setLocationRelativeTo(null);
        f.setVisible(true);

    }

}
]

Output

JFrame:setSize(int width, int height)


Code Explanation

Above class extends JPanel Swing component and that the reason why we can access [public void paint(Graphics g)] method.
Below code creates a 3D rectangle object.

[public void paint(Graphics g)
    {
        Dimension d=this.getPreferredSize();
        int fontSize=15;
        Font f=new Font("TmesRoman",Font.BOLD,fontSize);
        g.setFont(f);
        g.setColor(Color.BLUE);
        g.draw3DRect(100, 100,100, 100, false);
    }]

[f.setSize(new Dimension(400,400));]-sets JFrame window size using Dimension class.

COMMENTS

Name

android arrays cnna1 graphics java java control statements java.swing JButton JFrame JLabel JTextField laravel node.js OPP questions swing technology
false
ltr
item
Code WHIZZ: JFrame:setSize(int width, int height)
JFrame:setSize(int width, int height)
JFrame:setSize(int width, int height)
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUY_XMcm2nSpMOzcwEmaVplEHjBJjbO8E4aWm5ToBftf0_s7C5QoGA3x7gUmeursuGCIjW6AD5Vvzwe60OwKPXkErdr2fyEBx7NmNYz5YryK8Jm3cAdhkADGmEraKSRuKb51HkxeHld1jS/s200/JFrame-setSize-example-min.PNG
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUY_XMcm2nSpMOzcwEmaVplEHjBJjbO8E4aWm5ToBftf0_s7C5QoGA3x7gUmeursuGCIjW6AD5Vvzwe60OwKPXkErdr2fyEBx7NmNYz5YryK8Jm3cAdhkADGmEraKSRuKb51HkxeHld1jS/s72-c/JFrame-setSize-example-min.PNG
Code WHIZZ
https://code-whizz.blogspot.com/2017/03/jframesetsizeint-width-int-height.html
https://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/2017/03/jframesetsizeint-width-int-height.html
true
5534598864497432585
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy