<分区>
Possible Duplicate:
JButton minimizing a window(JFrame)
我删除了 JFrame 的关闭/最小化按钮,我想添加我自己的按钮,对于关闭,它可以通过使用 .dispose();
来工作,但是我应该使用什么来最小化JFrame 如果我点击一个 JButton?
我认为 .setVisible(false);
会完全隐藏它,我不会在任务栏中单击任何东西来取回 JFrame。
<分区>
Possible Duplicate:
JButton minimizing a window(JFrame)
我删除了 JFrame 的关闭/最小化按钮,我想添加我自己的按钮,对于关闭,它可以通过使用 .dispose();
来工作,但是我应该使用什么来最小化JFrame 如果我点击一个 JButton?
我认为 .setVisible(false);
会完全隐藏它,我不会在任务栏中单击任何东西来取回 JFrame。
最佳答案
只需使用.setState(Frame.ICONIFIED)
工作示例:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FrameTest {
public static void main(String args[]) throws Exception {
final JFrame frame = new JFrame();
frame.setUndecorated(true);
JButton button = new JButton("Minimize");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setState(Frame.ICONIFIED);
}
});
frame.add(button);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
关于java - 如何通过单击 JButton 最小化 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12388297/