->效果
->源码
//: Show.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* // 显示框架
* @author cyb_23
*/
public class Show {
/**
* 框架
* @param jp
* @param width
* @param height
*/
public static void inFrame(JPanel jp, int width, int height) {
// 框架标题
String title = jp.getClass().toString();
if (title.indexOf("class") != -1){
title = title.substring(6);
}
// 框架
JFrame frame = new JFrame(title);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.getContentPane().add(jp, BorderLayout.CENTER);
frame.setSize(width, height);
frame.setVisible(true);
}
} // /:~
//: Borders.java
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
/**
* Swing边框
* @author cyb_23
*/
public class Borders extends JPanel {
/**
* 显示边框的面板
* @param b
* @return JPanel
*/
static JPanel showBorder(Border b) {
JPanel jp = new JPanel();
jp.setLayout(new BorderLayout());
String nm = b.getClass().toString();
nm = nm.substring(nm.lastIndexOf('.') + 1);
jp.add(new JLabel(nm, JLabel.CENTER), BorderLayout.CENTER);
jp.setBorder(b);
return jp;
}
/**
* 构造函数
*/
public Borders() {
setLayout(new GridLayout(2, 4));
add(showBorder(new TitledBorder("Title")));
add(showBorder(new EtchedBorder()));
add(showBorder(new LineBorder(Color.blue)));
add(showBorder(new MatteBorder(5, 5, 30, 30, Color.green)));
add(showBorder(new BevelBorder(BevelBorder.RAISED)));
add(showBorder(new SoftBevelBorder(BevelBorder.LOWERED)));
add(showBorder(new CompoundBorder(new EtchedBorder(), new LineBorder(Color.red))));
}
public static void main(String args[]) {
Show.inFrame(new Borders(), 500, 300);
}
} ///:~