자바 JFC 구성 및 일반 클래스 활용 단축키 설정

자바 JFC 구성 및 일반 클래스 활용 단축키 설정


Swing 단축키 설정
Swing컴포넌트에서는 많은 이벤트 관련 메서드를 제공한다
예를들면 Alt를 이용한 단축키를 지정하여 버튼을 누를 수 있도록 한다든지
다른 이벤트를 발생했을 때 버튼이 놀려져 있는 시간을 지정한다든지
이미지를 기준으로 글자의 위치를 원하는 곳에 배치한다든지 등등 많은 메서드를 지원한다.

ex )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.awt.*;
import javax.swing.*;

class Round22_Ex01_Sub extends JFrame implements MouseListener {// JFrame을 상속받는다
private Container con;
private FlowLayout fl = new FlowLayout();
private JButton jb = new JButton("String");
private JButton jb1 = new JButton(im);
private JButton jb2 = new JButton("Str&Icon", im);
private ImageIcon im = new ImageIcon("aa.gif");
private ImageIcon im1 = new ImageIcon("bb.gif");
private ImageIcon im2 = new ImageIcon("cc.gif");

public Round22_Ex01_Sub() {
super("제목");
this.init();
this.start();
im = new ImageIcon("title.gif");
this.setIconImage(im.getImage());
this.setSize(300, 200);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension di = tk.getScreenSize();
Dimension di1 = this.getSize();
int xpos = ((int) di.getWidth() / 2 - (int) di1.getWidth() / 2);
int ypos = ((int) di.getHeight() / 2 - (int) di1.getHeight() / 2);
this.setLocation(xpos, ypos);
this.setVisible(true);
}

public void init() {
con = this.getContentPane();// 다중 Panel에서의 기본 작업영역 획득
con.setLayout(fl);
jb.setEnabled(false);// 버튼 사용금지 메서드
con.add(jb);
jb1.setMnemonic('a');// alt + a단축키 지정 메서드
con.add(jb1);
jb2.setHorizontalTextPosition(SwingConstants.RIGHT);
// jb2의 수평의 텍스트의 오른쪽으로 위치를 지정한다.
jb2.setVerticalTextPosition(SwingConstants.TOP);
// jb2의 수직의 텍스트의 맨위쪽으로 위치를 지정한다
// 위 두 포지션을 지정하면 오른쪽 상단위치로 텍스트가 이동한다.
jb2.setMnemonic('b');// alt + b 단축키 지정 메서드
jb2.setPressedIcon(im1);// 마우스로 눌렀을 때의 이미지변화 메서드
jb2.setRolloverIcon(im2);// 마우스를 올렸을 때의 이미지 변화 메서드
con.add(jb2);
// 폼 구성 영역 (초기화 영역)
}

public void start() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jb.addMouseListener(this);
// Frame의 x버튼을 눌렀을 때의 Event
// 이벤트나 기타 액션의 영역
}

// 기타 마우스 listener제외 ...
public void mouseClicked(MouseEvent e) {
if (e.getSource == jb) {
jb.doClick(5000);// 마우스 클릭 지속 메서드
}
}
}

public class Round22_Ex01 {
public static void main(String[] ar) {
Round22_Ex01_Sub es = new Round22_Ex01_Sub();
}
}

Share