0

JFrame のボタンのサイズを変更しようとしています。この質問に対するすべての回答は、button.setPreferredSize(new Dimension(x, y)) または button.setSize(new Dimension(x, y)) を使用するように言っていますが、どちらも機能しません。ボタンのタイトルは適切な場所に表示されますが、ボタンが画面全体を占めています。また、ペインではなくフレームにボタンを追加してみました。そうすると、ボタンは正しいサイズですが、画像の外にあります。このボタンを画像の上に表示するには、このボタンを配置してサイズを変更する方法を教えてください。

public ImageTest(String title, String imageSource, int minimum, int middle, int maximum, int curr, boolean x) { // Extends JFrame...
        frame = new JFrame();
        frame.setTitle(title);
        frame.setLayout(new GridBagLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        maxim = maximum;
        minim = minimum;
        mercSize(minim, maxim, curr); //change size of mercury in thermometer

        pane = new PaintPane(new ImageIcon(imageSource).getImage());
        pane.setLayout(new BorderLayout());
        frame.add(pane);

        b1 = new JButton("Increase Temp");
        b1.setPreferredSize(new Dimension(100, 100)); //Button does not work yet
        b1.setBorder(BorderFactory.createEmptyBorder(560,250,0,0));
        b1.setLocation(95, 45);
        pane.add(b1);
        frame.pack();

        min = new JLabel("" + minimum);
        min.setFont(min.getFont().deriveFont(Font.BOLD, 28));
        if(x == true) min.setForeground(Color.BLACK); //Changes the font color to match the image
        else min.setForeground(Color.WHITE);
        min.setVerticalAlignment(JLabel.TOP);
        min.setVerticalTextPosition(JLabel.TOP);
        min.setBorder(BorderFactory.createEmptyBorder(445, 150, 0, 0)); //Positions the text
        pane.add(min);
        frame.pack();

        mid = new JLabel("" + middle);
        mid.setFont(mid.getFont().deriveFont(Font.BOLD, 28));
        if(x == true) mid.setForeground(Color.BLACK); //Changes the font color to match the image
        else mid.setForeground(Color.WHITE);
        mid.setVerticalAlignment(JLabel.TOP);
        mid.setVerticalTextPosition(JLabel.TOP);
        mid.setBorder(BorderFactory.createEmptyBorder(240, 150, 0, 0)); //Positions the text
        pane.add(mid);
        frame.pack();

        max = new JLabel("" + maximum);
        max.setFont(max.getFont().deriveFont(Font.BOLD, 28));
        if(x == true) max.setForeground(Color.BLACK); //Changes the font color to match the image
        else max.setForeground(Color.WHITE);
        max.setVerticalAlignment(JLabel.TOP);
        max.setVerticalTextPosition(JLabel.TOP);
        max.setBorder(BorderFactory.createEmptyBorder(35, 150, 0, 0)); //Positions the text
        pane.add(max);
        frame.pack();

        temp = new JLabel("Current Temperature: " + curr);
        temp.setFont(temp.getFont().deriveFont(Font.BOLD, 28));
        if(x == true) temp.setForeground(Color.BLACK); //Changes the font color to match the image
        else temp.setForeground(Color.WHITE);
        temp.setVerticalAlignment(JLabel.TOP);
        temp.setVerticalTextPosition(JLabel.TOP);
        temp.setBorder(BorderFactory.createEmptyBorder(560, 120, 0, 0)); //Positions the text
        pane.add(temp);
        frame.pack();

        error = new JLabel();

        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
4

1 に答える 1

2

一連の複合的な問題があります。基本的に、 のレイアウト マネージャーを に設定してPaintPaneからBorderLayout、一連のコンポーネントをそれに追加してみます。

は、多くの要因 (コンポーネントの優先サイズと外部コンポーネントの最小サイズ... 私の記憶が正しければ...)BorderLayoutに基づいて、コンテンツをどのようにレイアウトするのが最善と考えるかについて決定を下します。CENTER

詳しく見てみましょう...

public ImageTest(String title, String imageSource, int minimum, int middle, int maximum, int curr, boolean x) { // Extends JFrame...
    //...
    pane = new PaintPane(new ImageIcon(imageSource).getImage());
    pane.setLayout(new BorderLayout());
    // Add button to center position
    pane.add(b1);
    // Add min to center position, overwriting b1
    pane.add(min);
    // Add mid to center position, overwriting 
    pane.add(mid);
    // Add max to center position, overwriting mid
    pane.add(max);
    // Add temp to center position, overwriting max
    pane.add(temp);
    frame.pack();
    //...
}

に新しいコンポーネントを追加するたびに、PaintPane以前に存在していたものを効果的に削除します (表示されないだけでコンテナーに残ります)。これは、BorderLayout5 つのレイアウト位置のそれぞれに 1 つのコンポーネントしか表示できないためです。デフォルトの位置は常にCENTER

これは、tempが最後に追加されたコンポーネントであることを意味します。フレームは、フレームのサイズを計算するための基準として優先サイズを使用します (フレームに表示可能なその他のコンポーネントを加えます)。

より良い解決策は、別のレイアウトマネージャーを使用することです...

また、Java Swing で set(Preferred|Maximum|Minimum)Size メソッドを使用しないようにする必要がありますか?も参照してください。

于 2013-10-14T06:10:14.123 に答える