Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-在调整JFrame大小时重新验证JFrame_Java_Swing_Jframe - Fatal编程技术网

Java-在调整JFrame大小时重新验证JFrame

Java-在调整JFrame大小时重新验证JFrame,java,swing,jframe,Java,Swing,Jframe,我试图在单击按钮时调整JFrame的大小,代码运行良好(但我不知道这是否是实现这一点的最佳方法) 但问题是: 调整大小时,JFrame会慢慢重新验证。GIF可以解释到底发生了什么: 这段代码是: chatButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { new Thread (new Runnabl

我试图在单击按钮时调整JFrame的大小,代码运行良好(但我不知道这是否是实现这一点的最佳方法)

但问题是: 调整大小时,JFrame会慢慢重新验证。GIF可以解释到底发生了什么:

这段代码是:

chatButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                new Thread (new Runnable() {
                    public void run(){
                        int width = frame.getWidth();
                        int height = frame.getHeight();
                        int buttonWidth = chatButton.getWidth();
                        if (frame.getWidth() < 1150) {  
                            while (frame.getWidth() < 1150) {
                                width = frame.getWidth();
                                frame.setSize(width + 2 , height);
                                chatButton.setLocation(width - buttonWidth , 0);
                                frame.invalidate();
                                frame.validate();
                            }
                        } else {
                            while (frame.getWidth() > 897) {
                                width = frame.getWidth();
                                frame.setSize(width - 2 , height);
                                chatButton.setLocation(width - buttonWidth , 0);
                                frame.invalidate();
                                frame.validate();
                            }
                        }   
                    }
                }).start(); 
            }
        });
chatButton.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0){
新线程(newrunnable()){
公开募捐{
int width=frame.getWidth();
int height=frame.getHeight();
int buttonWidth=chatButton.getWidth();
如果(frame.getWidth()<1150){
while(frame.getWidth()<1150){
宽度=frame.getWidth();
框架尺寸(宽度+2,高度);
设置位置(宽度-按钮宽度,0);
frame.invalidate();
frame.validate();
}
}否则{
while(frame.getWidth()>897){
宽度=frame.getWidth();
框架尺寸(宽度-2,高度);
设置位置(宽度-按钮宽度,0);
frame.invalidate();
frame.validate();
}
}   
}
}).start();
}
});
我把它放在了一个可运行状态,因为直到调整大小结束它才重新验证

我也尝试过
repaint()
revalidate()
但它们根本没有解决问题

我能做什么


提前感谢。

发生这种情况是因为您从非回转螺纹调整回转组件的尺寸。这将导致窗口的大小与组件的实际移动不同步。通过使内部代码在计时器上运行,tat每50毫秒激活一次,您可以在不紧张的情况下顺利打开

timer = new Timer(50, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        int width = frame.getWidth();
        int height = frame.getHeight();
        int buttonWidth = chatButton.getWidth();
        if(frame.getWidth() < 1150) {
            width = frame.getWidth();
            frame.setSize(width + 2 , height);
            chatButton.setLocation(width - buttonWidth , 0);
            frame.invalidate();
            frame.validate();
         } else {
            ((Timer)e.getSource()).cancel()
         } 
     });
timer.setInitialDelay(0);
timer.start(); 
timer=新定时器(50,新ActionListener(){
已执行的公共无效操作(操作事件e){
int width=frame.getWidth();
int height=frame.getHeight();
int buttonWidth=chatButton.getWidth();
if(frame.getWidth()<1150){
宽度=frame.getWidth();
框架尺寸(宽度+2,高度);
设置位置(宽度-按钮宽度,0);
frame.invalidate();
frame.validate();
}否则{
((计时器)e.getSource()).cancel()
} 
});
timer.setInitialDelay(0);
timer.start();

上面的代码是一个示例,当打开屏幕时,您需要执行与关闭相同的逻辑。

谢谢您的回答,但很抱歉,我找不到计时器的确切位置,我应该将其放在chatButton ActionListener中吗?还是其他地方?在检查窗口是否需要后,放在ActionListener中在边上增加,或者减少,一直给我一个错误:构造函数计时器(int,newactionlistener(){})你导入了正确的计时器吗?我们正在使用swing,所以你需要javax.swing.timer,它有一个合适的,你可以使用。嗯,你是对的。我检查了我的导入,我记得我需要导入java.util.timer来在屏幕上显示一个计时器,那么我如何导入这两个计时器呢?就我个人而言,我不确定你会找到一个com完全令人满意的解决方案。主要原因是,随着窗口大小的调整,内容并不总是立即更新,重绘管理器可能会将重复的更新调用减少到实际更新的数量,尝试手动调整窗口大小,以了解我的意思
chatButton.setLocation(width-buttonWidth,0);
这意味着一个空布局。Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作。在不同的地区使用不同的PLAF。因此,它们不利于像素完美的布局。相反,使用布局管理器,或连同布局填充和边框。进一步注意:循环部分应在使用Swing
计时器
在EDT上进行GUI更新。要更快获得更好的帮助,请发布或。