Java JButton 更改默认边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33954698/
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
JButton change default border
提问by Kitsundere-chi
I'm using the SystemLookAndFeel which makes the default border around my buttons.
我正在使用 SystemLookAndFeel,它在我的按钮周围设置默认边框。
Now I would like a ticker black border, but when I set the border it just adds my new border around the default one, so I have two.
现在我想要一个股票黑色边框,但是当我设置边框时,它只会在默认边框周围添加我的新边框,所以我有两个。
How can I change or remove the border without removing the LookAndFeel?
如何在不删除 LookAndFeel 的情况下更改或删除边框?
Also: I am using java 7 and Win 8.1
另外:我正在使用 java 7 和 Win 8.1
采纳答案by MadProgrammer
Work with Java 8 on Windows 10, I did this little test
在 Windows 10 上使用 Java 8,我做了这个小测试
As you can see, about the only method I can get to work is using setContentAreaFilled
如您所见,我可以开始工作的唯一方法是使用 setContentAreaFilled
The general problem is, many look and feels don't use the border
property, but instead paint their own borders independently (hence the reason for setBorderPainted
), but the look and feel for Windows 10 just wants to be different
普遍的问题是,许多外观并不使用该border
属性,而是独立绘制自己的边框(因此是 的原因setBorderPainted
),但 Windows 10 的外观只是希望有所不同
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class ButtonTest {
public static void main(String[] args) {
new ButtonTest();
}
public ButtonTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
JButton btn = new JButton("Normal");
add(btn, gbc);
btn = new JButton("With border");
btn.setBorder(new LineBorder(Color.BLACK));
add(btn, gbc);
btn = new JButton("borderPainted false");
btn.setBorderPainted(false);
btn.setBorder(new LineBorder(Color.BLACK));
add(btn, gbc);
btn = new JButton("contentArea false");
btn.setContentAreaFilled(false);
btn.setBorder(new LineBorder(Color.BLACK));
add(btn, gbc);
btn = new JButton("focusPained false");
btn.setContentAreaFilled(false);
btn.setFocusPainted(false);
btn.setBorder(new LineBorder(Color.BLACK));
add(btn, gbc);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
回答by RAP
Try this program it has all types of borders you can have in a Jbutton
试试这个程序,它有你可以在 Jbutton 中拥有的所有类型的边框
import javax.swing.*;
import java.awt.*;
public class jbuttonBoders extends JFrame {
private JButton button[];
private JPanel panel;
public jbuttonBoders() {
super("JButton Border");
setSize(220,190);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button = new JButton[12];
panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
//Constructing all 12 JButtons using "for loop"
for(int count=0; count<button.length; count++) {
button[count] = new JButton("Button "+(count+1));
panel.add(button[count]);
}
//Setting Different Borders on each JButton
button[0].setBorder(BorderFactory.createLineBorder(Color.blue)); // Simple Line Border
button[1].setBorder(BorderFactory.createLineBorder(Color.red, 5)); // Line Border + Thickness of the Border
button[2].setBorder(BorderFactory.createBevelBorder(1)); // Inner Bevel Border
button[3].setBorder(BorderFactory.createBevelBorder(0)); // Outer Bevel Border
button[4].setBorder(BorderFactory.createBevelBorder(1, Color.red, Color.blue)); // Two Colors Inner Bevel
button[5].setBorder(BorderFactory.createBevelBorder(0, Color.green, Color.orange)); // Two Colors Outer Bevel
button[6].setBorder(BorderFactory.createBevelBorder(1, Color.green, Color.orange, Color.red, Color.blue)); //Four Colors Inner Bevel
button[7].setBorder(BorderFactory.createBevelBorder(0, Color.green, Color.orange, Color.red, Color.blue)); //Four Colors Outer Bevel
button[8].setBorder(BorderFactory.createEmptyBorder(5,10,5,50)); // Empty Border (Upper Space, Left Space, Bottom Space, Right Space)
button[9].setBorder(BorderFactory.createEtchedBorder(0)); //Raised Border Line
button[10].setBorder(BorderFactory.createEtchedBorder(1)); //
button[11].setBorder(BorderFactory.createTitledBorder("My Titled Border")); // Titled Border
/** The Borders shown above are the basic borders that we commonly used.
* There are still lots of Border Styles available so all you have to do is to discover
* and have some experiment using all the available borders. I recommend you use JCreator Pro
* if want to know more about different border styles and learn how to implement them.
*/
//Setting up the container ready for the components to be added.
Container pane = getContentPane();
setContentPane(pane);
//Adding the JPanel to the container
pane.add(panel);
/**Set all the Components Visible.
* If it is set to "false", the components in the container will not be visible.
*/
setVisible(true);
}
//Main Method
public static void main (String[] args) {
jbuttonBoders jbb = new jbuttonBoders();
}
}
Important Part of the Program:
//Setting Different Borders on each JButton
button[0].setBorder(BorderFactory.createLineBorder(Color.blue)); // Simple Line Border
button[1].setBorder(BorderFactory.createLineBorder(Color.red, 5)); // Line Border + Thickness of the Border
button[2].setBorder(BorderFactory.createBevelBorder(1)); // Inner Bevel Border
button[3].setBorder(BorderFactory.createBevelBorder(0)); // Outer Bevel Border
button[4].setBorder(BorderFactory.createBevelBorder(1, Color.red, Color.blue)); // Two Colors Inner Bevel
button[5].setBorder(BorderFactory.createBevelBorder(0, Color.green, Color.orange)); // Two Colors Outer Bevel
button[6].setBorder(BorderFactory.createBevelBorder(1, Color.green, Color.orange, Color.red, Color.blue)); //Four Colors Inner Bevel
button[7].setBorder(BorderFactory.createBevelBorder(0, Color.green, Color.orange, Color.red, Color.blue)); //Four Colors Outer Bevel
button[8].setBorder(BorderFactory.createEmptyBorder(5,10,5,50)); // Empty Border (Upper Space, Left Space, Bottom Space, Right Space)
button[9].setBorder(BorderFactory.createEtchedBorder(0)); //Raised Border Line
button[10].setBorder(BorderFactory.createEtchedBorder(1)); //
button[11].setBorder(BorderFactory.createTitledBorder("My Titled Border")); // Titled Border
Output:
输出: