ToolBar problem in swing + java
I have this code in my file
String[] iconFiles = { "state.jpg", "cursor.jpg", "arrow.jpg" };
String[] buttonLabels = { "New Node", "Attribute Change", "Add Edge"};
for (int i = 0; i < buttonLabels.length; ++i) {
icons[i] = new ImageIcon(iconFiles[i]);
buttons[i] = new JButton(icons[i]);
buttons[i].setToolTipText(buttonLabels[i]);
buttons[i].setPreferredSize(new java.awt.Dimension(648, 600));
toolBar.add(buttons[i]);
toolBar.addSeparator();
}
jp2.add(toolBar) where jp2 is a Tabbed Pane
I have 2 questions in this.
Firstly the image file that is represented by the icon does not get resized on its own and thus gets cut by the button size.
Secondly the toolbar gets shown in the window but I am able to drag it out into a sort of a new windo开发者_如何学Pythonw. How can I prevent this from happening?
A) The image in the icon can be rescaled using the following code:
icons[i].setImage(icons[i].getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT));
B) The JToolBar
can be made unfloatable which is what I think you want:
toolBar.setFloatable(false);
精彩评论