我希望能够在Java Swing中更改JButton组件的边框颜色。
我尝试了以下方法:
这会生成两个按钮,我尝试在button2组件上更改边框颜色,但它会移除填充。有没有办法保留标准JButton的原始插入并只更改颜色?
注意:我假设在分配新边框时删除了插入。但我不确定。
我尝试了以下方法:
package com.example.test;
import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test extends JFrame {
public Test() {
JPanel panel = new JPanel();
JButton button1 = new JButton("Test Button 1");
JButton button2 = new JButton("Test Button 2");
button2.setBorder(BorderFactory.createLineBorder(Color.RED));
panel.add(button1);
panel.add(button2);
this.add(panel);
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
} catch (ClassNotFoundException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
Test t = new Test();
}
}
这会生成两个按钮,我尝试在button2组件上更改边框颜色,但它会移除填充。有没有办法保留标准JButton的原始插入并只更改颜色?
注意:我假设在分配新边框时删除了插入。但我不确定。