简体   繁体   English

Swing + Java中的ToolBar问题

[英]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: A)可以使用以下代码重新缩放图标中的图像:

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: B)可以使JToolBar变得不膨胀,这就是我想您想要的:

toolBar.setFloatable(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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