ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JAVA_GUI
    JAVA 2020. 9. 3. 17:37
    반응형

    개념

    출력 결과물 또는 입력하는 방식을 콘솔이 아닌 컴포넌트(윈도우 어플리케이션)에서 작성하는 것

    종류

    AWT / SWING / Java2D / JavaFX

    LayoutManager

    • 종류는 flow / border / grid / card / grid이 있다.
    • setLayout(레이아웃 종류) 형식으로 지정한다.
    • JDialog, JFrame, JWindow은 기본 값이 border로 지정된다.
    • JApplet, JPanel은 기본 값이 flow로 지정된다.

    FlowLayout → 왼쪽에서 부터 오른쪽으로 배치하는 레이아웃

    BorderLayout → 동서남북으로 배치하는 레이아웃

    GridLayout → 격자 형식으로 배치하는 레이아웃

    Event Handling

    예제

    생성방식

    public static void main(String[] args){
    		JFrame f = new JFrame();
    	  f.setTitle("GUI");
    	  f.setSize(400,100);
    	  f.setVisible(true);
    
    }
    public class UrFrame extends JFrame{
    		UrFrame(){
    		setTitle("GUI");
    		setSize(400,100);
    		setVisible(true);
    	}
    }
    public static void main(String[] args){
    		new UrFrame();
    	}
    
    public class Gui1 extends JFrame{
    		Gui1(){
    		setTitle("GUI");
    		setSize(400,100);
    		setVisible(true);
    }
    public static void main(String[] args){
    		new Gui1();
    	}
    }

    패널 추가 / 버튼 추가

    public class Gui1 extends JFrame{
    		Gui1(){
    		setTitle("GUI");
    		JPanel p = new JPanel();
    		JLabel l = new JLabel("레이블");
    		JButton b = new JButton("버튼");
    		
    		p.add(l);
    		p.add(b);// 버튼을 패널에 추가
    		add(p);  // 패널을 프레임에 추가
    
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // x 버튼을 눌르면 종료
    		setSize(400,100);
    		pack(); // 사이즈에 맞게 조절
    		setVisible(true);
    }
    public static void main(String[] args){
    		new Gui1();
    	}
    }

    FlowLayout

    public class Gui1 extends JFrame{
    	Gui1(){
    		setTitle("Flow Layout");
    	  JPanel p = new JPanel(new FlowLayout());
    		p.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT) // 오 -> 왼
    		
    		JButton b1 = new JButton("Buttion 1");
    		JButton b2 = new JButton("Btn 2");
    		JButton b3 = new JButton("B 3");
    		JButton b4 = new JButton("B4");
    		JButton b5 = new JButton("Buttion 55555");
    
    		p.add(b1);
    		p.add(b2);
    		p.add(b3);
    		p.add(b4);
    		p.add(b5);
    
    		add(p);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(250,120);
    		setVisible(true);
    	}
    	
    	public static void main(String[] args){
    		new Gui1();
    	}
    }

    BorderLayout

    public class Gui1 extends JFrame{
    	Gui1(){
    		setTitle("Border Layout");
    		setLayout(new BorderLayout());
    		JButton b1 = new JButton("Buttion 1");
    		JButton b2 = new JButton("Btn 2");
    		JButton b3 = new JButton("B 3");
    		JButton b4 = new JButton("B4");
    		JButton b5 = new JButton("Buttion 55555");
    
    		add("East",b1);
    		add("South",b2);
    		add("West",b3);
    		add("North",b4);
    		add(b5, BorderLayout.CENTER);
    
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(300,300);
    		setVisible(true);
    	}
    	
    	public static void main(String[] args){
    		new Gui1();
    	}
    }

    GridLayout

    public class Gui1 extends JFrame{
    	Gui1(){
    		setTitle("Border Layout");
    		setLayout(new GridLayout(0,3));
    		JButton b1 = new JButton("Buttion 1");
    		JButton b2 = new JButton("Btn 2");
    		JButton b3 = new JButton("B 3");
    		JButton b4 = new JButton("B4");
    		JButton b5 = new JButton("Buttion 55555");
    
    		add(b1);
    		add(b2);
    		add(b3);
    		add(b4);
    		add(b5);
    		add(new JButton("마지막 버튼"));
    
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(300,300);
    		setVisible(true);
    	}
    	
    	public static void main(String[] args){
    		new Gui1();
    	}
    }

    Event Handling

    ActionListener

    public class Event1 extends JFrame{
        Event1(){
            setTitle("Event Handling");
    				// ActionListener al = ev -> System.out.println("람다");
    				// 람다식으로 간단하게 리스너 구현 가능
            ActionListener al = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("버튼 클릭");
                }
            }; 
            JButton btn = new JButton("클릭");
            btn.addActionListener(al);
            add(btn);
    				setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				setSize(500,300);
    				setVisible(true);
    	public static void main(String[] args)
    	{
    		new Event1();
    	}
    }

    Scrollbar

    public class Event1 extends JFrame{
        Event1(){
            setTitle("Scrollbar Event Handling");
    				JLabel lbl = new JLabel("", JLabel.CENTER);
    				JScrollBar sb = new JScrollBar(JScrollBar.VERTICAL);
    				sb.setValues(50,1,0,100);
    				sb.addAdjustmentListener(ev -> {
    					int display = ev.getValue();
    					lbl.setText("현재 위치" + display);
    				});
            add("North", lbl);
    				add(sb,BorderLayout.EAST);
    				setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				setSize(500,300);
    				setVisible(true);
    	public static void main(String[] args)
    	{
    		new Event1();
    	}
    }

    Text

    class Event1 extends JFrame{
        Event1(){
            setTitle("Event Handling");
            setLayout(new FlowLayout());
            JTextField tf1 = new JTextField(10);
            JTextField tf2 = new JTextField(10);
    
            JLabel lbl = new JLabel();
            JButton b1 = new JButton("Plus");
            JButton b2 = new JButton("Minus");
    
            ActionListener al = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ev) {
                    String s1 = tf1.getText();
                    String s2 = tf2.getText();
                    int a = Integer.parseInt(s1);
                    int b = Integer.parseInt(s2);
                    int result = 0;
                    if(ev.getSource().equals(b1)){
                        result = a + b;
                    }
                    else if(ev.getSource().equals(b2)){
                        result = a - b;
                    }
                    lbl.setText(String.valueOf(result));
                }
            };
            b1.addActionListener(al);
            b2.addActionListener(al);
            add(tf1);add(tf2);add(lbl);add(b1);add(b2);
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(500,120);
            setVisible(true);
        }
        public static void main(String[] args){
            new Event1();
        }
    }
    class Event1 extends JFrame implements ActionListener{
        JTextField tf1,tf2;
        JLabel lbl;
        JButton b1,b2;
        Event1(){
            setTitle("Event Handling");
            setLayout(new FlowLayout());
            tf1 = new JTextField(10);
            tf2 = new JTextField(10);
    
            lbl = new JLabel();
            b1 = new JButton("Plus");
            b2 = new JButton("Minus");
    
            b1.addActionListener(this);
            b2.addActionListener(this);
            add(tf1);add(tf2);add(lbl);add(b1);add(b2);
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(500,120);
            setVisible(true);
        }
        @Override
        public void actionPerformed(ActionEvent ev) {
            String s1 = tf1.getText();
            String s2 = tf2.getText();
            int a = Integer.parseInt(s1);
            int b = Integer.parseInt(s2);
            int result = 0;
            if(ev.getSource().equals(b1)){
                result = a + b;
            }
            else if(ev.getSource().equals(b2)){
                result = a - b;
            }
            lbl.setText(String.valueOf(result));
        }
        public static void main(String[] args){
            new Event1();
        }
    
    }

     

    반응형

    'JAVA' 카테고리의 다른 글

    JAVA_File  (0) 2020.09.04
    JAVA_Network  (0) 2020.09.03
    JAVA_Parallel Operation  (0) 2020.09.03
    JAVA_Stream  (0) 2020.09.03
    JAVA_Collection  (0) 2020.09.03

    댓글

Designed by Tistory.