java – 有没有人知道如何布局不移动或重新放大其中任何组件的JToolBar?
作者:互联网
任何人都可以帮助解决这个问题我正在尝试创建一个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,layout,swing,preferredsize,jtoolbar 来源: https://codeday.me/bug/20190826/1725569.html