Java jframe design and minimize to the implementation of the system tray __java

Source: Internet
Author: User
Tags java jframe

The Java JFrame can implement a local instantiation window, by adding various controls to the jframe to achieve a variety of corresponding functions.

First, create a window instance

JFrame myframe = new JFrame ();

Ii. setting the various properties of JFrame

Myframe.setlayout (New BorderLayout ()); ---How to arrange the components in a window

Myframe.settitle ("window title"); --Set the title of the window

Myframe.setlocation (X,y); --Set the display position of the window

Myframe.setsize (x,y), setting the window size

Iii. examples

Package Monitorserver.control;

Import java.awt.AWTException;
Import Java.awt.BorderLayout;
Import Java.awt.Color;
Import Java.awt.FlowLayout;
Import Java.awt.MenuItem;
Import Java.awt.Panel;
Import Java.awt.PopupMenu;
Import Java.awt.SystemTray;
Import Java.awt.TextArea;
Import Java.awt.TrayIcon;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.event.MouseAdapter;
Import java.awt.event.MouseEvent;
Import Java.awt.event.WindowAdapter;
Import java.awt.event.WindowEvent;
Import java.io.IOException;
Import Java.net.ServerSocket;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;


Import Javax.swing.ImageIcon;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import javax.swing.SwingConstants;
Import Javax.swing.Timer;

Import MonitorServer.control.MServer;
Import MonitorServer.model.CheckLicense;

public class Mserver extends JFrame {

Private static final long serialversionuid = 1L;
private static int i = 0;
static int runTime = 0;
static TextArea ta = new TextArea ();
static Boolean regstatus = false;
private static TrayIcon TrayIcon = null;
static JFrame MF = new JFrame ();
Static Systemtray tray = Systemtray.getsystemtray ();

public static void MyFrame () {//form

Mf.setlocation (300, 100);
Mf.setsize (500, 300);
Mf.settitle ("xxxx system");
Mf.setlayout (New BorderLayout ());

ImageIcon ll = new ImageIcon ("C://tyjkdb//leida.gif")//Use picture in JFrame
JLabel i = new JLabel (ll);

JLabel j = new JLabel ("xxxx system v2.02", swingconstants.center);//Set label, show text centered
JLabel k = new JLabel ("Software development: Li Yonghai 2010.12,all Rights Reserved", swingconstants.center);/set label, show text centered
J.setfont (New Java.awt.Font ("", 1, 18));//Set label J Display Font

panel p1 = new Panel (); Materialized panel P1
P1.setlayout (New BorderLayout ());//The arrangement of controls in the Setup Panel P1

Final panel p11 = new Panel ();
P11.setlayout (New BorderLayout ()); Set P11 control arrangement
P11.add (J, Borderlayout.north); Display control above//P11 J
P11.add (k, borderlayout.south);//p11 Display control below K

Final JLabel t = new JLabel ("", Swingconstants.center); Set label T to display the clock
T.setfont (New Java.awt.Font ("", 1, 26)); Set Label T font
T.setforeground (Color.Blue);//Set label T foreground color

P11.add (t,borderlayout.center);//label T shown in middle position

Timer timer_date = new Timer (1000, new ActionListener () {Set digital clock
public void actionperformed (ActionEvent evt) {
T.settext (New SimpleDateFormat ("yyyy mm month DD Day HH:MM:SS"). Format (new Date ());

}
}
); Timer_date.start ();


P1.add (i, borderlayout.west);//Left icon
P1.add (P11, borderlayout.center);//p11 arranged in the middle of the P1

Mf.add (P1, Borderlayout.north);//show P1 above the window

Mf.add (TA, borderlayout.center); To display a multi-line text area in the middle of a stylistic

Mf.setvisible (TRUE);//Make window visible

Mf.addwindowlistener (New Windowadapter () {//Window Shutdown Event
public void windowclosing (WindowEvent e) {
System.exit (0);
};

public void windowiconified (WindowEvent e) {//Window Minimize Event

Mf.setvisible (FALSE);
Mserver.minitray ();

}

});

}

private static void Minitray () {//window minimized to taskbar tray

ImageIcon trayimg = new ImageIcon ("C://tyjkdb//leida.gif");//Pallet icon

PopupMenu pop = new PopupMenu (); Add Tray Right Click menu
MenuItem show = new MenuItem ("restore");
MenuItem exit = new MenuItem ("Exit");

Show.addactionlistener (new ActionListener () {

public void actionperformed (ActionEvent e) {//press restore key

Tray.remove (TrayIcon);
Mf.setvisible (TRUE);
Mf.setextendedstate (Jframe.normal);
Mf.tofront ();
}

});

Exit.addactionlistener (new ActionListener () {//Press exit key

public void actionperformed (ActionEvent e) {

Tray.remove (TrayIcon);
System.exit (0);

}

});

Pop.add (show);
Pop.add (exit);

TrayIcon = new TrayIcon (Trayimg.getimage (), "xxxx system", pop);
Trayicon.setimageautosize (TRUE);

Trayicon.addmouselistener (New Mouseadapter () {

public void mouseclicked (MouseEvent e) {//mouse double-click event

if (e.getclickcount () = = 2) {

Tray.remove (TrayIcon); Remove tray icon
Mf.setvisible (TRUE);
Mf.setextendedstate (Jframe.normal); Restore window
Mf.tofront ();
}

}

});

try {

Tray.add (TrayIcon);

catch (Awtexception E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}

}

public static void Main (string[] args) {

New Mserver ();
Mserver.myframe ();

}
}

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.