Final Exam

자바프로그래밍1 기말고사
범위: 중간고사문제 포함 – 중간고사 이후 끝까지 (강의노트, 숙제, 수업블로그, 예제 중심으로)
일시: 2022/06/14(화) 2:30-3:30
장소: 2공 521 (2분반) 2공 524 (3분반)

5 Ways to Implement Event Listener

Slider(JSlider, JLabel)
1. event listener using anonymous class

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

// SliderFrame1 shows event listener using anonymous class
public class SliderFrame1 extends JFrame {

    JPanel panel = new JPanel(new BorderLayout());
    JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 20);
    JLabel label = new JLabel("Value : ", JLabel.CENTER);

    public SliderFrame1(String name) {
        super(name);

        this.add(panel);
        this.panel.add(slider, BorderLayout.CENTER);
        this.panel.add(label, BorderLayout.SOUTH);

        // using anonymous class
        this.slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSlider s = (JSlider)e.getSource();
                label.setText("Value : " + s.getValue());
            }
        });

        this.setSize(250, 150);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new SliderFrame1("SliderTest1");
    }
}

2. event listener using interface implements

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

// using implements ChangeListener
public class SliderFrame2 extends JFrame implements ChangeListener {

    JPanel panel = new JPanel(new BorderLayout());
    JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 20);
    JLabel label = new JLabel("Value : ", JLabel.CENTER);

    public SliderFrame2(String name) {
        super(name);

        this.add(panel);
        this.panel.add(slider, BorderLayout.CENTER);
        this.panel.add(label, BorderLayout.SOUTH);

        this.slider.addChangeListener(this); // implements ChangeListener

        this.setSize(250, 150);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new SliderFrame2("SliderTest2");
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        JSlider s = (JSlider)e.getSource();
        label.setText("Value : " + s.getValue());
    }
}

3. event listener using private inner class

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SliderFrame3 extends JFrame {

    JPanel panel = new JPanel(new BorderLayout());
    JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 20);
    JLabel label = new JLabel("Value : ", JLabel.CENTER);

    // using private inner class
    private class SliderChangeListener implements ChangeListener {
        @Override
        public void stateChanged(ChangeEvent e) {
            JSlider s = (JSlider)e.getSource();
            label.setText("Value : " + s.getValue());
        }
    }

    public SliderFrame3(String name) {
        super(name);
        this.add(panel);
        this.panel.add(slider, BorderLayout.CENTER);
        this.panel.add(label, BorderLayout.SOUTH);

        this.slider.addChangeListener(new SliderChangeListener()); // private class

        this.setSize(250, 150);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new SliderFrame3("SliderTest3");
    }
}

4. event listener using outer class

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

// using private inner class
class SliderChangeListener implements ChangeListener {
    JLabel label = null;
    public SliderChangeListener(JLabel label) {
        this.label = label;
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        JSlider s = (JSlider)e.getSource();
        label.setText("Value : " + s.getValue());
    }
}

public class SliderFrame4 extends JFrame {

    JPanel panel = new JPanel(new BorderLayout());
    JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 20);
    JLabel label = new JLabel("Value : ", JLabel.CENTER);

    public SliderFrame4(String name) {
        super(name);
        this.add(panel);
        this.panel.add(slider, BorderLayout.CENTER);
        this.panel.add(label, BorderLayout.SOUTH);

        this.slider.addChangeListener(new SliderChangeListener(this.label)); // outer class

        this.setSize(250, 150);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new SliderFrame4("SliderTest4");
    }
}

5. event listener using lambda

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

// SliderFrame5 shows event listener using lambda
public class SliderFrame5 extends JFrame {

    JPanel panel = new JPanel(new BorderLayout());
    JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 20);
    JLabel label = new JLabel("Value : ", JLabel.CENTER);

    public SliderFrame5(String name) {
        super(name);

        this.add(panel);
        this.panel.add(slider, BorderLayout.CENTER);
        this.panel.add(label, BorderLayout.SOUTH);

        // using lambda
        this.slider.addChangeListener(e -> { // lambda
                JSlider s = (JSlider)e.getSource();
                label.setText("Value : " + s.getValue());
        });

        this.setSize(250, 150);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new SliderFrame5("SliderTest5");
    }
}

Java Swing Component

https://www.javatpoint.com/java-swing
https://www.javatpoint.com/java-jbutton
https://www.javatpoint.com/java-jlabel
https://www.javatpoint.com/java-jtextfield
https://www.javatpoint.com/java-jcheckbox
https://www.javatpoint.com/java-jradiobutton
https://www.javatpoint.com/java-jcombobox
https://www.javatpoint.com/java-jtable
https://www.javatpoint.com/java-jslider
https://www.javatpoint.com/java-jdialog
https://www.javatpoint.com/java-jpanel
https://www.javatpoint.com/java-jfilechooser
https://www.javatpoint.com/java-jtogglebutton
https://www.javatpoint.com/java-jframe
https://www.javatpoint.com/java-jscrollpane

java.io.InputStream.read(byte[] b, int off, int len)

https://www.tutorialspoint.com/java/io/inputstream_read_byte_len.htm

The java.io.InputStream.read(byte[] b, int off, int len) method reads upto len bytes of data from the input stream into an array of bytes. If the parameter len is zero, then no bytes are read and 0 is returned; else there is an attempt to read at least one byte. If the stream is at the end of the file, the value returned is -1.

Parameters

  • b − The destination byte array.
  • off − The start offset in array b at which the data is written.
  • len − The number of bytes to read.

Return Value

The method returns the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

Exception

  • IOException − If an I/O error occurs.
  • NullPointerException − If b is null.
  • IndexOutOfBoundsException − If off is negative, len is negative, or len is greater than b.length – off.

 

Exceptions

ArrayIndexOutOfBoundsException 배열의 범위를 벗어난 접근할 때 발생

ArithmeticException 산술 연산 오류 에 의해 발생 (예를 들어, 0으로 정수를 나누는 경우)

NullPointerException null 객체를 사용하려고 시도할 때 발생

ClassCastException 변환할 수 없는 클래스로 객체 변환을 시도할 때 발생

NumberFormatException String을 숫자(number)로 변환할 수 없을 때 발생 (예를 들어, “1.23”을 Integer 변환을 시도할 때)

IllegalArgumentException 메소드 인자(argument) 유형을 잘못 사용할 경우

InputMismatchException (Scanner 클래스) 잘못된 입력일 때 발생

IOException 입출력(IO) 오류에 의해 발생

FileNotFoundException 지정된 경로에서 파일을 찾지 못할 때 발생

Difference between a module and a package

https://learnjava.co.in/what-is-the-difference-between-a-module-and-a-package/

Package Module
A package cannot be deployed by itself A module can be deployed by itself
A package groups together related classes A module groups together related packages
Packages are present in Java right from the beginning Modules were added by Java 9
Packages were added to keep related classes together and to allow developers to have a class with the same name in a different packages Modules were added for security reasons and to reduce the size of the JDK
Classes defined within a package are accessible via reflection even if they are private Classes defined within a module are not accessible outside the module via reflection
Packages do not require a package descriptor Modules require a module descriptor which is a file called module-info.java