java 在 Swing 中创建方形按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16257125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating Square Buttons in Swing
提问by Thunderforge
I would like to create something like the following in Swing:
我想在 Swing 中创建如下内容:
The top part is relatively easy: I can just create a table and display it. What I'm having trouble with is the square plus and minus buttons at the bottom, which are designed to add a new item or remove the selected item respectively. In particular, I haven't been able to make the square shape because on Mac OS X and some other platforms, JButtons are rectangles with rounded corners and I can't find a way to change that. Also, I'm wanting to make sure it's a perfect square and without any space in between buttons.
顶部相对简单:我可以创建一个表格并显示它。我遇到的问题是底部的方形加号和减号按钮,它们旨在分别添加新项目或删除所选项目。特别是,我无法制作方形,因为在 Mac OS X 和其他一些平台上,JButton 是带圆角的矩形,我找不到改变它的方法。另外,我想确保它是一个完美的正方形,并且按钮之间没有任何空间。
How can this be accomplished in a cross-platform way on Swing?
如何在 Swing 上以跨平台的方式实现这一点?
采纳答案by user0
You can set the size of a button using using setPreferredSize():
您可以使用setPreferredSize()设置按钮的大小:
JButton button = new JButton("+");
button.setPreferredSize(new Dimension(10, 10));
You might be able to remove the rounded corners using:
您可以使用以下方法去除圆角:
button.setBorder(BorderFactory.createEmptyBorder());
button.setBorder(BorderFactory.createEmptyBorder());
If this does not work then you can override the paintComponent()method on JButton.
如果这不起作用,那么您可以覆盖JButton 上的paintComponent()方法。
回答by camickr
JButtons are rectangles with rounded corners and I can't find a way to change that.
JButtons 是带有圆角的矩形,我找不到改变它的方法。
Change the Border:
更改边框:
button.setBorder( new LineBorder(Color.BLACK) );
Edit.
编辑。
Another approach is to create your own icon from an existing button. Something like the following:
另一种方法是从现有按钮创建自己的图标。类似于以下内容:
JButton button = new JButton("+");
Dimension size = button.getPreferredSize();
size.x += 6;
size.y += 6;
button.setPreferredSize(size);
Rectangle rectangle = new Rectangle(3, 3, size.x - 3, size.y - 3);
ScreenImage buttonImage = ScreenImage(button, rectangle);
ImageIcon icon = new ImageIcon(buttonImage);
JButton plus = new JButton(icon);
plus.setBorder( ... );
The above code should create an image of your button on any platform. I increased the preferred size to avoid taking an image of the border.
上面的代码应该在任何平台上创建按钮的图像。我增加了首选尺寸以避免拍摄边框图像。
You will need to use the Screen Imageclass.
您将需要使用屏幕图像类。
回答by Andrew Thompson
This is most easily achieved by returning a preferred size that is NxN - where N is the larger of preferred width or height.
这最容易通过返回首选大小 NxN 来实现 - 其中 N 是首选宽度或高度中的较大者。
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class SquareButton extends JButton {
SquareButton(String s) {
super(s);
}
@Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
int s = (int)(d.getWidth()<d.getHeight() ? d.getHeight() : d.getWidth());
return new Dimension (s,s);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
JComponent gui = new JPanel(new FlowLayout());
for (int ii=0; ii<5; ii++) {
gui.add(new SquareButton("" + ii));
}
gui.setBorder(new EmptyBorder(4, 8, 4, 8));
JFrame f = new JFrame("Square Buttons");
f.add(gui);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
回答by Ash..
This is good tutorial for custom designing of buttons.
这是自定义按钮设计的好教程。
回答by user2277872
Well in order to make them square, you have 2 options: 1. Make the button hold an icon image of just a square image of a transparent image. 2. you could set the button dimensions on your own. I am not sure how to set the dimensions, but that is a an option you could choose. You can just create a JToolBar that is set on the BorderLayout.SOUTH end of the window whenever you add, and whatever buttons are added onto that, will be right next to each other. To add the buttons do this:
好吧,为了使它们成为方形,您有 2 个选择: 1. 使按钮保持透明图像的方形图像的图标图像。2.您可以自己设置按钮尺寸。我不确定如何设置尺寸,但这是您可以选择的一个选项。无论何时添加,您都可以创建一个设置在窗口的 BorderLayout.SOUTH 端的 JToolBar,并且添加到其上的任何按钮都将彼此相邻。要添加按钮,请执行以下操作:
JButton button1 = new JButton("+");
JButton button2 = new JButton("-");
JToolBar toolbar = new JToolBar();
<JPanel,JFrame,Whatever>.add(toolbar, BorderLayout.SOUTH);
toolbar.add(button1);
toolbar.add(button2);
This will add the toolbar onto the JFrame, JPanel, or whatever you're adding it onto, and it will set it to the bottom of the screen as you see above.
这会将工具栏添加到 JFrame、JPanel 或您添加到的任何内容上,并将其设置到屏幕底部,如上所示。
Hope this helps!
希望这可以帮助!