JButton如何控制它的大小?

时间:2013-12-02 20:09:32

标签: java swing layout-manager preferredsize null-layout-manager

我在框架上添加了一个按钮,并使用计时器控制文本和/或最小尺寸。

我注意到,该按钮有时会增长,有时则不会增长。

如果在null布局中,则忽略文本和最小宽度更改。

如果在FlowLayout布局中,则在文本更改时增长,但忽略最小宽度更改。

在后一种情况下如何让它成长?如果最小宽度发生变化,那么为什么布局管理器不会重新整形按钮?如何强迫它重塑?

注意:以下代码为SSCCE,即用于调查目的。

public class Try_ButtonAutoresize_01 {

    private static final Logger log = LoggerFactory.getLogger(Try_ButtonAutoresize_01.class);

    private static final String text = "Very Long Text For Appear On Button ";
    private static int position = 7;

    public static void main(String[] args) {

        final JButton button = new JButton(text.substring(0, position));
        button.setBounds(10, 10, 100, 50);

        JFrame frame = new JFrame();
        //frame.setLayout(null);
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.add(button);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100,100);
        frame.setSize(640, 480);
        frame.setVisible(true);

        new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {


                /*
                button.setText(button.getText() + text.charAt(position));

                position++;
                if( position >= text.length() ) {
                    position=0;
                }
                */


                button.setMinimumSize(new Dimension((int)button.getMinimumSize().getWidth()+10, (int)button.getMinimumSize().getHeight()));

                log.info("Minimum width is now = {}", button.getMinimumSize().getWidth());
            }
        }).start();

    }

}

更新

它也不适用于首选尺寸。无效也无济于事。

public class Try_ButtonAutoresize_01 {

    private static final Logger log = LoggerFactory.getLogger(Try_ButtonAutoresize_01.class);

    private static final String text = "Very Long Text For Appear On Button ";
    private static int position = 7;

    public static void main(String[] args) {

        final JButton button = new JButton(text.substring(0, position));
        button.setBounds(10, 10, 100, 50);

        final JFrame frame = new JFrame();
        //frame.setLayout(null);
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.add(button);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100,100);
        frame.setSize(640, 480);
        frame.setVisible(true);

        new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {


                /*
                button.setText(button.getText() + text.charAt(position));

                position++;
                if( position >= text.length() ) {
                    position=0;
                }
                */


                //button.setMinimumSize(new Dimension((int)button.getMinimumSize().getWidth()+10, (int)button.getMinimumSize().getHeight()));
                button.setPreferredSize(new Dimension((int)button.getPreferredSize().getWidth()+10, (int)button.getPreferredSize().getHeight()));

                //frame.invalidate();
                //button.invalidate();

                log.info("Minimum width is now = {}", button.getMinimumSize().getWidth());
                log.info("Preferred width is now = {}", button.getPreferredSize().getWidth());
            }
        }).start();

    }

}

更新2

答案需要2个条件:

1)使用setPreferredSize()作为@Sage建议,

2)致电revalidate()(我在setText()源代码中看到的内容)。

最终的工作代码如下:

public class Try_ButtonAutoresize_02 {

    private static final Logger log = LoggerFactory.getLogger(Try_ButtonAutoresize_02.class);

    public static void main(String[] args) {

        final JButton button = new JButton("short");

        final JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.add(button);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100,100);
        frame.setSize(640, 480);
        frame.setVisible(true);

        new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {


                //button.setMinimumSize(new Dimension((int)button.getMinimumSize().getWidth()+10, (int)button.getMinimumSize().getHeight()));
                button.setPreferredSize(new Dimension((int)button.getPreferredSize().getWidth()+10, (int)button.getPreferredSize().getHeight()));

                button.revalidate();

                log.info("Preferred width is now = {}", button.getPreferredSize().getWidth());


            }
        }).start();

    }

}

1 个答案:

答案 0 :(得分:3)

  1. 不要采用第一种方法:null layout(AbsoluteLayout)即使有人威胁你生命死亡。
  2. 使用FlowLayout的第二种方法:您可能已经知道它是一个LayoutManager。但是,此布局管理器尊重组件的首选大小。因此,通过设置边界来提供大小提示:setBounds(x, y, width, height)将不起作用。使用button.setPreferredSize(Dimension)获得快速结果。但是,您应该再次扩展该类并覆盖getPreferredSize(Dimension)和其他getXXXSize(Dimension)方法以根据内容大小进行调整。

     JButton button = new JButton("Press Me"){
        @Override
        public Dimension getPreferredSize() {
            Dimension size = new Dimension();
            size.width = ???; // give some thinking about it
            size.height = ???; // give some thinking about it
            return size;
        }
    
     };
    
  3. 正如@mKobel在下面指出的那样,我忘了解决这个问题:不要将大小设置为JFrame。它减少了布局管理器正确布局的机会。请改为在JFrame.pack()之前致电JFrame.setVisible(true)

  4. 有关详细信息,请查看本教程页面:

    1. How to use FlowLayout
    2. Solving Common Layout Problems