如何在JScrollPane中获取(可更改的)组件的完整大小?

时间:2015-12-30 18:16:00

标签: java size components jscrollpane

我的代码是这样的:

JFrame frame = new JFrame();

JPanel panel = new JPanel();

JTextPane text = new JTextPane();
text.setPreferredSize(new Dimension(200,260));
text.setContentType("text/html");
text.setEditable(false);
text.setText("some random thing makes this pane bigger than its perferred size");

JScrollPane scroll = new JScrollPane(text);

panel.add(scroll);

frame.add(panel);
frame.setVisible(true);
frame.pack();
frame.validate();

我的问题是我不知道如何获得JTextPane text )的真实大小。我已经尝试了所有getSizes& getVisibleRectsscroll.getViewport().getViewSize() ...但他们都返回[200,260]。有人可以帮我解决这个问题吗?我非常感谢您提供的任何帮助。

编辑:我发现了问题:我的getSizes请求在验证帧之前执行了。 我的可运行测试代码:

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

public class Abcd {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame frame = new JFrame();

    JPanel panel = new JPanel();

    JTextPane text = new JTextPane();
    text.setPreferredSize(new Dimension(150,126));
    text.setContentType("text/html");
    text.setEditable(false);

    //Alicia Vikander's photo. She's cute anw.
    String tmp = "<a href=\"https://upload.wikimedia.org/wikipedia/commons/thumb/b/b2/Alicia_Vikander.jpg/220px-Alicia_Vikander.jpg\"><img src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/b/b2/Alicia_Vikander.jpg/220px-Alicia_Vikander.jpg\"/></a>";
    //text.setText("asdf fssdfsdf sfdsdf sd sdf sfasdf fssdfsdf sfdsdf sd sdf sfasdf fssdfsdf sfdsdf sd sdf sfasdf fssdfsdf sfdsdf sd sdf sfasdf fssdfsdf sfdsdf sd sdf sfasdf fssdfsdf sfdsdf sd sdf sfasdf fssdfsdf sfdsdf sd sdf sfasdf fssdfsdf sfdsdf sd sdf sfasdf fssdfsdf sfdsdf sd sdf sfasdf fssdfsdf sfdsdf sd sdf sfasdf fssdfsdf sfdsdf sd sdf sfasdf fssdfsdf sfdsdf sd sdf sfasdf fssdfsdf sfdsdf sd sdf sf");
    text.setText(tmp);
    JScrollPane scroll = new JScrollPane(text);
    scroll.setPreferredSize(new Dimension(150,126));

    panel.add(scroll);

    frame.add(panel);
    frame.setVisible(true);
    frame.pack();
    frame.validate();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    for (double i = 0; i < 1000; i = i +  0.0001){
        System.out.print(text.getHeight()+"\n");            
        if (text.getHeight()!=123){   //3 pixels for the scrollpane's border i guessed
            System.out.print("i = "+i+", height = "+text.getHeight());
            break;
        }
    }
}

在我的电脑中i大约是50 - 100.所以我的新问题是:无论如何,我可以尽快得到实物,但仍能确保框架有效吗? 非常感谢。

1 个答案:

答案 0 :(得分:0)

我强烈建议您开始使用不同的Layout managers

如果我将你的代码调整一下:

JFrame frame = new JFrame();

final JTextPane text = new JTextPane();
text.setContentType("text/html");
text.setEditable(false);
text.setText("some random thing makes this pane bigger than its perferred size");

final JScrollPane scroll = new JScrollPane(text);
scroll.addComponentListener(new ComponentAdapter()  {
        @Override
        public void componentResized(ComponentEvent e) {
            System.out.println("Size of JScrollPane is: " + scroll.getSize());
            System.out.println("Size of JTextPane is: " + text.getSize());
        }
});

frame.setLayout(new BorderLayout());
frame.add(scroll,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);

我们将使用滚动组件的大小调用其scroll.getSize()以及调用其text.getSize()

的文本组件的大小