简体   繁体   中英

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 window. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM