[英]when a JFrame is minimized setState(JFrame.ICONIFIED) but where to write to code of event when user restores it from taskbar
当用户单击最小化时,我的程序将两个框架最小化,并且它们在任务栏中的显示均不同,但是当用户在任务栏中单击它们中的任何一个时,我希望将它们还原为在屏幕上查看。
Two JFrame
's are in Frame.ICONIFIED
state (minimized) - when one of them is restored to Frame.NORMAL
state the other also gets the normal state. 两个JFrame
处于Frame.ICONIFIED
状态(最小化)-当其中一个恢复到Frame.NORMAL
状态时,另一个Frame.NORMAL
也恢复为正常状态。 This is achieved by implementing the java.awt.event.WindowStateListener
for both the frames. 这是通过对两个框架都实现java.awt.event.WindowStateListener
来实现的。 Here is the demo program: 这是演示程序:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TwoJframes implements WindowStateListener {
private JFrame frame;
private JFrame frame2;
public static void main(String [] args) {
new TwoJframes().gui();
}
private void gui() {
frame = new JFrame();
frame.setTitle("JFrame 1");
JButton button = new JButton("Show Frame 2");
button.addActionListener(e -> showAnotherFrame());
frame.addWindowStateListener(this);
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(250, 100);
frame.setVisible(true);
}
private void showAnotherFrame() {
frame2 = new JFrame();
frame2.setTitle("JFrame 2");
frame2.addWindowStateListener(this);
frame2.add(new JLabel("2nd frame"));
frame2.setSize(250, 100);
frame2.setLocation(300, 300);
frame2.setVisible(true);
}
// This is the WindowStateListener's implementation method
@Override public void windowStateChanged(WindowEvent e) {
JFrame f = (JFrame) e.getSource();
if (f == frame) {
if ((frame.getExtendedState() == Frame.NORMAL) &&
(frame2.getExtendedState() == Frame.ICONIFIED)) {
frame2.setExtendedState(Frame.NORMAL);
}
}
if (f == frame2) {
if ((frame2.getExtendedState() == Frame.NORMAL) &&
(frame.getExtendedState() == Frame.ICONIFIED)) {
frame.setExtendedState(Frame.NORMAL);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.