gpt4 book ai didi

java - .getSize() 未更新

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:53:14 25 4
gpt4 key购买 nike

我需要能够在事件函数中更改 JPanel 的大小,然后再次获取大小。似乎 JPanel 在函数调用完成之前不会更新。我怎样才能得到真正的尺寸? 这是一个 SSCCE:

import java.awt.*;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.*;


public class Test extends JFrame implements MouseWheelListener{
JPanel p;
Test(){
setLayout(new FlowLayout());
setPreferredSize(new Dimension(1000,1000));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);

p = new JPanel();
p.setPreferredSize(new Dimension(200,200));
p.setBackground(Color.red);
add(p);

addMouseWheelListener(this);
pack();
}
public static void main(String args[]){
new Test();
}
public void mouseWheelMoved(MouseWheelEvent e) {

System.out.println(p.getSize());

p.setPreferredSize(new Dimension(100,100));
p.revalidate();

System.out.println(p.getSize());
}
}

代码工作正常,但如果我将鼠标滚动一步,它会在控制台中打印以下内容:

java.awt.维度[width=200,height=200]

java.awt.维度[width=200,height=200]

我要打印:

java.awt.维度[width=200,height=200]

java.awt.维度[width=100,height=100]

最佳答案

你的代码对我有用,但只适用于鼠标滚轮事件,你必须用鼠标滚轮滚动

修改示例

import java.awt.*;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.*;

public class Test extends JFrame implements MouseWheelListener {

private static final long serialVersionUID = 1L;
private JPanel p;

public Test() {
setLayout(new FlowLayout());
setPreferredSize(new Dimension(1000, 1000));
setDefaultCloseOperation(EXIT_ON_CLOSE);

p = new JPanel();
p.setPreferredSize(new Dimension(200, 200));
p.setBackground(Color.red);
add(p);
addMouseWheelListener(this);
pack();
setVisible(true);
}

public void mouseWheelMoved(MouseWheelEvent e) {
Dimension dim100 = new Dimension(100, 100);
Dimension dim200 = new Dimension(200, 200);
System.out.println(p.getSize());
if (p.getPreferredSize().equals(dim100)) {
p.setPreferredSize(dim200);
p.revalidate();
System.out.println(p.getSize());
} else if (p.getPreferredSize().equals(dim200)) {
p.setPreferredSize(dim100);
p.revalidate();
System.out.println(p.getSize());
}
}

public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
new Test();
}
});
}
}

编辑:

必须延迟 System.out.println(p.getSize());,invokeLater() 在这种情况下是最好的,因为容器在所有事件完成后返回更改的维度美国东部时间

例子

import java.awt.*;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.*;

public class Test extends JFrame implements MouseWheelListener {

private static final long serialVersionUID = 1L;
private JPanel p;

public Test() {
setLayout(new FlowLayout());
setPreferredSize(new Dimension(1000, 1000));
setDefaultCloseOperation(EXIT_ON_CLOSE);
p = new JPanel();
p.setPreferredSize(new Dimension(900, 900));
p.setBackground(Color.red);
add(p);
addMouseWheelListener(this);
pack();
setVisible(true);
}

public void mouseWheelMoved(MouseWheelEvent e) {
Dimension dim100 = p.getSize();
System.out.println("before "+ p.getSize());
p.setPreferredSize(new Dimension(dim100.height - 5, dim100.width - 5));
p.revalidate();
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
System.out.println("after "+ p.getSize());
}
});
}

public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
new Test();
}
});
}
}

生成

before java.awt.Dimension[width=900,height=900]
after java.awt.Dimension[width=895,height=895]
before java.awt.Dimension[width=895,height=895]
after java.awt.Dimension[width=890,height=890]
before java.awt.Dimension[width=890,height=890]
after java.awt.Dimension[width=885,height=885]
before java.awt.Dimension[width=885,height=885]
after java.awt.Dimension[width=880,height=880]
before java.awt.Dimension[width=880,height=880]
after java.awt.Dimension[width=875,height=875]
before java.awt.Dimension[width=875,height=875]
after java.awt.Dimension[width=870,height=870]
before java.awt.Dimension[width=870,height=870]
after java.awt.Dimension[width=865,height=865]
before java.awt.Dimension[width=865,height=865]
after java.awt.Dimension[width=860,height=860]

关于java - .getSize() 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11470530/

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