setBorder是设置边框的
比如默认的label标签,没有边框就是确定按钮后面的一行字,设置了边框以后
createLoweredBevelBorder()这个的样式就是这样的,陷下去了,下面是测试程序,你还可以设置其他的边框样式:
// jLabel.setBorder(BorderFactory.createLineBorder(Color.red));
// jLabel.setBorder(BorderFactory.createEtchedBorder());
代码:
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MyFrame extends JFrame{
boolean flag = false;
public MyFrame(){
init();
}
private void init() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200, 300);
setContentPane(initPanel());
}
private Container initPanel() {
JPanel panel = new JPanel();
final JLabel jLabel = new JLabel("斯蒂芬螺丝刀理发师");
JButton button = new JButton("确定");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(flag){
//设置边框
jLabel.setBorder(BorderFactory.createLoweredBevelBorder());
// jLabel.setBorder(BorderFactory.createLineBorder(Color.red));
// jLabel.setBorder(BorderFactory.createEtchedBorder());
}else{
jLabel.setBorder(null);
}
flag = !flag;
}
});
panel.add(button);
panel.add(jLabel);
return panel;
}
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.setVisible(true);
}
}
是p.setBorder(BorderFactory.createLoweredLevelBorder());吧?就是给p对象set一个boder,这个border是从一个工厂里面取的
分成两部分,BorderFactory.createLoweredBevelBorder() 获取值,p.setBorder,给其赋值