JAVA/멀티스레드, 이벤트
java) 메모장 만들기 2) 나가기, 복사, 붙여 넣기, 잘라내기, 삭제, 글꼴크기, 모달창 띄우기
읽히는 블로그
2024. 5. 28. 20:48
▤ 목차
✔ 나가기
💻 코드로 보기
else if (e.getSource() == mnuExit) { // 나가기
int close = JOptionPane.showConfirmDialog(this, "종료하시겠습니까?", "종료",
JOptionPane.YES_NO_OPTION);
switch (close) {
case JOptionPane.YES_OPTION:
System.exit(0);
break;
case JOptionPane.NO_OPTION:
break;
}
👏 중요
✔복사
💻 코드로 보기
else if (e.getSource() == mnuCopy) { // 복사
// System.out.println(txtMemo.getSelectedText()); //모든 범위 복사된다.
copyText = txtMemo.getSelectedText(); //지정된 범위만 복사된다.
}
👏 중요
✔붙여넣기
💻 코드로 보기
else if (e.getSource() == mnuPaste) { // 붙여넣기
// txtMemo.setText(copyText); //전체 글자가 날라간다.
int position = txtMemo.getCaretPosition(); //커서가 있는 위치을 탐색한다.
txtMemo.insert(copyText, position);
}
👏 중요
✔잘라내기
💻 코드로 보기
else if (e.getSource() == mnuCut) { // 잘라내기
copyText = txtMemo.getSelectedText();
//copyText에 저장된 부분은 txtMemo에서 지움
int start = txtMemo.getSelectionStart();
int end = txtMemo.getSelectionEnd();
txtMemo.replaceRange("", start, end);
}
👏 중요
✔삭제
💻 코드로 보기
else if (e.getSource() == mnuDel) { // 삭제 (범위지정)
int start = txtMemo.getSelectionStart();
int end = txtMemo.getSelectionEnd();
txtMemo.replaceRange("", start, end);
}
👏 중요
✔글꼴 크기
💻 코드로 보기
else if (e.getSource() == mnuFontSize) { // 글꼴크기
String fontSize = JOptionPane.showInputDialog(this,"글꼴 크기: ","16"); //기본값 16
if (fontSize != null) {
try {
int fSize = Integer.parseInt(fontSize);
if (fSize == 0 ) {
JOptionPane.showMessageDialog(null, "다시 입력하세요");
return;
}
txtMemo.setFont(new Font(txtMemo.getFont().getName(), txtMemo.getFont().getStyle(), fSize));
} catch (Exception e2) {
JOptionPane.showConfirmDialog(this, "글꼴 크기를 정확히 입력하세요.");
}
}
}
👏 중요
✔모달창 띄우기
💻 코드로 보기
# 메인클래스
else if (e.getSource() == mnuAbout) { // 메모장이란..
new Ex49MemoAbout(this); //모달은 다음 코드 진행을 안한다.
System.out.println("About 호출 후 상황"); //modal과 modaless 구분(포커스)
}
# 모달창 클래스 (JDialog)
public class MemoAbout extends JDialog implements ActionListener { //메모장에 종속되는 창이다.
public MemoAbout(JFrame frame) {
//부모 프레임을 알려줘야한다.
super(frame);
setTitle("메모장이란");
setModal(true); // true Modal
initLayoutAbout();
setBackground(Color.LIGHT_GRAY);
setBounds(350, 350, 200, 200);
setVisible(true);
}
private void initLayoutAbout() {
JLabel lblMes = new JLabel("미니 메모장 ver 0.9");
JButton btnConfirm = new JButton("확인");
btnConfirm.addActionListener(this);
add("Center", lblMes);
add("South", btnConfirm);
}
@Override
public void actionPerformed(ActionEvent e) {
dispose(); //JDialog 담기
}
}
👏 Modal창
- true Modal
- false Modal 또는 Modaless
🪄구현 방법
1번 방법
// true Modal
setModal(true);
// false Modal, Modalsee
setModal(false);
2번 방법
생성자에서 super를 사용하려면 가장 상단에 적어야한다.
super(frame,"메모장이란",true); //Modal이다.
super(frame,"메모장이란",false); //false이면 Modaless이다