3

我需要能够在事件函数中更改 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]

4

2 回答 2

6

你的代码对我有用,但只在鼠标滚轮事件上工作,你必须用鼠标滚轮滚动

修改示例

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]
于 2012-07-13T12:59:31.123 回答
-2

你不需要调用paint() 来改变大小吗?

于 2012-07-13T12:38:13.267 回答