Resizing JButtons and other components according to text

前端 未结 2 833
不思量自难忘°
不思量自难忘° 2021-02-09 05:27

How do you resize a JButton at runtime so it adapts to the text given by setSize? I\'ve done some searching and this is the code I\'ve come up with so far. Could th

2条回答
  •  长情又很酷
    2021-02-09 06:06

    I had the same problem, even when using a layout manager (BorderLayout). But in my case a simple call to layoutContainer() of the associated layout manager and then a repaint() on the JFrame was sufficient for changing the width of the button.

    button1.setText("New Label that differs in width");
    // button1 is inside the container horizontalBox
    horizontalBox.getLayout().layoutContainer(horizontalBox);
    repaint(); // on the containing JFrame
    

提交回复
热议问题