Java Development GUI Tutorial JFrame listening for form size change events and jframe creating forms _java

Source: Internet
Author: User

Copy Code code as follows:

Import java.awt.event.WindowEvent;
Import Java.awt.event.WindowStateListener;

Import Javax.swing.JFrame;


public class WinFrame extends JFrame {

Public WinFrame () {
This.setname ("window state");
This.setsize (300,300);
This.setdefaultcloseoperation (Jframe.exit_on_close);
This.addwindowstatelistener (New Windowstatelistener () {

public void windowstatechanged (WindowEvent state) {

if (state.getnewstate () = 1 | | state.getnewstate () = 7) {
System.out.println ("window minimized");
}else if (state.getnewstate () = 0) {
System.out.println ("window back to initial state");
}else if (state.getnewstate () = 6) {
System.out.println ("Window maximization");
}
}
});
This.setvisible (TRUE);
}
public static void Main (string[] args) {
New WinFrame ();
}
}

Another small example of creating a form using JFrame

Windows created with JFrame contain a title, a minimized button, a Maximize button, and a close button

Copy Code code as follows:

public class Test () {
public static void Main (string[] args) {
JFrame frame = new JFrame ();
JPanel panel = new JPanel ();
JTextArea TextArea = new JTextArea ();

Panel.setlayout (New GridLayout ());
Textarea.settext ("test");
Generate scroll bars when content in textarea is too long
Panel.add (New JScrollPane (TextArea));
Frame.add (panel);

Frame.setsize (200,200);
Frame.setvisible (TRUE);
}
}

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.