JAVA/Java.awt GUI
버튼 클릭 시 랜덤하게 색 변경 구현
public class Ex13changeColor extends JFrame{ Ex13changeColor(){ Container c = getContentPane(); c.setLayout(new GridLayout(4, 3)); JLabel[] j1 = new JLabel[12]; for(int i=0; i
Thread가 실행될때 프레임에 숫자 띄우기
* 쓰레드가실행될때 화면에1초단위로 정수를 출력하게끔 만들어라. * (정수0부터 시작하여 프레임에 정수를 문자열로 변경해서넣고 * try문안에서 1초씩 정지하게끔만 들어라 예외발생하면 쓰레드는 종료된다.(return) public class Ex12Thread extends JFrame { class Th extends Thread { JLabel j; Th (JLabel j){ this.j = j; } public void run() { int n = 0; while(true) { j.setText(Integer.toString(n)); n++; try { sleep(1000); } catch (Exception e) { return; } } } } Ex12Thread (){ Container c=ge..
swing을 이용하여 계산기 구현하기
겉모습만 구현했다. 기능은 추후에 구현 예정 public class Calculator extends JFrame{ Calculator (){ Container c = getContentPane(); JPanel j1 = new JPanel(); JPanel j2 = new JPanel(); JPanel j3 = new JPanel(); j1.setBackground(Color.orange); j2.setLayout(new GridLayout(4,4)); j3.setBackground(Color.pink); JLabel jl1 = new JLabel("입력"); JTextField jt1 = new JTextField(10); j1.add(jl1); j1.add(jt1); JLabel jl2 = new ..
Panel, Slider
패널은 프레임 안으로 들어갈 것이기 때문에 프레임 크기를 넘어갈 수 없다 setBounds public class Ex07Panel extends JFrame{ Ex07Panel(){ Container c = getContentPane(); setTitle("다이어로그"); //add Panel class c.add(new Dia(), BorderLayout.NORTH); //setting setSize(500, 350); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } class Dia extends Panel { JButton j = new JButton("이름입력"); JTextField j1 = new JTextField(1..
이미지와 콤보박스
public class Ex05ComboBox extends JFrame { String[] s = {"피자", "버거"}; ImageIcon[] im = { new ImageIcon("E://26.newJava/pizza.JPG"), new ImageIcon("E://26.newJava/burger.JPG") }; //ImageIcon 은 JLabel에 부착! JLabel j = new JLabel(im[0]); Ex05ComboBox(){ Container c = getContentPane(); c.setLayout(new FlowLayout()); JComboBox com = new JComboBox(s); //콤보박스 선택 시 사진 변경되는 이벤트 com.addActionListener(new A..
GUI 버튼 클릭이벤트 처리방법
ActionListener ActionListener를 상속받아 사용 MouseAdapter public class GuiEx20 extends JFrame { GuiEx20 (){ Container c = getContentPane(); c.setLayout(new FlowLayout()); JButton j1 = new JButton("버튼 1"); // 방법1 j1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { j1.setBackground(Color.CYAN); } }); //방법 2 j1.addActionListener(new MouseEx20()); //방법 3 j1.a..
GUI - JMenuBar, ImageIcon
메뉴 바 이벤트처리는 JMenuItem 에만 가능하다 public class GuiEx13menuBar extends JFrame{ GuiEx13menuBar() { Container c = getContentPane(); //1. bar 생성 JMenuBar jb = new JMenuBar(); //2. 메뉴 생성 JMenu j1 = new JMenu("File"); JMenu j2 = new JMenu("Edit"); JMenu j3 = new JMenu("Source"); //3. 서브메뉴 생성 JMenuItem j4 = new JMenuItem("new"); JMenuItem j5 = new JMenuItem("Undo Typing"); JMenuItem j6 = new JMenuItem("Red..
GUI 문자열과 체크박스
문자열 class MouseEx10 extends MouseAdapter { public void mouseEntered(MouseEvent e) { JLabel j = (JLabel) e.getSource();//이벤트 발생원천 j.setText("재밋어");//문자열 세팅 } public void mouseExited(MouseEvent e) { JLabel j = (JLabel) e.getSource(); j.setText("자바는"); } } public class GuiEx10JLabel extends JFrame { GuiEx10JLabel(){ Container c = getContentPane(); c.setLayout(new FlowLayout()); JLabel j = new JLabe..
Java GUI
GUI (graphic user interface) JFrame을 상속받아 만든다 배치관리자 종류 FlowLayout(): 순서대로(왼쪽에서 오른족으로 컴포넌트 배치) BorderLayout(): 동,서,남,북,중앙 GridLayout(): 2차원 CardLayout(): 카드 쌓아놓듯이 포개어 배치 public class GuiEx01 extends JFrame { //JFrame을 상속받아 만든다. GuiEx01() { super("Java");//제목설정 - 부모생성자(JFrame) 호출 setSize(300, 300);//프레임크기 설정 setVisible(true);//프레임 출력 메서드 JButton j1 = new JButton("버튼1"); JButton j2 = new JButton("버..