3

誰かがこの問題を解決できますか?私はJToolBarを作成しようとしていますが、そのすべてのコンポーネントのサイズと位置を修正したいと思います。私はいくつかの異なるレイアウトマネージャーを試しましたが、それらはすべて、コンポーネントのサイズが変更されたときに、コンポーネントの中央に配置されたり、コンポーネントのサイズが変更されたりします。

これはGridbagLayoutを使用した例です。toolbar.add(component)メソッドを使用してデフォルトのレイアウトマネージャーも使用しましたが、結果は同じです: '

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;

public class ToolBarTest extends JFrame
{
    private JToolBar toolbar;
    private JPanel mainPanel;
    private JPanel toolBarPanel;
    private JButton aButton;
    private JCheckBox aCheckBox;
    private JList aList;
    private Box toolbarBox;
    private GridBagConstraints toolbarConstraints;
    private GridBagLayout toolbarLayout;
    private JLabel shapeLabel;
    private JComboBox<ImageIcon> shapeChooser;
    private JLabel colorLabel;
    private JComboBox colorChooser;

    private String colorNames[] = { "Black" , "Blue", "Cyan", "Dark Gray",
            "Gray", "Green", "Light Gray", "Magenta", "Orange",
            "Pink", "Red", "White", "Yellow", "Custom" };

    private String shapeNames[] = { "Line", "Oval", "Rectangle",
        "3D Rectangle","Paint Brush", "Rounded Rectangle" };

    public ToolBarTest()
    {


        setLayout( new BorderLayout() );
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        setSize( 500, 500 );


        add( createToolBar(), BorderLayout.PAGE_START );

        setVisible( true );

    }


    public void addToToolbar( Component component, int row, int column )
    {
        toolbarConstraints.gridx = column;
        toolbarConstraints.gridy = row;
        toolbarConstraints.anchor = GridBagConstraints.WEST;
        toolbarConstraints.fill = GridBagConstraints.NONE;
        toolbarConstraints.weightx = 0;
        toolbarConstraints.weighty = 0;
        toolbarConstraints.gridwidth = 1;
        toolbarConstraints.gridheight = 1;
        toolbarLayout.setConstraints( component, toolbarConstraints );
        toolbar.add( component );

    }// end addToToolbar


    public final JToolBar createToolBar()
    {
        toolbarLayout = new GridBagLayout();
        toolbarConstraints = new GridBagConstraints();

        // create the tool bar which holds the items to draw
        toolbar = new JToolBar();
        toolbar.setBorderPainted(true);
        toolbar.setLayout( toolbarLayout );
        toolbar.setFloatable( true );




        shapeLabel = new JLabel( "Shapes: " );
        addToToolbar( shapeLabel, 0, 1 );


        String iconNames[] = { "PaintImages/Line.jpg", 
            "PaintImages/Oval.jpg", "PaintImages/Rect.jpg",
            "PaintImages/3DRect.jpg","PaintImages/PaintBrush.jpg",
        "PaintImages/RoundRect.jpg"};

        ImageIcon shapeIcons[] = new ImageIcon[ shapeNames.length ];


        // create image icons 
        for( int shapeButton = 0; shapeButton < shapeNames.length; shapeButton++ )
        {

            shapeIcons[ shapeButton ] =
                    new ImageIcon( iconNames[ shapeButton ] );

        }// end for


        shapeChooser = 
                new JComboBox< ImageIcon >( shapeIcons );

        shapeChooser.setSize(  new Dimension( 50, 20 ));

        shapeChooser.setPrototypeDisplayValue( shapeIcons[ 0 ] ); 

        shapeChooser.setSelectedIndex( 0 );

        addToToolbar( shapeChooser, 0, 2 );

        colorLabel = new JLabel( "Colors: " );

        addToToolbar( colorLabel, 0, 3 );

        colorChooser = new JComboBox( colorNames );
        addToToolbar( colorChooser, 0, 4 );


        return toolbar;
    }// end createToolBar

    public static void main( String args[] )
    {
        new ToolBarTest();

    }// end main


}// end class ToolBarTest'
4

2 に答える 2

5

すでに述べたように、JToolBar のデフォルトのレイアウトは、コンポーネントの maxSize を尊重する BoxLayout です。JComboBox には妥当な最大値がないため、サブクラス化して getMaxSize をオーバーライドする必要があります。

protected JComboBox createCombo(Object[] shapeIcons) {
    return new JComboBox( shapeIcons ) {

        @Override
        public Dimension getMaximumSize() {
            return getPreferredSize();
        }

    };
}

// usage (keep the default layoutManager of the toolbar)
shapeChooser = createCombo(shapeIcons);
toolBar.add(shapeChooser);
... 
colorChooser = createCombo( colorNames );
toolBar.add(colorChooser)
于 2012-11-12T16:17:06.550 に答える
1

を拡張する代わりの方法を次に示しJComboBoxます。ツールバーに追加すると、優先サイズと最小/最大サイズが優先サイズにロックされるだけです。

ToolBarTest

もう 1 つの調整は、EmtpyBorder任意instanceofの aに an を追加することJLabelです。彼らはそこで少し「混雑」しているようです。

import java.awt.*;
import javax.swing.*;

public class ToolBarTest extends JFrame
{
    private JToolBar toolbar;
    private JLabel shapeLabel;
    private JComboBox shapeChooser;
    private JLabel colorLabel;
    private JComboBox colorChooser;

    private String colorNames[] = { "Black" , "Blue", "Cyan", "Dark Gray",
            "Gray", "Green", "Light Gray", "Magenta", "Orange",
            "Pink", "Red", "White", "Yellow", "Custom" };

    private String shapeNames[] = { "Line", "Oval", "Rectangle",
        "3D Rectangle","Paint Brush", "Rounded Rectangle" };

    public ToolBarTest()
    {
        setLayout( new BorderLayout() );
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        setSize( 500, 200 );
        add( createToolBar(), BorderLayout.PAGE_START );

        setVisible( true );
    }

    public void addToToolbar( Component component, int row, int column )
    {
        Dimension d = component.getPreferredSize();
        component.setMaximumSize(d);
        component.setMinimumSize(d);
        component.setPreferredSize(d);
        toolbar.add( component );

    }// end addToToolbar

    public final JToolBar createToolBar()
    {
        // create the tool bar which holds the items to draw
        toolbar = new JToolBar();

        shapeLabel = new JLabel( "Shapes: " );
        addToToolbar( shapeLabel, 0, 1 );

        shapeChooser = 
                new JComboBox( shapeNames );
        shapeChooser.setSelectedIndex( 0 );
        addToToolbar( shapeChooser, 0, 2 );
        colorLabel = new JLabel( "Colors: " );

        addToToolbar( colorLabel, 0, 3 );

        colorChooser = new JComboBox( colorNames );
        addToToolbar( colorChooser, 0, 4 );

        return toolbar;
    }// end createToolBar

    public static void main( String args[] )
    {
        new ToolBarTest();

    }// end main
}// end class ToolBarTest'
于 2012-11-12T22:57:38.400 に答える