✏️ 사용 방법
AbstractBorder border = new LineBorder(Color.BLACK, 2); pnl.setBorder(border);
총 정리 하기 전에 기본 세팅을 미리 깔고 정리하겠다!
임의의
getLabel()
메서드를 만들어주었다.
public class BorderTest extends JFrame {
private Font font = new Font(Font.DIALOG, Font.BOLD, 25);
// 글자를 입력하면 테두리쳐진 틀이 만들어짐
private JLabel getLabel(String text, AbstractBorder border) {
JLabel lbl = new JLabel(text);
lbl.setFont(font); // 폰트 미리 설정
lbl.setBorder(border);
return lbl;
...
생성자에도 미리 세팅
public BorderTest() {
JPanel pnlMain = new JPanel(new GridLayout(0, 1, 10, 10));
JScrollPane scroll = new JScrollPane(pnlMain);
// Insets: 위, 왼, 아래, 오
Insets insets = new Insets(20, 20, 20, 20);
pnlMain.setBorder(new EmptyBorder(insets));
...
임의의 메서드를 만들어서 간단하게 만들어졌다.
원래라면, pnlMain.setBorder(border)
를 해줘야 한다.
LineBorder border = new LineBorder(Color.BLACK, 2);
pnlMain.add(getLabel("LineBorder", border));
TitledBorder title = new TitledBorder("this is a title");
pnlMain.add(getLabel("TitledBorder", title));
LineBorder lBorder = new LineBorder(Color.GRAY, 1);
border = new TitledBorder(lBorder, "TitledBorder + LineBorder");
pnlMain.add(getLabel("TitledBorder", border));
// tBorder.setBorder(new TitledBorder(new EmptyBorder(0, 0, 0, 0), "title"));
TitledBorder tBorder = new TitledBorder(new EmptyBorder(0, 0, 0, 0), "title");
pnlMain.add(getLabel("TitledBorder", tBorder));
setTitleJustification
TitledBorder tBorder = new TitledBorder("right");
// 타이틀의 횡위치 설정: setTitleJustification
tBorder.setTitleJustification(TitledBorder.RIGHT); // 타이틀 위치가 오른쪽으로!
border = tBorder;
pnlMain.add(getLabel("TitledBorder", border));
setTitleFont
: 폰트 설정
setTitleColor
: 폰트 색깔 설정
tBorder = new TitledBorder("Text");
//타이틀 폰트설정: setTitleFont
tBorder.setTitleFont(new Font(Font.DIALOG, Font.BOLD | Font.ITALIC, 15));
// 타이틀 색깔설정: setTitleColor
tBorder.setTitleColor(Color.RED);
pnlMain.add(getLabel("TitleBorder", tBorder);
setTitlePosition
tBorder = new TitledBorder("ABOVE-BOTTOM");
tBorder.setTitlePosition(TitledBorder.ABOVE_BOTTOM);
pnlMain.add(getLabel("TitledBorder", tBorder));
tBorder = new TitledBorder("ABOVE-TOP");
tBorder.setTitlePosition(TitledBorder.ABOVE_TOP);
pnlMain.add(getLabel("TitledBorder", tBorder));
tBorder = new TitledBorder("BELOW-TOP");
tBorder.setTitlePosition(TitledBorder.BELOW_TOP);
pnlMain.add(getLabel("TitledBorder", tBorder));
tBorder = new TitledBorder("BELOW-BOTTOM");
tBorder.setTitlePosition(TitledBorder.BELOW_BOTTOM);
pnlMain.add(getLabel("TitledBorder", tBorder));
TitledBorder innerBorder = new TitledBorder(
(new LineBorder(Color.BLUE, 1)),
"inner-border"
);
innerBorder.setTitlePosition(TitledBorder.BOTTOM);
tBorder = new TitledBorder(
innerBorder,
"TOP"
);
pnlMain.add(getLabel("TitledBorder", tBorder));
tBorder = new TitledBorder("CENTER-BOTTOM");
// 타이틀 횡위치 설정
tBorder.setTitleJustification(TitledBorder.CENTER);
// 타이틀의 종위치 설정
tBorder.setTitlePosition(TitledBorder.BOTTOM);
pnlMain.add(getLabel("TitledBorder", tBorder));
보더의 선이 입체적이다.
border = new EtchedBorder(EtchedBorder.RAISED); // 보더가 입체적
pnlMain.add(getLabel("EtchedBorder-RAISED", border));
border = new EtchedBorder(EtchedBorder.LOWERED);
pnlMain.add(getLabel("EtchedBorder-LOWERED", border));
보더가 버튼같은 효과를 낸다.
border = new BevelBorder(BevelBorder.RAISED);
pnlMain.add(getLabel("BevelBorder-RAISED", border));
border = new BevelBorder(BevelBorder.LOWERED);
pnlMain.add(getLabel("BevelBorder-LOWERED", border));
// 세로스크롤바를 가져온다.
JScrollBar bar = scroll.getVerticalScrollBar();
// 스크롤바의 움직이는 단위를 설정한다.
bar.setUnitIncrement(5);
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.border.AbstractBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class BorderTest extends JFrame {
private Font font = new Font(Font.DIALOG, Font.BOLD, 25);
// 글자를 입력하면 테두리쳐진 틀 만들어짐
private JLabel getLabel(String text, AbstractBorder border) {
JLabel lbl = new JLabel(text);
lbl.setFont(font);
lbl.setBorder(border);
return lbl;
}
public BorderTest() {
JPanel pnlMain = new JPanel(new GridLayout(0, 1, 10, 10));
JScrollPane scroll = new JScrollPane(pnlMain);
// Insets: 위, 왼, 아래, 오
Insets insets = new Insets(20, 20, 20, 20);
pnlMain.setBorder(new EmptyBorder(insets));
AbstractBorder border = new LineBorder(Color.BLACK, 2);
pnlMain.add(getLabel("LineBorder", border));
border = new TitledBorder("this is a title");
pnlMain.add(getLabel("TitledBorder", border));
LineBorder lBorder = new LineBorder(Color.GRAY, 1);
border = new TitledBorder(lBorder, "TitleBorder + LineBorder");
pnlMain.add(getLabel("TitledBorder", border));
TitledBorder tBorder = new TitledBorder("right");
// 타이틀의 횡위치 설정: setTitleJustification
tBorder.setTitleJustification(TitledBorder.RIGHT); // 타이틀 위치가 오른쪽으로!
border = tBorder;
pnlMain.add(getLabel("TitledBorder", border));
tBorder = new TitledBorder("Text");
//타이틀 폰트설정: setTitleFont
tBorder.setTitleFont(new Font(Font.DIALOG, Font.BOLD | Font.ITALIC, 15));
tBorder.setTitleColor(Color.RED);
pnlMain.add(getLabel("TitleBorder", tBorder));
TitledBorder innerBorder = new TitledBorder(
(new LineBorder(Color.BLUE, 1)),
"inner-border"
);
innerBorder.setTitlePosition(TitledBorder.BOTTOM);
tBorder = new TitledBorder(
innerBorder,
"TOP"
);
pnlMain.add(getLabel("TitledBorder", tBorder));
tBorder = new TitledBorder("CENTER-BOTTOM");
tBorder.setTitleJustification(TitledBorder.CENTER);
// 타이틀의 종위치 설정
tBorder.setTitlePosition(TitledBorder.BOTTOM);
pnlMain.add(getLabel("TitledBorder", tBorder));
tBorder = new TitledBorder("ABOVE-BOTTOM");
tBorder.setTitlePosition(TitledBorder.BOTTOM);
pnlMain.add(getLabel("TitledBorder", tBorder));
tBorder = new TitledBorder("ABOVE-TOP");
tBorder.setTitlePosition(TitledBorder.ABOVE_TOP);
pnlMain.add(getLabel("TitledBorder", tBorder));
tBorder = new TitledBorder("BELOW-TOP");
tBorder.setTitlePosition(TitledBorder.BELOW_TOP);
pnlMain.add(getLabel("TitledBorder", tBorder));
tBorder = new TitledBorder("BELOW-BOTTOM");
tBorder.setTitlePosition(TitledBorder.BELOW_BOTTOM);
pnlMain.add(getLabel("TitledBorder", tBorder));
border = new EtchedBorder(EtchedBorder.RAISED); // 보더가 입체적
pnlMain.add(getLabel("EtchedBorder-RAISED", border));
border = new EtchedBorder(EtchedBorder.LOWERED);
pnlMain.add(getLabel("EtchedBorder-LOWERED", border));
border = new BevelBorder(BevelBorder.RAISED);
pnlMain.add(getLabel("BevelBorder-RAISED", border));
border = new BevelBorder(BevelBorder.LOWERED);
pnlMain.add(getLabel("BevelBorder-LOWERED", border));
add(scroll, BorderLayout.CENTER);
// 세로스크롤바를 가져온다.
JScrollBar bar = scroll.getVerticalScrollBar();
// 스크롤바의 움직이는 단위르 설정한다.
bar.setUnitIncrement(5);
setTitle("Border Test");
setSize(400, 300);
setLocation(100, 0);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new BorderTest();
}
}