java windowiconified_Java JFrame 设计视图 以及最小化到系统托盘和还原

这篇博客介绍了如何使用Java JFrame创建窗口,并实现窗口的最小化到系统托盘以及从托盘还原窗口的功能。通过设置窗口的基本属性,如布局、大小、位置、标题和图标,然后利用WindowListener监听窗口事件,实现窗口的状态管理。在窗口关闭时,如果系统托盘支持,窗口将最小化到托盘,并提供还原和退出的选项。
摘要由CSDN通过智能技术生成

Java

JFrame可以实现本地实例化窗口,通过向JFrame添加各种控件,实现各种相应的功能。

一、创建窗口实例

JFrame myFrame = new JFrame();

二、设置JFrame的各种属性

myFrame.setLayout(new BorderLayout());

---设置窗口中组件的排列方式

myFrame.setTitle("窗口标题"); --设置窗口的标题

myFrame.setLocation(x,y); --设置窗口的显示位置

myFrame.setSize(x,y);设置窗口大小

三、实例

import

java.awt.AWTException;

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.awt.Image;

import java.awt.MenuItem;

import java.awt.PopupMenu;

import java.awt.SystemTray;

import java.awt.Toolkit;

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.WindowEvent;

import java.awt.event.WindowListener;

import

javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

public class MyQQJFrame extends

