.getSize()未更新

时间:2012-07-13 12:28:17

标签: java swing user-interface jpanel

我需要能够在事件函数中更改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.Dimension中[宽度= 200,高度= 200]

java.awt.Dimension中[宽度= 200,高度= 200]

我希望它打印

java.awt.Dimension中[宽度= 200,高度= 200]

java.awt.Dimension中[宽度= 100,高度= 100]

2 个答案:

答案 0 :(得分:7)

您的代码适用于我,但仅适用于鼠标滚轮事件,您必须使用鼠标滚轮滚动

修改示例

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()在这种情况下是最好的,因为在EDT中完成所有事件后,容器返回已更改Dimension

例如

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]

答案 1 :(得分:-2)

你是否也需要调用paint()来改变大小?