728x90

SwingConstants / ImageIcon (이미지 집어넣기)

import javax.swing.*;
import java.awt.*;

public class Test1 extends JFrame {
    Test1(){
        Container c=getContentPane();
        setVisible(true);
        c.setLayout(new FlowLayout());

        JLabel j = new JLabel("안녕하세요");

        ImageIcon i = new ImageIcon("C:\\\\Users\\\\Kang\\\\Pictures/BB1hd16x.jfif");
        JLabel j1=new JLabel(i); // 사진은 항상 JLable 매개변수로 들어온다

        JLabel j2=new JLabel("월요일임",SwingConstants.CENTER);

        c.add(j);
        c.add(j1);
        c.add(j2);

    }

    public static void main(String[] args) {
        new Test1();
    }
}

 

 

JRadioButton / JCheckBox (체크박스 선택 안했을시 다음 선택 못함)

import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
class Check implements ItemListener {

    JRadioButton b1, b2, b3;

    Check(JRadioButton b1, JRadioButton b2, JRadioButton b3) {
        this.b1 = b1;
        this.b2 = b2;
        this.b3 = b3;
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
            //체크박스 선택했을 때
            //라디오버튼 활성화
            b1.setEnabled(true);
            b2.setEnabled(true);
            b3.setEnabled(true);
        } else {  //체크박스 선택 안했을 시
            b1.setEnabled(false);
            b2.setEnabled(false);
            b3.setEnabled(false);
        }
    }
}

public class Test1 {
    public static void main(String[] args) {

        JFrame j = new JFrame();
        j.setLayout(new GridLayout(0, 1));

        JCheckBox jc = new JCheckBox("공부선택");

        JRadioButton b1 = new JRadioButton("java");
        JRadioButton b2 = new JRadioButton("python");
        JRadioButton b3 = new JRadioButton("c++");

        //라디오버튼이기때문 하나만 선택가능
        ButtonGroup g1 = new ButtonGroup();
        g1.add(b1);
        g1.add(b2);
        g1.add(b3);

        j.add(jc);
        j.add(b1);
        j.add(b2);
        j.add(b3);
        //라디오버튼 비활성화
        b1.setEnabled(false);
        b2.setEnabled(false);
        b3.setEnabled(false);

        jc.addItemListener(new Check(b1, b2, b3));

        j.setVisible(true);

        b1.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange() == ItemEvent.SELECTED){
                    System.out.println("자바~");
                }
            }
        });
        b2.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange() == ItemEvent.SELECTED){
                    System.out.println("파이썬~");
                }
            }
        });
        b3.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange() == ItemEvent.SELECTED){
                    System.out.println("씨플~");
                }
            }
        });
    }
}

 

 

 

JTextField / JTextArea (입력값 텍스트박스에 저장 / like 계산기?)

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Test1 extends JFrame {
    JTextField j = new JTextField(20);
    JTextArea j1 = new JTextArea(10,20);

    Test1() {
        Container c = getContentPane();
        setVisible(true);
        c.setLayout(new FlowLayout());

        JLabel jl=new JLabel("enter키 입력");
        c.add(j);
        c.add(jl);
        c.add(new JScrollPane(j1));

        j.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JTextField jt=(JTextField) e.getSource();
                j1.append(jt.getText()); // 텍스트필으데 입력한 문자를 textarea에 삽입

                jt.setText(""); // 공백으로 초기화
            }
        });

    }

    public static void main(String[] args) {
        new Test1();

    }
}

728x90

+ Recent posts