重写setPreferredSize()和getPreferredSize() [英] Overriding setPreferredSize() and getPreferredSize()

查看:170
本文介绍了重写setPreferredSize()和getPreferredSize()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于收到的答复,我现在以不同的方式进行操作.

是否有任何方法可以覆盖setPreferredSize()和getPreferredSize(),因此它将实际上将组件的大小设置为一个比实际输入的数字大的数字,并获得一个比实际输入的数字小的数字? /p>

在这里,我重写了这两种方法,以将面板的尺寸设置为比我实际在setPreferredSize()中设置的尺寸大100像素,如果要获得首选的尺寸,我希望它返回我在setPreferredSize中设置的尺寸()

我怀疑Swing使用getPreferredSize()来设置大小,那么是否有我可以重写的另一种方法来实现此目的,还是我不得不制作另一种方法来返回想要的值?

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

public class PreferredSize extends JPanel{

public static void main(String[] args) {
    JPanel background = new JPanel();

    PreferredSize ps = new PreferredSize();
    ps.setPreferredSize(new Dimension(200, 200));
    ps.setBackground(Color.CYAN);

    JPanel panel = new JPanel();
    panel.setBackground(Color.BLUE);
    panel.setPreferredSize(new Dimension(200, 200));


    background.add(ps);
    background.add(panel);

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(background);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}


@Override
public void setPreferredSize(Dimension preferredSize) {
    super.setPreferredSize(new Dimension(preferredSize.width+100, preferredSize.height+100));
}

@Override
@Transient
public Dimension getPreferredSize() {
    return new Dimension(super.getPreferredSize().width-100, super.getPreferredSize().height-100);
}
}

谢谢.

解决方案

您面临的问题是,首选大小仅是布局管理者可以用来决定组件应该如何使用的指南.布置.布局经理忽略这些提示是完全合理的.

如果是ip,则为您的示例;

  1. JPanel的默认大小为0x0,因此,在您添加和减去100个像素时,其大小再次变为0x0.
  2. JFrame的默认布局管理器是BorderLayout,如果可用大小大于或小于首选大小指定的大小,它将忽略首选大小

真正的问题是,您实际要实现什么目标?

Due to the answers I have received I am now doing this a different way.

Is there any way to override setPreferredSize() and getPreferredSize() so it will actually set the size of the component to a number higher than was actually input, and get a number that is a number lower than it actually is?

Here I am overriding both methods to set the size of a panel to 100 pixels more than what I actually put in setPreferredSize() and if I were to get the preferred size, I would like it to return what I put in setPreferredSize()

I am suspecting that Swing uses getPreferredSize() to set the size, so is there another method I can override to achieve this or am I stuck having to make another method to return the values I want?

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

public class PreferredSize extends JPanel{

public static void main(String[] args) {
    JPanel background = new JPanel();

    PreferredSize ps = new PreferredSize();
    ps.setPreferredSize(new Dimension(200, 200));
    ps.setBackground(Color.CYAN);

    JPanel panel = new JPanel();
    panel.setBackground(Color.BLUE);
    panel.setPreferredSize(new Dimension(200, 200));


    background.add(ps);
    background.add(panel);

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(background);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}


@Override
public void setPreferredSize(Dimension preferredSize) {
    super.setPreferredSize(new Dimension(preferredSize.width+100, preferredSize.height+100));
}

@Override
@Transient
public Dimension getPreferredSize() {
    return new Dimension(super.getPreferredSize().width-100, super.getPreferredSize().height-100);
}
}

Thanks.

解决方案

The problem you're facing is the fact that preferred size is only a guide that CAN be used by the layout managers to make decisions about how a component should be laid out. It's perfectly reasonable for a layout manager to ignore these hints.

In the case of ip your example;

  1. The default size of a JPanel is 0x0, so by the time you've added in and subtracted the 100 pixels, its now 0x0 again.
  2. The default layout manager for a JFrame is BorderLayout, which will ignore the preferred size if its available size is larger or smaller then that specified by preferred size

The real question is, what are your actually trying to achieve?

这篇关于重写setPreferredSize()和getPreferredSize()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