如何确定 JFrame 被移动的原因(Windows、JDK6)
import java.awt.Dimension;
import java.awt.event.ComponentListener;
import java.awt.event.ComponentEvent;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
public class FrmMain extends JFrame implements ComponentListener
{
FrmMain()
{
super("Moves & Resize");
setMinimumSize(new Dimension(512,384));
setDefaultCloseOperation(EXIT_ON_CLOSE);
addComponentListener(this);
setVisible(true);
}
public void componentMoved( ComponentEvent ev )
{
int state = getExtendedState();
if( (state&JFrame.MAXIMIZED_BOTH)==JFrame.MAXIMIZED_BOTH )
{
System.out.println("Moved, maximized");
} else {
System.out.println("Moved, not maximized");
}
}
public void componentResized( ComponentEvent ev )
{
int state = getExtendedState();
if( (state&JFrame.MAXIMIZED_BOTH)==JFrame.MAXIMIZED_BOTH )
{
System.out.println("Resized, maximized");
} else {
System.out.println("Resized, not maximized");
}
}
public void componentShown( ComponentEvent ev )
{
}
public void componentHidden( ComponentEvent ev )
{
}
public static void main( String[] args )
{
SwingUtilities.invokeLater(
new Runnable() {
public void run()
{
FrmMain mainwin = new FrmMain();
}
}
);
}
}
我想确定框架是否由于最大化而被调整大小/移动,或者只是被用户拖动。令人惊讶的是,上面的代码输出(在我按下框架上的“最大化”按钮后):
Moved, not maximized
Resized, maximized
这意味着,窗口状态的更新发生在调用 componentMoved
之后和调用 componentResized
之前。那么,我如何知道在 componentMoved
事件中窗口是否被最大化?
import java.awt.Dimension;
import java.awt.event.ComponentListener;
import java.awt.event.ComponentEvent;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
public class FrmMain extends JFrame implements ComponentListener
{
FrmMain()
{
super("Moves & Resize");
setMinimumSize(new Dimension(512,384));
setDefaultCloseOperation(EXIT_ON_CLOSE);
addComponentListener(this);
setVisible(true);
}
public void componentMoved( ComponentEvent ev )
{
int state = getExtendedState();
if( (state&JFrame.MAXIMIZED_BOTH)==JFrame.MAXIMIZED_BOTH )
{
System.out.println("Moved, maximized");
} else {
System.out.println("Moved, not maximized");
}
}
public void componentResized( ComponentEvent ev )
{
int state = getExtendedState();
if( (state&JFrame.MAXIMIZED_BOTH)==JFrame.MAXIMIZED_BOTH )
{
System.out.println("Resized, maximized");
} else {
System.out.println("Resized, not maximized");
}
}
public void componentShown( ComponentEvent ev )
{
}
public void componentHidden( ComponentEvent ev )
{
}
public static void main( String[] args )
{
SwingUtilities.invokeLater(
new Runnable() {
public void run()
{
FrmMain mainwin = new FrmMain();
}
}
);
}
}
I want to determine whether the frame was resized/moved due to being maximized, or simply dragged around by user. Surprisingly, the code above outputs (after I press "maximize" button on the frame):
Moved, not maximized
Resized, maximized
That means, the update of window state happens after calling componentMoved
and before calling componentResized
. So, how do I find out, if the window is being maximized, inside componentMoved
event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用:
如MAXIMIZED_BOTH是水平和垂直最大化的两位。
在 JSE 7 下,我确实得到了“感动、最大化”。
我不会篡改移动和调整大小事件的顺序。尝试重新实现关闭框也是一个坏主意。
您可以在“移动”上发布您自己的事件,该事件将在调整大小的事件之后发布。
Use:
as MAXIMIZED_BOTH is two bits of horizontally and vertically maximized.
Under JSE 7 I do get "moved, maximized."
I would not tamper with the order of moved and resized event. Trying to reimplement the close box is a bad idea too.
You could post your own event on moved, which then would come after the resized event.