您的位置:首页 > 移动开发 > Android开发

JFrame最大化、最小化、关闭相关处理

2016-01-05 13:53 369 查看
一、去掉整个标题栏

在1.4以上的JDK中,只需要调用this.setUndecorated(false)即可不显示标题栏,调用之后最大化、最小化以及关闭按钮将不显示,需要自己实现这些功能,下例为去掉标题栏之后自己实现最大化、最小化、关闭的例子:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package programtest;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
*
* @author seadee
*/
public class MaxMinCloseFrame extends JFrame{
public final static String TAG = "ProgrameTest";

JButton closeBtn = null;
JButton maxBtn = null;
JButton minBtn = null;

public MaxMinCloseFrame(String host)
{
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setLayout(null);
SwingUtilities.updateComponentTreeUI(this);
setTitle("最大化最小化测试");
this.setName("最大化最小化测试");

setSize(700,600);
this.setLocationRelativeTo(null);//居中显示
//this.setResizable(false);
this.setUndecorated(true);
this.setAlwaysOnTop(true);

initComponent(host);

}

private void initComponent(String host)
{
closeBtn = new JButton("关闭");
closeBtn.setBounds(300, 500, 130, 40);
closeBtn.setHorizontalAlignment(JButton.CENTER);
closeBtn.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
//dispose();
System.exit(0);
//使用dispose();也可以关闭只是不是真正的关闭
}
});
this.add(closeBtn);

maxBtn = new JButton("最大化");
maxBtn.setBounds(300, 400, 130, 40);
maxBtn.setHorizontalAlignment(JButton.CENTER);
maxBtn.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

setExtendedState(JFrame.MAXIMIZED_BOTH);//最大化窗体

}
});
this.add(maxBtn);

minBtn = new JButton("最小化");
minBtn.setBounds(300, 300, 130, 40);
minBtn.setHorizontalAlignment(JButton.CENTER);
minBtn.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

setExtendedState(JFrame.ICONIFIED);//最小化窗体

}
});
this.add(minBtn);
}

public static void main(String[] args) {
new MaxMinCloseFrame("192.168.0.1").setVisible(true);
}

}


二、不去掉标题兰,屏蔽最小化、最大化、关闭中的一个或多个功能,方法为
监听窗口最大化最、最小化、关闭事件,然后做相应处理(setExtendedState(JFrame.NORMAL)),下面直接上代码:
package programtest;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
*
* @author seadee
*/
public class MaxMinCloseFrame extends JFrame{
public final static String TAG = "ProgrameTest";

public MaxMinCloseFrame(String host)
{
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setLayout(null);
SwingUtilities.updateComponentTreeUI(this);
setTitle("最大化最小化测试");
this.setName("最大化最小化测试");

setSize(700,600);
this.setLocationRelativeTo(null);//居中显示
this.setResizable(false);
this.setAlwaysOnTop(true);

initComponent(host);

}

private void initComponent(String host)
{
this.addWindowListener(new WindowAdapter() {

@Override
public void windowIconified(WindowEvent e) {
setExtendedState(JFrame.NORMAL);
}
});

}

public static void main(String[] args) {
new MaxMinCloseFrame("192.168.0.1").setVisible(true);
}

}

这里只是屏蔽了最小化操作,最大化要屏蔽的话,只需要将窗体设置为不可改变大小即可,即在代码中添加如下函数:
this.setResizable(false);
最大化最小化设置的方法还有很多,具体代码因人而异。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java android