getPreferredSize()适用于JFrame;为什么它不适用于Frame?

时间:2016-05-28 14:14:52

标签: java swing preferredsize

我希望能帮助解决一个令我困惑的问题。

我在getPreferredSize()Frame上测试了Frame。在Frame中,getPreferredSize()返回全0(宽度和高度),而在Frame中,它返回非零数字,这是有效的。

我不确定nolayout()方法中我的编码的哪一部分,getPreferredSize()只返回0,isPreferredSizeSet()返回FALSE,这意味着未设置首选大小

为什么不设置?有人可以帮忙吗?感谢。

以下是这个简单测试程序的编码。

import java.awt.*;
import javax.swing.*;

public class test {
public test(){
    JFrame frame = new JFrame("AbsoluteLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container p = frame.getContentPane();
    p.setLayout(null);
    JButton b1 = new JButton("one");
    p.add(b1);
    Dimension size = b1.getPreferredSize();
    System.out.println("size width is "+size.width+" height is "+size.height);
    JLabel label2 = new JLabel("Text-Only Label");
    p.add(label2);
    size = label2.getPreferredSize();
    System.out.println("label size width is "+size.width+" height is "+size.height);

    Insets insets = p.getInsets(); 
    frame.setSize(300 + insets.left + insets.right, 125 + insets.top + insets.bottom);
    frame.setVisible(true);
}
public static void main(String[] args) {
    test t = new test();
    nolayout();
}enter code here



public static void nolayout() {

    Frame f = new Frame();
    f.setLayout(null);

    Insets its = f.getInsets();

    System.out.println("its left, "+its.left+" right, "+its.right+" top "+its.top+" bottom "+its.bottom);

    f.setLocation(200, 100 );
    f.setSize(its.left+its.right+300,its.top+its.bottom+200);

    Label l1 = new Label("Enter Integer to be added : ",Label.RIGHT);
    TextField t1 = new TextField("0",10);

    f.add(l1);
    f.add(t1);


    System.out.println("is l1 preferred size set "+l1.isPreferredSizeSet());
    Dimension sz = l1.getPreferredSize();
    l1.setBounds(its.left + 10, its.top + 10, sz.width, sz.height);
    System.out.println("label 1 width "+sz.width+" height " +sz.height);

    sz = t1.getPreferredSize();
    t1.setBounds(its.left + 100, its.top + 10, sz.width, sz.height);
    System.out.println("Text 1 width "+sz.width+" height " +sz.height);

    f.setVisible(true);
    }
} 

命令窗口的输出如下:

C:\JAVAPR>java test
size width is 55 height is 26
label size width is 88 height is 16
its left, 0 right, 0 top 0 bottom 0
is l1 preferred size set false
label 1 width 0 height 0
Text 1 width 0 height 0 enter code here

1 个答案:

答案 0 :(得分:1)

getPreferredSize()的结果是可靠的,一旦您指定layoutpack()Window。根据建议here,您可能希望覆盖getPreferredSize()

image

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/** @see https://stackoverflow.com/posts/37501391/edit */
public class Test {

    private void display() {
        Frame f = new Frame(){
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 240);
            }
        };
        f.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        f.setLayout(new GridBagLayout());
        Panel p = new Panel();
        p.add(new Label("Test", Label.RIGHT));
        p.add(new TextField("Test", 10));
        f.add(p);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        new Test().display();
    }
}