java swing - make the smallest button
Hi
When I create a button in swing it adds a border around my text, thus making my button a bit bigger. Now, I really need that screen space and what I usually do is create a text item (disabled ) which create much smaller component size (smaller space around my text) and add to it a listner. saves space. but awkward. Is there a better way to create a tiny button? (when I try to make it smaller it quickly puts a "..." inside though it got 开发者_如何学Croom for much more text)10x
You just need to set The Insets narrower...
jButton1.setText("jButton1");
jButton1.setMargin(new java.awt.Insets(1, 2, 1, 2));
The integer arguments are: int top, int left, int bottom, int right
Have you tried setting the size of the button?
jButton.
setMinimumSize(new Dimension(width, height))
jButton.
setPreferredSize(new Dimension(width, height))
jButton.
setMaximumSize(new Dimension(width, height))
You should be able to achieve the desired results using a combination of these methods, along with specifying the margin of your button, which controls the space between the text and the edges, i.e.
jButton.
setMargin(new Insets(top, left, bottom, right))
If youre using NetBeans GUI-builind this can be achived under the button property "margin" by changing its value to the type "user code" and the value to "new java.awt.Insets(1, 2, 1, 2)"
精彩评论