フレームにボタンを追加し、タイマーでテキストや最小サイズを制御しました。
ボタンが大きくなることもあれば、そうでないこともあります。
レイアウトの場合、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();
}
}