package pack.awt;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
//Swing
//JFrame 상속
//Swing은 J만 앞에 붙여준다(JButton, JLabel...)
public class SwingTest1 extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JButton btn;
JLabel lbl;
int count;
public SwingTest1() {
btn = new JButton("클릭");
btn.setMnemonic(KeyEvent.VK_C); //단축키
btn.addActionListener(this);
JPanel pn = new JPanel();
pn.setLayout(new GridLayout(2,1)); //2행1열
pn.setBorder(BorderFactory.createEmptyBorder(30,30,10,30)); //여백주기
pn.add(btn);
lbl = new JLabel("버튼 클릭 수 : " + count);
pn.add(lbl);
getContentPane().add(pn, BorderLayout.CENTER); //가운데 정렬
setBounds(200,200,300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
count++;
lbl.setText("버튼 클릭 수 : " + count);
}
public static void main(String[] args) {
new SwingTest1();
}
}
===================================================================================
package pack.awt;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
//JPanel 상속
public class SwingTest2 extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
JButton btnR, btnG, btnB;
JMenuBar menubar;
JMenuItem menuMes, menuOk, menuInput;
JTextArea txtArea = new JTextArea("결과 출력", 10 ,50);
public SwingTest2() {
this.setLayout(new BorderLayout());
btnR = new JButton("Red");
btnG = new JButton("Green");
btnB = new JButton("Blue");
JPanel pn = new JPanel();
pn.add(btnR);
pn.add(btnG);
pn.add(btnB);
btnR.addActionListener(this);
btnG.addActionListener(this);
btnB.addActionListener(this);
this.add("South",pn); // 남쪽에 위치
this.add("Center",txtArea);
menuProcess();
}
private void menuProcess() {
menubar = new JMenuBar();
JMenu menu = new JMenu("Dialog test"); //인자 : 메뉴이름
menuMes = new JMenuItem("Message");
menuOk = new JMenuItem("menuOk");
menuInput = new JMenuItem("menuInput");
menu.add(menuMes);
menu.add(menuOk);
menu.add(menuInput);
menubar.add(menu);
//리스너 장착
menuMes.addActionListener(this);
menuOk.addActionListener(this);
menuInput.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnR){
txtArea.setBackground(Color.RED);
}else if(e.getSource() == btnG){
txtArea.setBackground(Color.GREEN);
}else if(e.getSource() == btnB){
txtArea.setBackground(Color.BLUE);
}else if(e.getSource() == menuMes){
JOptionPane.showMessageDialog(this, "메시지", "알림", JOptionPane.INFORMATION_MESSAGE); //다이얼로그 창(alert창)
}else if(e.getSource() == menuOk){
int re = JOptionPane.showConfirmDialog(this, "버튼 선택","골라",JOptionPane.YES_NO_CANCEL_OPTION); // 예 아니오 취소 창
switch (re) {
case JOptionPane.YES_OPTION:txtArea.append("\n예 선택"); break;
case JOptionPane.NO_OPTION:txtArea.append("\n노 선택"); break;
case JOptionPane.CANCEL_OPTION:txtArea.append("\n취소"); break;
default:break;
}
}else if(e.getSource() == menuInput){
String str = JOptionPane.showInputDialog(this,"이름 입력","정확히"); // 텍스트를 입력할 수 있는 창(문자열로 리턴)
JOptionPane.showMessageDialog(this, "입력값 : " + str);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("대화상자 연습");
SwingTest2 test2 = new SwingTest2(); //JPanel
//... JPanel이 여러개 있다고 가정
frame.getContentPane().add(test2, "Center"); // 가운데 정렬
frame.setJMenuBar(test2.menubar);
frame.setBounds(200,200,400,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
'자바' 카테고리의 다른 글
JAVA RMI (0) | 2014.03.17 |
---|---|
모듈별(단위별) 테스트 (0) | 2014.02.27 |
AWT 레이아웃의 종류 (0) | 2014.02.25 |
AWT를 이용한 간단한 메모장 작성 (0) | 2014.02.25 |
동기화 synchronized (0) | 2014.02.25 |