Container (JFrame)

Source: Internet
Author: User

[Java] // instance 1: directly define the Jframe class object to create a window package jFrame; import javax. swing. *; // to use the Swing class, you must introduce the Swing package public class JFrameDemo1 {public static void main (String args []) {// define a form object f, the form name is "a simple window" JFrame f = new JFrame (); f. setTitle ("instance 1: A simple window"); // you can specify the text in the title bar f. setLocationRelativeTo (null); // center the form to f. setSize (300,200); // set the size of the form to 300*200. setResizable (false); // sets whether the form can be adjusted. The parameter is a Boolean value. // sets the form to be visible. Without this statement, the form is invisible. This statement must exist, otherwise, f. setVisible (true); // The operation performed by the program when the user clicks the close button of the window. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); // close the window when the user clicks} [java // instance 2: Create class inherits JFrame class to create a window package JFrameDemo2; import javax. swing. *; // create a subclass MyFrame to inherit the parent class JFrame class MyFrame extends JFrame {// define the constructor without the MyFrame () {setLocationRelativeTo (null) parameter ); // set setTitle ("instance 1: A simple window") in the center of the form; // set the text setSize (300,200) in the title bar of the window ); // set the size of the form to 300*200. setResizable (false); // set whether the form can be adjusted. The parameter is a Boolean value of setVisible (true ); // set whether the form size can be adjusted. The parameter is a Boolean value setDefaultCloseOperation (EXIT_ON_CLOSE); // close the window when the user clicks} public class JFrameDemo2 {public static void main (String args []) {// instantiate a class object, prompting to set the parameter MyFrame f = new MyFrame () ;}} [java] // instance 3: No constructor is used, and the method can be the same as that of the instance, the code can be changed to the following mode: package JFrameDemo3; import javax. swing. *; class MyFrame extends JFrame {MyFrame () {}} public class JframeDemo3 {public static void main (String args []) {MyFrame f = new MyFrame (); f. setTitle ("instance 1: A simple window"); // you can specify the text in the title bar f. setLocationRelativeTo (null); // center the form to f. setSize (300,200); // set the size of the form to 300*200. setResizable (false); // sets whether the form can be adjusted. The parameter is a Boolean value. // sets the form to be visible. Without this statement, the form is invisible. This statement must exist, otherwise, f. setVisible (true); // The operation performed by the program when the user clicks the close button of the window. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); // close the window when the user clicks }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.