1

フレームにボタンを追加し、タイマーでテキストや最小サイズを制御しました。

ボタンが大きくなることもあれば、そうでないこともあります。

レイアウトの場合、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();

    }

}
4

1 に答える 1

3
  1. 最初のアプローチには乗らないでくださいnull layout(AbsoluteLayout)。誰かがあなたを生死に脅かしたとしても。
  2. 2 番目のアプローチFlowLayout: ご存知かもしれませんが、これは LayoutManager です。ただし、このレイアウト マネージャーは、コンポーネントの推奨サイズを尊重します。したがって、bounds: を設定してサイズのヒントを与え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 が以下で指摘しているように、対処するのを忘れていました: size を に設定しないでくださいJFrame。レイアウト マネージャーが適切にレイアウトする可能性を減らします。代わりJFrame.pack()に前に電話してください。JFrame.setVisible(true)

詳細については、このチュートリアル ページをご覧ください。

  1. フローレイアウトの使い方
  2. 一般的なレイアウトの問題を解決する
于 2013-12-02T20:15:53.817 に答える