gpt4 book ai didi

java - 当 setResizable 为 false 时,窗口在 JFrame 上的 setSize 之后移动

转载 作者:行者123 更新时间:2023-11-29 04:46:10 26 4
gpt4 key购买 nike

当我查看 Frame.setResizable 的文档时,我读到:

Sets whether this frame is resizable by the user.

所以我知道我仍然可以调用它的 setSize 以编程方式调整它的大小。但如果我这样做,框架就会向下移动! (至少在 Ubuntu、Unity 下)。如果我希望框架与内容的大小相匹配,这尤其令人讨厌。例如,只需尝试以下 SSCCE:

package test;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class Test2 {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final JFrame mainFrame = new JFrame("test");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setResizable(false);
final JTextArea field = new JTextArea() {
public Dimension getPreferredSize() {
Dimension pref = super.getPreferredSize();
return new Dimension(Math.max(50, pref.width),Math.max(20, pref.height));
}
};
((AbstractDocument)field.getDocument()).setDocumentFilter(new DocumentFilter() {
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
super.replace(fb, offset, length, text, attrs);
System.out.println("before "+mainFrame.getLocation().y);
mainFrame.pack();
System.out.println("after "+mainFrame.getLocation().y+"\n");
}
});
mainFrame.getContentPane().setLayout(new BorderLayout());
mainFrame.getContentPane().add(field,BorderLayout.CENTER);
mainFrame.pack();
mainFrame.setVisible(true);
}
});
}

}

键入一些字符使窗口扩展。至少在 Unity 上,窗口会向下移动屏幕...我不知道如何解决这个问题。

更新:

以上代码运行结果如下:

before 52
after 52

before 80
after 108

before 108
after 108

before 136
after 136

before 164
after 164

before 192
after 192
...

最后,我不知道框架何时被移动...

如果我在我的框架上添加一个 ComponentListener,这是我可以获得的堆栈跟踪:

at test.Test2$1$3.componentMoved(Test2.java:50)
at java.awt.AWTEventMulticaster.componentMoved(AWTEventMulticaster.java:169)
at java.awt.AWTEventMulticaster.componentMoved(AWTEventMulticaster.java:169)
at java.awt.Component.processComponentEvent(Component.java:6345)
at java.awt.Component.processEvent(Component.java:6296)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Window.processEvent(Window.java:2022)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:77)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:77)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

它的行为就像我手动移动框架一样。调整框架大小时,可能是 Unity 在执行某些操作。

最佳答案

just try the following SSCCE :

它不是 SSCCE(接近但不完全)。没有 main() 方法,也没有导入。我们应该能够不假思索地复制/粘贴/编译/测试。

Type some characters to make the window extend

在 Windows 7 上使用 JDK8 对我来说效果很好。

//mainFrame.setSize(mainFrame.getContentPane().getPreferredSize());
mainFrame.pack();

我会使用上面的代码。您将获得更好的框架尺寸,因为 pack() 将考虑框架装饰,这与 setSize() 方法不同。

如果 pack() 没有解决问题,那么我猜建议是先保存框架的位置,然后调用 setLocation(...) 方法,或使用 setBounds(...) 来完全控制大小/位置。

编辑:

In the end, I don't get when the Frame is being moved...

尝试这样的事情:

((AbstractDocument)field.getDocument()).setDocumentFilter(new DocumentFilter()
{
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException
{
Point location = mainframe.getLocation();

super.replace(fb, offset, length, text, attrs);

mainFrame.pack();

SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
mainframe.setLocation( location );
}
});
}
});

关于java - 当 setResizable 为 false 时,窗口在 JFrame 上的 setSize 之后移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37037556/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com