Swing을 이용한 화면 구성
AWT(Abstract Window Toolkit) | JFC(Java Foundation Class Library) (Swing) |
플랫폼에 종속적인 컴포넌트 : 현재 시스템에 종속적으로 프래임 모형을 구축한다. | 플랫폼에 독립적인 컴포넌트 : 자바로 만들어져 시스템에 독립적으로 프레임 모형을 구축할 수 있다. |
중량의 컴포넌트 : 시스템 자원을 끌어와 사용하기 때문이다. | 경량의 컴포넌트 : 자바로 구현된 소스를 이용하기 때문이다. |
단일 프레임 : 컴포넌트를 표시하는 패널이 하나만 존재한다. | 다중 프레임 : 컴포넌트가 적재되는 패널이 계층별로 여러개 존재한다. |
사용 패키지 영역 : java.awt 등이 있다. | 사용 패키지 영역 : java.awt, javax.swing, javax.swing.border, javax.swing.table, javax.swing.tree 등이 있다. |
JFC는 다중패널로 구성된다.
import java.awt.*;
import java.awt.swing.*;
class Round22_Ex01_Sub extends JFrame {
private Container con;
private ImageIcon im;
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();
this.setLocation(
(int)(di.getWidth()/2 - di1.getWidth()/2)
, (int)(di.getHeight()/2 - di1.getHeight()/2)
);
this.setVisible(true);
}
public void init() {
con = this.getContentPane(); // 다중 Pane에서의 기본 작업영역 획득
// 폼 구성영역
}
public void start() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Frame의 X버튼을 눌렀을 때의 Event (WindowEvent Closing)
}
}
public class Round22_Ex01 {
public static void main(String[] args) {
new Round22_Ex01_Sub();
}
}
JFC Component 클래스
각 컴포넌트에 이미지를 추가 할 수 있는 예제
import java.awt.*;
import java.awt.swing.*;
class Round22_Ex02_Sub extends JFrame {
private Container con;
private JButton j1 = new JButton("Test");
private ImageIcon im, im1;
public Round22_Ex02_Sub() {
super("Test");
this.init();
this.start();
this.setIconImage(im.getImage());
this.setSize(300, 200);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension di = tk.getScreenSize();
Dimension di1 = this.getSize();
this.setLocation(
(int)(di.getWidth()/2 - di1.getWidth()/2)
, (int)(di.getHeight()/2 - di1.getHeight()/2)
);
this.setVisible(true);
}
public void init() {
im = new ImageIcon("title.gif");
im1 = new ImageIcon("title.gif");
con = this.getContentPane(); // 기본 작업영역 획득
con.setLayout(new BorderLayout()); // 작업영역에 Layout 설정
con.add("North",j1);
con.add("Center", new JButton("Test1", im1));
}
public void start() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint() {
}
}
public class Round22_Ex02 {
public static void main(String[] args) {
new Round22_Ex02_Sub();
}
}
툴팁 예제
import java.awt.*;
import java.awt.swing.*;
class Round22_Ex03_Sub extends JFrame {
private Container con;
private JButton jb = new JButton("Test");
private JButton jb1 = new JButton("Test1");
public Round22_Ex03_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
}
public void init() {
con = this.getContentPane(); // 기본 작업영역 획득
con.setLayout(new BorderLayout()); // con.setLayout(new FlowLayout());
jb.reshape(50, 50, 100, 100); // jb.setBounds(50, 50, 100, 100);
jb.setBackground(Color.yellow);
jb.setEnabled(true);
jb.setForeground(Color.black);
jb.setFont(new Font("굴림체", Font.BOLD, 20));
jb.setOpaque(false); //투명성 속성값을 사용하여 표시한다.
jb.setToolTipText("하하하");
con.add("Center",jb);
jb1.setToolTipText("호호호");
con.add("North",jb1);
jb.updateUI(); //combobox, list
}
public void start() {
}
public void paint() {
}
}
public class Round22_Ex03 {
public static void main(String[] args) {
new Round22_Ex03_Sub();
}
}
이벤트 관련 메서드 지원
import java.awt.*;
import java.awt.swing.*;
class Round22_Ex04_Sub extends JFrame implements MouseListener {
private Container con;
private FlowLayout fl = new FlowLayout();
private JButton jb = new JButton("Test");
private ImageIcon im = new ImageIcon("aa.gif");
private JButton jb1 = new JButton(im);
private JButton jb2 = new JButton("Str & Icon",im);
private ImageIcon im1 = new ImageIcon("bb.gif");
private ImageIcon im2 = new ImageIcon("cc.gif");
public Round22_Ex04_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
}
public void init() {
con = this.getContentPane();
con.setLayout(fl);
jb.setEnabled(true);
con.add(jb);
jb1.setMnemonic('a'); //alt + a (단축키 지정 Method)
con.add(jb1);
jb2.setHorizontalTextPosition(SwingConstants.RIGHT);
jb2.setVerticalTextPosition(SwingConstants.TOP);
jb2.setMnemonic('b'); //alt + b (단축키 지정 Method)
jb2.setPressedIcon(im1);
jb2.setRolloverIcon(im2);
con.add(jb2);
}
public void start() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jb.addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
if( e.getSource() == jb ) {
jb.doClick(5000);
}
}
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
}
public class Round22_Ex04 {
public static void main(String[] args) {
new Round22_Ex04_Sub();
}
}
토클을 지원하는 버튼
import java.awt.*;
import java.awt.swing.*;
class Round22_Ex05_Sub extends JFrame {
private Container con;
private GridLayout gl = new GridLayout(2, 2, 5, 5);
private JToggleButton tb = new JToggleButton("1", true);
private JToggleButton tb1 = new JToggleButton("2", false);
private JToggleButton tb2 = new JToggleButton("3", false);
private JToggleButton tb3 = new JToggleButton("4", false);
private ButtonGroup bg = new ButtonGroup();
public Round22_Ex05_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
}
public void init() {
con = this.getContentPane();
con.setLayout(gl);
bg.add(tb);
bg.add(tb1);
bg.add(tb2);
bg.add(tb3);
con.add(tb);
con.add(tb1);
con.add(tb2);
con.add(tb3);
}
public void start() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Round22_Ex05 {
public static void main(String[] args) {
new Round22_Ex05_Sub();
}
}
RadioButton 예제
import java.awt.*;
import java.awt.swing.*;
class Round22_Ex06_Sub extends JFrame {
private Container con;
private GridLayout gl = new GridLayout(2, 2, 5, 5);
private JRadioButton cb1 = new JRadioButton("1", true);
private JRadioButton cb2 = new JRadioButton("2");
private JRadioButton cb3 = new JRadioButton("3");
private JRadioButton cb4 = new JRadioButton("4", true);
private ButtonGroup bg = new ButtonGroup();
private ButtonGroup bg1 = new ButtonGroup();
public Round22_Ex06_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
}
public void init() {
con = this.getContentPane();
con.setLayout(gl);
bg.add(cb1);
bg.add(cb2);
bg1.add(cb3);
bg1.add(cb4);
con.add(cb1);
con.add(cb2);
con.add(cb3);
con.add(cb4);
}
public void start() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Round22_Ex06 {
public static void main(String[] args) {
new Round22_Ex06_Sub();
}
}
Border
- Empty, Line, Etched, Bevel, SoftBebel, Matte, Titled, Compound
import java.awt.*;
import java.awt.swing.*;
class Round22_Ex07_Sub extends JFrame {
private Container con;
private FlowLayout fl = new FlowLayout();
private JButton bt = new JButton("TestTestTestTest");
//private BevelBorder bb = new BevelBorder(BevelBorder.RAISED, Color.green, Color.red, Color.blue, Color.darkgray);
//private EmptyBorder bb = new EmptyBorder(3,5,7,9);
//private EtchedBorder bb = new EtchedBorder(EtchedBorder.LOWERED, Color.green, Color.red);
//private LineBorder bb = new LineBorder(Color.red, 5, true);
//private MatteBorder bb = new MatteBorder(5 ,10, 15, 20, new ImageIcon("bbb.gif"));
//private TitledBorder bb = new TitledBorder(new LineBorder(Color.red, 3),"Title",TitledBorder.CENTER, TitledBorder.BELOW_BOTTOM);
private CompoundBorder bb = new CompoundBorder(
new LineBorder(Color.red, 3)
, new SoftBevelBorder(SoftBevelBorder.RAISED)
);
public Round22_Ex07_Sub() {
super("Test");
this.init();
this.start();
this.setSize(400, 300);
this.setVisible(true);
}
public void init() {
con = this.getContentPane();
con.setLayout(fl);
bt.setBorder(bb);
con.add(bt);
}
public void start() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Round22_Ex07 {
public static void main(String[] args) {
new Round22_Ex07_Sub();
}
}
메뉴
import java.awt.*;
import java.awt.swing.*;
import java.awt.border.*;
class Round22_Ex08_Sub extends JFrame {
private JMenuBar jmb = new JMenuBar();
private JMenu file = new JMenu("파일");
private JMenuItem fnew = new JMenuItem("파일");
private JMenuItem fopen = new JMenuItem(new ImageIcon("bbb.gif"));
private JMenuItem fexit = new JMenuItem("종료", new ImageIcon("ccc.gif"));
private JMenu edit = new JMenu("수정");
private JMenu esize = new JMenu("크기");
private JRadioButtonMenuItem es10 = new JRadioButtonMenuItem("10");
private JRadioButtonMenuItem es20 = new JRadioButtonMenuItem("20");
private JRadioButtonMenuItem es30 = new JRadioButtonMenuItem("30");
private ButtonGroup bg = new ButtonGroup();
private JMenu ecolor = new JMenu("색상");
private JCheckboxMenuItem ecred = new JCheckboxMenuItem("RED");
private JCheckboxMenuItem ecgreen = new JCheckboxMenuItem("GREEN");
private JCheckboxMenuItem ecblue = new JCheckboxMenuItem("BLUE");
private JMenu help = new JMenu("도움말");
public Round22_Ex08_Sub() {
super("Test");
this.init();
this.start();
this.setSize(400, 300);
this.setVisible(true);
}
public void init() {
this.setJMenuBar(jmb);
fexit.setArmed(true);
file.add(fnew);
file.addSeparator();
file.add(fopen);
file.addSeparator();
file.add(exit);
jmb.add(file);
edit.setBorder(new LineBorder(Color.red, 3));
esize.setBorder(new BevelBorder(BevelBorder.RAISED));
ecolor.setBorder(new BevelBorder(BevelBorder.RAISED));
bg.add(es10);
bg.add(es20);
bg.add(es30);
esize.add(es10);
esize.add(es20);
esize.add(es30);
edit.add(esize);
edit.addSeparator();
ecolor.add(ecred);
ecolor.add(ecgreen);
ecolor.add(ecblue);
edit.add(ecolor);
jmb.add(edit);
jmb.add(help);
}
public void start() {
}
}
public class Round22_Ex08 {
public static void main(String[] args) {
new Round22_Ex08_Sub();
}
}
Box ( 컴포넌트를 박스로 묶어 관리)
import java.awt.*;
import java.awt.swing.*;
import java.awt.border.*;
class Round22_Ex09_Sub extends JFrame {
private Container con;
private FlowLayout fl = new FlowLayout();
private JButton[] bt = new JButton[6];
private Box box, box1, box2;
public Round22_Ex09_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
}
public void init() {
con = this.getContentPane();
con.setLayout(fl);
for(int i=0; i<6; i++) {
bt[i] = new JButton(String.valueOf(i + 1));
}
box = Box.createVerticalBox();
box.add(Box.createVerticalStruct(10)); //10픽셀의 사이 간격을 추가한다.
for(int i=0; i<3; i++) {
box.add(bt[i]);
box.add(Box.createVerticalStruct(10));
}
//con.add(box);
box1 = Box.createHorizontalBox();
box1.add(Box.createHorizontalStruct(170));
for(int i=3; i<6; i++) {
box1.add(bt[i]);
box1.add(Box.createHorizontalStruct(10));
}
//con.add(box1);
box2 = Box.createHorizontalBox();
box2.add(box);
box2.add(Box.createHorizontalStruct(30));
box2.add(box1);
con.add(box2);
}
public void start() {
}
}
public class Round22_Ex09 {
public static void main(String[] args) {
new Round22_Ex09_Sub();
}
}
JDialog
import java.awt.*;
import java.awt.event.*;
import java.awt.swing.*;
import java.awt.border.*;
class Round22_Ex10_Sub extends JFrame implements ActionListener {
private Container con;
private FlowLayout fl = new FlowLayout();
private JLabel lb = new JLabel("ID : ", JLable.RIGHT);
private JTextField tf = new JTextField(10);
private JDialog dlg = new JDialog(this, "확인");
private Container dlgcon;
private JLabel dlglb = new JLabel("사용할 수 있는 ID입니다.",JLabel.CENTER);
private BorderLayout dlglb = new BorderLayout();
public Round22_Ex10_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
}
public void init() {
con = this.getContentPane();
con.setLayout(fl);
con.add(lb);
con.add(tf);
dlgcon = dlg.getContentPane();
clgcon.setLayout(dlglb);
dlglb.setBorder(new BevelBorder(BevelBorder.RAISED));
dlgcon.add("Center",dlglb);
dlg.setSize(200, 150);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension di = tk.getScreenSize();
Dimension di1 = dlg.getSize();
dlg.setLocation(
(int)(di.getWidth()/2 - di1.getWidth()/2)
, (int)(di.getHeight()/2 - di1.getHeight()/2)
);
}
public void start() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tf.addActionListener(this);
dlg.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if( e.getSource() == tf ) {
dlg.setVisible(true);
}
}
}
public class Round22_Ex10 {
public static void main(String[] args) {
new Round22_Ex10_Sub();
}
}
JFileChooser, JColorChooser
import java.awt.*;
import java.awt.swing.*;
class Round22_Ex11_Sub extends JFrame {
private JColorChooser jcc = new JColorChooser();
private JFileChooser jfc = new JFileChooser("C:\\");
public Round22_Ex11_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
Color cc = jcc.showDialog(this,"나의 색상선택", Color.black);
System.out.println("선택한 색상 = " + cc);
jcc.setColor(cc); // 기본 색상 지정해 두기
System.out.println("다시 얻어오기 = " + jcc.getColor());
jfc.setDialogTitle("호호호");
jfc.setMultiSelectionEnabled(true); // 다중 선택 가능하게 만들기
//버튼에 툴팁 표시하기
jfc.setApproveButtonToolTipText("하하하라는 글자가찍혀있네요");
jfc.showDialog(this, "하하하");
//jfc.showOpenDialog(this);
//jfc.showSaveDialog(this);
}
public void init() {
}
public void start() {
}
}
public class Round22_Ex11 {
public static void main(String[] args) {
new Round22_Ex11_Sub();
}
}
JTextArea, JScrollPane
import java.awt.*;
import java.awt.swing.*;
class Round22_Ex12_Sub extends JFrame {
private Container con;
private BorderLayout bl = new BorderLayout();
private JButton bt = new JButton("1");
private JButton bt1 = new JButton("1");
private JTextArea ta = new JTextArea();
private JScrollPane jsp = new JScrollPane( ta );
public Round22_Ex12_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
}
public void init() {
con = this.getContentPane();
con.setLayout(bl);
JPanel jp = new JPanel(new FlowLayout(FlowLayout.RIGHT));
jp.add(bt);
jp.add(bt1);
con.add("North", jp);
jsp.setWheelScrollingEnabled(true); // 스크롤을 가능하게 만든다.
con.add("Center" , jsp);
}
public void start() {
}
}
public class Round22_Ex12 {
public static void main(String[] args) {
new Round22_Ex12_Sub();
}
}
JComboBox
import java.awt.*;
import java.awt.swing.*;
class Round22_Ex13_Sub extends JFrame {
private Container con;
private String[] str = {"AAA", "BB", "CCC", "DDD", "EEE"};
private JComboBox jcb = new JComboBox(str);
public Round22_Ex13_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
System.out.println("얻어온 객체 = " + (String)jcb.getItemAt(2));
System.out.println("포함된 객체 개수 = " + jcb.getItemCount());
System.out.println("현재 선택된 객체 = " + (String)jcb.getSelectedItem());
try { Thread.sleep(2000); } catch( InterruptedException e) {}
jcb.setSelectedIndex(2);
jcb.setSelectedItem("BBB");
jcb.showPopup();
con.repaint();
try { Thread.sleep(2000); } catch( InterruptedException e) {}
jcb.hidePopup();
con.repaint();
}
public void init() {
Vector vc = new Vector();
vc.add("AAA");
vc.add("BBB");
jcb = new JComboBox(vc);
con = this.getContentPane();
con.setLayout(new FlowLayout());
jcb.addItem("FFF");
jcb.setEditable(true);
jcb.setMaximumRouCount(3);
con.add(jcb);
}
public void start() {
}
}
public class Round22_Ex13 {
public static void main(String[] args) {
new Round22_Ex13_Sub();
}
}
JInternalFrame : 프레임 내에 있는 작은 프레임
import java.awt.*;
import java.awt.swing.*;
class Round22_Ex14_Sub extends JFrame {
private Container con;
private BorderLayout bl = new BorderLayout();
private JButton bt = new JButton("1");
private JButton bt1 = new JButton("2");
private JPanel jp = new JPanel(null);
// 최대, 최소, 아이콘버튼, x버튼
private JInternalFrame jif = new JInternalFrame("제목", true, true, true, true);
public Round22_Ex14_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
}
public void init() {
con = this.getContentPane();
con.setLayout(bl);
con.add("West",bt);
con.add("South",bt1);
Container cc = jif.getContentPane();
cc.setLayout(new FlowLayout());
cc.add(new JButton("1"));
cc.add(new JButton("2"));
//jif.setClosable(false);
jif.setFrameIcon(new ImageIcon("bbb.gif"));
//jif.setIconifiable(false); //아이콘화 버튼의 사용유뮤
//jif.setMaximizable(false); //최대 최소 버튼의 사용유무
try {
jif.setMaximum(true); // 기본적으로 최대크기 보여줄 것을 설정
} catch (Exception e){}
jif.setSize(100, 80);
jif.setVisible(true);
jp.add("Center",jif);
con.add("Center",jp);
}
public void start() {
}
}
public class Round22_Ex14 {
public static void main(String[] args) {
new Round22_Ex14_Sub();
}
}
이미지 중심 글자 배치
import java.awt.*;
import java.awt.swing.*;
class Round22_Ex15_Sub extends JFrame {
private Container con;
private JLabel lb = new JLabel();
private JLabel lb1 = new JLabel(new ImageIcon("bb.gif"));
private JLabel lb2 = new JLabel(new ImageIcon("bb.gif"), JLabel.LEFT);
private JLabel lb3 = new JLabel("Test");
private JLabel lb4 = new JLabel("Test1", JLabel.CENTER);
private JLabel lb4 = new JLabel("Test2", new ImageIcon("aa.gif"), JLabel.CENTER);
// 최대, 최소, 아이콘버튼, x버튼
private JInternalFrame jif = new JInternalFrame("제목", true, true, true, true);
public Round22_Ex15_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
System.out.println(lb5.getDisabledIcon());
System.out.println(lb5.getIcon());
}
public void init() {
con = this.getContentPane();
con.setLayout(new GridLayout(3, 2));
con.add(lb);
con.add(lb1);
con.add(lb2);
con.add(lb3);
con.add(lb4);
lb5.setHorizontalTextPosition(SwingConstants.LEFT);
lb5.setVerticalTextPosition(SwingConstants.BOTTOM);
con.add(lb5);
}
public void start() {
}
}
public class Round22_Ex15 {
public static void main(String[] args) {
new Round22_Ex15_Sub();
}
}
JList 셀관리
import java.awt.*;
import java.awt.swing.*;
class Round22_Ex16_Sub extends JFrame {
private Container con;
private String[] str = {"AAA", "BBB", "CCC", "DDD", "EEE"};
private JList li = new JList(str);
private JScrollPane jsp = new JScrollPane( li );
public Round22_Ex16_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
System.out.println(li.getCellBounds(1, 3));
try { Thread.sleep(2000); } catch( InterruptedException e ) { }
li.clearSelection();
try { Thread.sleep(2000); } catch( InterruptedException e ) { }
String[] str1 = {"aaa", "bbb", "ccc", "ddd", "eee"};
li.setListData(str1);
//li.updateUI();
li.setSelectionBackground(Color.red);
li.setSelectionForeground(Color.yellow);
li.setSelectedIndex(3);
}
public void init() {
con = this.getContentPane();
con.setLayout(new FlowLayout());
//li.setVisibleRowCount(3);
li.addSelectionInteval(1, 3);
con.add(li);
}
public void start() {
}
}
public class Round22_Ex16 {
public static void main(String[] args) {
new Round22_Ex16_Sub();
}
}
JOptionPane
import java.awt.*;
import java.awt.swing.*;
class Round22_Ex17_Sub extends JFrame {
private Container con;
private JOptionPane jop = new JOptionPane();
public Round22_Ex17_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
/*
System.out.println(JOptionPane.showConfirmDialog(
this
, "저장하시겠습니까?"
, "저장"
, JOptionPane.OK_CANCEL_OPTION
, JOptionPane.QUESTION_MESSAGE
, new IconImage("bbb.gif")
));
*/
/*
System.out.println(JOptionPane.showInternalConfirmDialog(
this
, "아이디 ? "
, "ID"
, JOptionPane.INFORMATION_MESSAGE
, new IconImage("bbb.gif")
, new String[] {"AAA", "BBB", "CCC"}
, "BBB"
));
*/
//System.out.println(JOptionPane.showInternalConfirmDialog(con, "저장하시겠습니까?"));
//System.out.println(JOptionPane.showInternalConfirmDialog(con, "이름 = "));
//job.showMessageDialog(this, "오류가 발생했습니다.", "오류", JOptionPane.ERROR_MESSAGE);
System.out.println(job.showOptionDialog(
this
, "선택하세요 ? "
, "물건선택"
, JOptionPane.YES_NO_OPTION
, JOptionPane.QUESTION_MESSAGE
, new IconImage("bbb.gif")
, new String[] {"a", "b", "c", "d", "e", "f"}
, "a"
));
}
public void init() {
con = this.getContentPane();
con.setLayout(new BorderLayout());
}
public void start() {
}
}
public class Round22_Ex17 {
public static void main(String[] args) {
new Round22_Ex17_Sub();
}
}
팝업 메뉴 관리
import java.awt.*;
import java.awt.swing.*;
import java.awt.event.*;
class Round22_Ex18_Sub extends JFrame implements MouseListener {
private Container con;
private JLabel lb = new JLabel("메모를 하십시오");
private JTextArea ta = new JTextArea();
private JScrollPane jsp = new JScrollPane(ta);
private JPopupMenu jpm = new JPopupMenu();
private List<JMenuItem> jmis = Arrays.asList(
new JMenuItem("복사")
, new JMenuItem("붙여넣기")
, new JMenuItem("잘라내기")
);
public Round22_Ex18_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
}
public void init() {
for(int i=0; i<jmis.size(); i++) {
jpm.add(jmis.get(i));
if( i < jmis.size()-1 ) {
jpm.addSeparator();
}
}
con = this.getContentPane();
con.setLayout(new BorderLayout());
con.add("North", lb);
con.add("Center", jsp);
}
public void start() {
this.setDefaultCloseOption(JFrame.EXIT_ON_CLOSE);
ta.addMouseListener(this);
}
public void mouseClicked(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) {
if( e.getSource() == ta ) {
//팝업 띄우기
if( e.isPopupTrigger() ) {
jpm.show(ta, e.getX(), e.getY());
}
}
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
}
public class Round22_Ex18 {
public static void main(String[] args) {
new Round22_Ex18_Sub();
}
}
JTabbedPane (CardLayout의 변형)
import java.awt.*;
import java.awt.swing.*;
class Round22_Ex19_Sub extends JFrame {
private Container con;
private JLabel lb = new JLabel("이것은 탭 팬입니다.");
private JButton bt = new JButton("확인");
private JButton bt1 = new JButton("취소");
privaet JPanel jp = new JPanel(new FlowLayout(FlowLayout.RIGHT));
privaet JPanel jp1 = new JPanel(new BorderLayout());
private JTabbedPane jtp = new JTabbedPane(
JTabbedOane.BOTTOM
, JTabbedPane.SCROLL_TAB_LAYOUT
);
private JButton bt2 = new JButton("첫번째 Tab");
private JButton bt3 = new JButton("두번째 Tab");
private JButton bt4 = new JButton("세번째 Tab");
private JButton bt5 = new JButton("네번째 Tab");
private JButton bt6 = new JButton("다섯번째 Tab");
public Round22_Ex19_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
}
public void init() {
con = this.getContentPane();
con.setLayout(new BorderLayout(5, 5));
con.add("North", lb);
jp.add(bt);
jp.add(bt1);
con.add("South", jp);
jtp.add(bt2);
jtp.add(bt3);
jtp.add(bt4, 1);
jtp.add("Title",bt5);
jtp.add("Title1", new ImageIcon("bbb.gif"), bt6 , "여긴 아이콘이 있습니다.");
jp1.add("Center",jtp);
jtp.setBackgroundAt(2, Color.red);
jtp.setDisplayMnemonicIndexAt(3, 0);
jpt.setEnabledAt(2, false);
jtp.setForegroundAt(4, Color.yellow);
jtp.setMnemonicAt(3, 65);
jtp.setTitleAt(0, "Test");
con.add("Center",jp1);
}
public void start() {
}
}
public class Round22_Ex19 {
public static void main(String[] args) {
new Round22_Ex19_Sub();
}
}
JTextArea, MouseEvent 오른쪽 마우스 수행
import java.awt.*;
import java.awt.swing.*;
import java.awt.event.*;
class Round22_Ex20_Sub extends JFrame implements MouseListener {
private Container con;
private JLabel lb = new JLabel("메모장", JLabel.CENTER);
private JButton bt = new JButton("확인");
private JTextArea ta = new JTextArea();
private JScrollPane jsp = new JScrollPane(ta);
public Round22_Ex20_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
}
public void init() {
con = this.getContentPane();
con.setLayout(new BorderLayout());
con.add("North", lb);
con.add("South", bt);
ta.setDragEnabled(true);
ta.setFocusAccelerator('r');
ta.setSelectedTextColor(Color.red);
ta.setSlectionColor(Color.yellow);
con.add("Center", jsp);
}
public void start() {
this.setDefaultCloseOption(JFrame.EXIT_ON_CLOSE);
ta.addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
if( e.getSource() == ta ) {
if( e.getClickCount() == 2 ) {
//ta.paste();
//System.out.println(ta.getCaretPositon());
//ta.moveCaretPosition(0);
ta.setEditable(false);
}
}
}
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) {
if( e.getSource() == ta ) {
//팝업 띄우기
if( e.isPopupTrigger() ) {
// ta.copy();
// ta.setCaretPostion(ta.getText().length());
// ta.setEditable(true);
// ta.setSelectionStart(3);
// ta.setSelectionEnd(7);
ta.setText("아무개");
}
}
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
}
public class Round22_Ex20 {
public static void main(String[] args) {
new Round22_Ex20_Sub();
}
}
JTextField, JPasswordField
import java.awt.*;
import java.awt.swing.*;
import java.awt.event.*;
class Round22_Ex21_Sub extends JFrame implements ActionListener {
private Container con;
private JTextArea ta = new JTextArea("abcdefghijklmnopqrstuvwxyz");
private JScrollPane jsp = new JScrollPane(ta);
private JLabel lb = new JLabel("ID : ", JLabel.RIGHT);
private JTextField tf = new TextField();
private JLabel lb1 = new JLabel("Pass : ", JLabel.RIGHT);
private JPasswordField jpf = new JPasswordField();
public Round22_Ex21_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
try { Thread.sleep(2000); } catch( InterruptedException e) { }
ta.replaceRange("ABCD", 0, 4);
}
public void init() {
con = this.getContentPane();
con.setLayout(new BorderLayout());
JPane jp = new JPane(new BorderLayout());
jp.add("West", lb);
tf.setHorizontalAlignment(JTextField.CENTER);
jp.add("Center", tf);
JPane jp1 = new JPane(new BorderLayout());
jp1.add("West", lb1);
jpf.setEchoChar('%');
jp1.add("Center", jpf);
con.add("South", jp1);
con.add("North", jp);
con.add("Center", jsp);
}
public void start() {
jpf.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if( e.getSource() == jpf ) {
String str = new String(jpf.getPassword());
System.out.println("얻어온 비밀번호 = " + str);
}
}
}
public class Round22_Ex21 {
public static void main(String[] args) {
new Round22_Ex21_Sub();
}
}