java改变窗体状态(最⼤化、最⼩化、正常)
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MyFrame extends JFrame implements ActionListener {
private static final long serialVersionUID = 1026416994451303162L;
private static MyFrame mf = null;
JButton jb1 = new JButton("最⼤化");
JButton jb2 = new JButton("最⼩化");
JButton jb3 = new JButton("正常");
public static MyFrame getInstance() {
if (mf == null)
mf = new MyFrame();
return mf;
}
public MyFrame() {
super("改变窗体测试");
init();
systemTray();
}
private void init() {
this.add(jb1, BorderLayout.NORTH);
this.add(jb2, BorderLayout.SOUTH);
this.add(jb3, BorderLayout.CENTER);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
this.setSize(300, 120);
Dimension d = DefaultToolkit().getScreenSize();
int h = (int) d.getHeight() / 4;
int w = (int) d.getWidth() / 4;
this.setLocation(w + 150, h + 100);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);  this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
}
});
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if (str.equals("最⼤化")) {
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
if (str.equals("最⼩化")) {
this.setExtendedState(JFrame.ICONIFIED);
System.out.println("最⼩化");
}
if (str.equals("正常")) {
this.setExtendedState(JFrame.NORMAL);
}
if(str.equals("m2")){
this.setExtendedState(JFrame.NORMAL);
}
}
public void systemTray() {
try {
if (java.awt.SystemTray.isSupported()) {// 判断当前平台是否⽀持系统托盘
java.awt.SystemTray st = java.SystemTray();
Image image = DefaultToolkit().getImage(
getClass().getResource("0.gif"));// 定义托盘图标的图⽚
java.awt.TrayIcon ti = new java.awt.TrayIcon(image);
ti.setToolTip("改变窗体测试");
PopupMenu p=new PopupMenu ("OK");
MenuItem m=new MenuItem("退出");
m.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
}
});
p.add(m);
MenuItem m1=new MenuItem("打开");
m1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
}
});
p.add(m1);
MenuItem m2=new MenuItem("最⼩化");
m2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
}
});
p.add(m2);
ti.setPopupMenu(p); // 为托盘添加右键菜单
st.add(ti);
}
} catch (Exception e) {htmlborder
}
}
public static void main(String[] args) {
try {
UIManager.SystemLookAndFeelClassName());  Instance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。