JFrame implements WindowListener{

private Toolkit toolkit;

//窗口起点

private int x;

private int y;

//窗口大小

private int height;

private int width;

private JPanel jp;

private JPanel jp1;

private JPanel jp2;

private JPanel jp3;

public int

getX() {

return x;

}

public void

setX(int x) {

this.x = x;

}

public int

getY() {

return y;

}

public void

setY(int y) {

this.y = y;

}

public int

getHeight() {

return height;

}

public void

setHeight(int height) {

this.height = height;

}

public int

getWidth() {

return width;

}

public void

setWidth(int width) {

this.width = width;

}

public void

init(){

//设置窗口基本属性(大小,位置,标题,图标等)

setBasicProperty();

//生成四个面板。 newJpanel(); //设置主面板为Flowlayout布局

setMajorPanel(); //设置jp1面板为Flowlayout布局,并实现其详细布局

setJp1();

//设置jp2面板为Gridlayout (3*3)

布局

setJp2();

//设置jp3面板为flowlayout布局。

setJp3();

//设置中间3*3 的 详细布局

Center();

//底层 的详细布局

South();

//把jp1,jp2,jp3,三个面板放入主面板jp中。

addJPanel(); this.addWindowListener(this);

//设置窗口可见性

this.setVisible(true);

}

protected void

newJpanel() {

//new 4个面板

,jp为主面板,jp1,jp2,jp3,依次为放置在jp面板上方,中间,底层的三个面板

jp=new JPanel();

jp1=new JPanel();

jp2=new JPanel();

jp3=new JPanel();

}

protected void

setBasicProperty() {

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//设置大小

this.setX(400);

this.setY(250);

this.setSize(this.getX(), this.getY());

//设置自定义居中显示

this.setCenterLocation();

//设置标题

this.setTitle("QQ");

//设置图标

this.setIconImage(new

ImageIcon("Image_src\\QQ.jpg").getImage());

}

protected void

setJp3() {

FlowLayout flowLayout=new

FlowLayout();

flowLayout.setHgap(35);

jp3.setLayout(flowLayout);

}

protected void

setJp2() {

GridLayout g2=new GridLayout(3,

3);

g2.setHgap(5);

g2.setVgap(5);

jp2.setLayout(g2);

}

protected void

setJp1() {

JLabel jl=new JLabel();

jl.setIcon(new

ImageIcon("Image_src\\QQtitle.jpg"));

jp1.add(jl);

}

protected void

setMajorPanel() {

// BorderLayout

BorderLayout1=new BorderLayout();

// BorderLayout1.setVgap(5);

// this.setLayout(BorderLayout1);

this.setContentPane(jp);

}

protected void

addJPanel() {

jp.add(jp1,

BorderLayout.NORTH);

jp.add(jp2,

BorderLayout.CENTER);

jp.add(jp3,

BorderLayout.SOUTH);

}

protected void

South() {

//把3个控件依次加入jp3面板

jp3.add(new

JButton("登陆"));

jp3.add(new

JButton("取消"));

jp3.add(new

JButton("注册向导"));

}

protected void

Center() {

JLabel j21=new

JLabel(); j21.setText("QQ号码");

JLabel j24=new

JLabel(); j24.setText("QQ密码");

JTextField jtf=new

JTextField();

JPasswordField jpf=new

JPasswordField(7);

//把9个控件依次加入jp2面板

jp2.add(j21);

jp2.add(jtf);

jp2.add(new

JButton("申请号码"));

jp2.add(j24);

jp2.add(jpf);

jp2.add(new

JButton("忘记密码")); jp2.add(new

JCheckBox("隐身登陆"));

jp2.add(new

JCheckBox("记住密码"));

jp2.add(new

JButton("申请密码保护"));

}

private void

setCenterLocation() {

this.toolkit=Toolkit.getDefaultToolkit();

Dimension

dimension=toolkit.getScreenSize();

this.width=(int)dimension.getWidth();

this.height=(int)dimension.getHeight();

this.width=(this.width-this.x)/2;

this.height=(this.height-this.y)/2;

this.setLocation(this.width,

this.height);

}

@Override

public void windowOpened(WindowEvent e) {

System.out.println("windowOpened 窗口首次变为可见时调用。");

}

@Override

public void windowClosing(WindowEvent e) {

System.out.println("windowClosing

用户试图从窗口的系统菜单中关闭窗口时调用。");

System.out.println(e.paramString());

}

@Override

public void windowClosed(WindowEvent e) {

System.out.println("windowClosed 因对窗口调用 dispose 而将其关闭时调用。");

}

@Override

public void windowIconified(WindowEvent e)

{

System.out.println("windowIconified 窗口从正常状态变为最小化状态时调用。");

if(SystemTray.isSupported()){

final

SystemTray tray=SystemTray.getSystemTray();

Image

image=new ImageIcon("Image_src\\27.jpg").getImage();

// MouseListener1

ml=new MouseListener1();

//System.out.println(e.getSource().toString());

PopupMenu pop = new PopupMenu(); //增加托盘右击菜单

MenuItem show = new MenuItem("还原");

MenuItem exit = new MenuItem("退出");

final TrayIcon trayIcon=new TrayIcon(image, "Tray Demo",

pop);

show.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { // 按下还原键

tray.remove(trayIcon);

MyQQJFrame.this.setVisible(true);

MyQQJFrame.this.setExtendedState(JFrame.NORMAL);

MyQQJFrame.this.toFront();

}

});

exit.addActionListener(new ActionListener() { // 按下退出键

public void actionPerformed(ActionEvent e) {

tray.remove(trayIcon);

System.exit(0);

}

});

pop.add(show);

pop.add(exit);

trayIcon.setImageAutoSize(true);

trayIcon.addMouseListener(new

MouseAdapter() {

public void mouseClicked(MouseEvent e) { // 鼠标器双击事件

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

tray.remove(trayIcon); // 移去托盘图标

MyQQJFrame.this.setVisible(true);

MyQQJFrame.this.setExtendedState(JFrame.NORMAL); // 还原窗口

MyQQJFrame.this.toFront();

}

}

});

try {

tray.add(trayIcon);

} catch

(AWTException e1) {

//

TODO Auto-generated catch block

e1.printStackTrace();

}

}

}

public void windowDeiconified(WindowEvent e)

{

System.out.println("windowDeiconified 窗口从最小化状态变为正常状态时调用。 ");

}

public void windowActivated(WindowEvent e)

{

System.out.println("windowActivated

将 Window 设置为活动 Window 时调用。");

}

public void windowDeactivated(WindowEvent e)

{

System.out.println("windowDeactivated 当 Window 不再是活动 Window 时调用。");

}

}

TestMyQQJFrame类:

public class TestMyQQJFrame {

public static void main(String[] args) {

MyQQJFrame qq=new

MyQQJFrame();

qq.init();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值