본문 바로가기
JAVA

GUI 예제

by Minius 2018. 3. 16.
반응형

package education.gui;


import java.awt.*;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;


public class Component2 {

private  class MouseListenerImp implements MouseListener {

@Override

public void mouseClicked(MouseEvent e) {

Label la = (Label)e.getSource();

System.out.println(la.getText());

cardLayout2.next(p12);

}


@Override

public void mousePressed(MouseEvent e) {

System.out.println("mousePressed");

}


@Override

public void mouseReleased(MouseEvent e) {

System.out.println("mouseReleased");

}


@Override

public void mouseEntered(MouseEvent e) {

System.out.println("mouseEntered");

}


@Override

public void mouseExited(MouseEvent e) {

System.out.println("mouseExited");

}

}


private Frame f1;

private Button b1;

private Toolkit tk;

private Dimension screenSize;

private TextField tf1,tf2,tf3;

private Label lb1,lb2,lb3;

private TextArea ta1;

private Checkbox ckb1,ckb2,ckb3,ckb4,ckb5,ckb6;

private CheckboxGroup ckbg1;

private List li1;

private Choice cho1;

private Panel p1,p2,p3,p4;

private Panel p11,p12;

private Label lb11,lb12,lb13,lb14,lb15;

private Label lb21,lb22,lb23,lb24,lb25;

private CardLayout cardLayout,cardLayout2;

private MenuBar menubar;

private Menu file,tool,rgbcolor,fontcolor;

private MenuItem miOpen,miSave,miExit,miBlack,miWhite,miGray,miRed;

Component2(){

f1 = new Frame("Component test2");

f1.setLayout(new FlowLayout());

f1.setBackground(new Color(224,173,193));

b1 = new Button("Confirm");

b1.setBackground(new Color(172,199,235));

b1.setForeground(Color.WHITE);

f1.add(b1);

lb1 = new Label("Name",Label.CENTER);

lb1.setBackground(new Color(172,199,235));

f1.add(lb1);

tf1 = new TextField("NAME : ",30);

f1.add(tf1);

ta1 = new TextArea("Input contents",10,30,TextArea.SCROLLBARS_BOTH);

f1.add(ta1);

ckb1 = new Checkbox("Sports", true); //체크박스, 중복선택 가능

f1.add(ckb1);

ckb2 = new Checkbox("BasketBall", false);

f1.add(ckb2);

ckb3 = new Checkbox("Soccer", true);

f1.add(ckb3);

ckbg1 = new CheckboxGroup(); //체크박스 그룹, 중복선택 불가능

ckb4 = new Checkbox("Sports", true,ckbg1);

f1.add(ckb4);

ckb5 = new Checkbox("BasketBall", false,ckbg1);

f1.add(ckb5);

ckb6 = new Checkbox("Soccer", true,ckbg1);

f1.add(ckb6);

li1 = new List(3,true); //리스트

f1.add(li1);

li1.add("ONE");

li1.add("T2O");

li1.add("THREE");

li1.add("FOUR", 1); //위치 지정 가능

li1.remove(1);

cho1 = new Choice(); //선택 목록

f1.add(cho1);

cho1.add("MON");

cho1.add("TUE");

cho1.add("WED");

cho1.add("THR");

cho1.add("FRI");

cho1.add("SAT");

cho1.add("SUN");

p1 = new Panel();

p1.setBackground(new Color(176, 231, 199));

f1.add(p1);

p2 = new Panel();

p2.setBackground(new Color(171, 216, 220));

f1.add(p2);

lb2 = new Label("ID : ");

lb3 = new Label("PASSWORD : ");

tf2 = new TextField(20);

tf3 = new TextField(20);

p1.add(lb2);

p1.add(tf2);

p2.add(lb3);

p2.add(tf3);

p3 = new Panel(new BorderLayout());

p3.setBackground(Color.DARK_GRAY);

f1.add(p3);

p3.add(p1,BorderLayout.NORTH);

p3.add(p2,BorderLayout.SOUTH);

p4 = new Panel(new GridLayout(2,2,10,10));

p4.setBackground(new Color(176, 231, 199));

p4.add(lb2);

p4.add(tf2);

p4.add(lb3);

p4.add(tf3);

cardLayout = new CardLayout();

p11 = new Panel(cardLayout);

f1.add(p11);

lb11 = new Label("CARD_1"); lb11.setBackground(Color.black);

lb12 = new Label("CARD_2"); lb12.setBackground(Color.blue);

lb13 = new Label("CARD_3"); lb13.setBackground(Color.CYAN);

lb14 = new Label("CARD_4"); lb14.setBackground(Color.DARK_GRAY);

lb15 = new Label("CARD_5"); lb15.setBackground(Color.GREEN);

p11.add(lb11,"1");

p11.add(lb12,"2");

p11.add(lb13,"3");

p11.add(lb14,"4");

p11.add(lb15,"5");

cardLayout2 = new CardLayout();

p12 = new Panel(cardLayout2);

f1.add(p12);

lb21 = new Label("CARD_1"); lb21.setBackground(Color.black);

lb22 = new Label("CARD_2"); lb22.setBackground(Color.blue);

lb23 = new Label("CARD_3"); lb23.setBackground(Color.cyan);

lb24 = new Label("CARD_4"); lb24.setBackground(Color.darkGray);

lb25 = new Label("CARD_5"); lb25.setBackground(Color.GREEN);

p12.add(lb21,"1");

p12.add(lb22,"2");

p12.add(lb23,"3");

p12.add(lb24,"4");

p12.add(lb25,"5");

lb21.addMouseListener(new MouseListenerImp2(cardLayout2,p12));

lb22.addMouseListener(new MouseListenerImp2(cardLayout2,p12));

lb23.addMouseListener(new MouseListenerImp2(cardLayout2,p12));

lb24.addMouseListener(new MouseListenerImp2(cardLayout2,p12));

lb25.addMouseListener(new MouseListenerImp2(cardLayout2,p12));

makeMenu();

}

private void makeMenu() {

menubar = new MenuBar();

f1.setMenuBar(menubar);

file = new Menu("File");

menubar.add(file);

//파일메뉴

miOpen = new MenuItem("Open");

file.add(miOpen);

miSave = new MenuItem("Save");

file.add(miSave);

file.addSeparator();

miExit = new MenuItem("Exit");

file.add(miExit);

//도구메뉴

tool = new Menu("Tool");

menubar.add(tool);

rgbcolor = new Menu("RGB color");

tool.add(rgbcolor);

miBlack = new MenuItem("Black");

rgbcolor.add(miBlack);

miWhite = new MenuItem("White");

rgbcolor.add(miWhite);

miGray = new MenuItem("Gray");

rgbcolor.add(miGray);

miRed = new MenuItem("Red");

rgbcolor.add(miRed);

fontcolor = new Menu("Fontcolor");

tool.add(fontcolor);

miWhite = new MenuItem("White");

fontcolor.add(miWhite);

miBlack = new MenuItem("Black");

fontcolor.add(miBlack);

}


void showCard() {

while(true) {

for(int i=1;i<6;i++) {

try {

Thread.sleep(45);

} catch (InterruptedException e) {

e.printStackTrace();

}

// cardLayout.next(p11);

cardLayout.show(p11, String.valueOf(i));

}

}

}

void loadFrame() {

Toolkit tk = Toolkit.getDefaultToolkit();

Dimension screenSize = tk.getScreenSize();

f1.setBounds(screenSize.width/2-400,screenSize.height/2-250,800,500);

f1.setVisible(true);

}


}



'JAVA' 카테고리의 다른 글

자원반납  (0) 2018.03.21
String / StringBuffer  (0) 2018.03.21
이벤트 핸들러 구현하는 방법  (0) 2018.03.16
MouseListener  (0) 2018.03.16
Event Handling  (0) 2018.03.16

댓글