gpt4 book ai didi

java - 有谁知道如何布局不移动或重新调整放置在其中的任何组件的大小的 JToolBar ?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:50:49 26 4
gpt4 key购买 nike

任何人都可以帮助解决这个问题我正在尝试创建一个 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'

最佳答案

如前所述:JToolBar 的默认布局是一个 BoxLayout,它尊重组件的 maxSize。 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)

关于java - 有谁知道如何布局不移动或重新调整放置在其中的任何组件的大小的 JToolBar ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13345640/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com