Java 最小化 最大化

来源:百度知道 编辑:UC知道 时间:2024/05/23 22:59:31
我extends JFrame 后用setUndecorated(true);吧标题栏去了,我现在想在面板里加一个最小化按钮,请问怎么实现;

看看是不是这样的:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class FrameDemo extends JFrame implements ActionListener{
JButton btn1;
JButton btn2;
public FrameDemo() {
this.setBounds(100, 100, 300, 200);
btn1 = new JButton("最小化");
btn2 = new JButton("最大化");
btn1.addActionListener(this);
btn2.addActionListener(this);
this.add(btn1, BorderLayout.CENTER);
this.add(btn2, BorderLayout.SOUTH);
this.setVisible(true);

}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
this.setExtendedState(JFrame.ICONIFIED);
} else if (e.getSource() == btn2) {
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
public static void main(String[] args) {
new FrameDemo();
}<