ChangeListener, KeyListener, ActionListener

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class MainFrame extends JFrame implements ChangeListener, KeyListener, ActionListener {

JPanel panel = new JPanel(new BorderLayout());
JPanel panel1 = new JPanel(new FlowLayout());
 JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 20);
JLabel label = new JLabel(“Value : “, JLabel.CENTER);
 JTextField textfield = new JTextField(10);
 JButton button = new JButton(“Close”);

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

this.add(panel);
panel1.add(label);
panel1.add(textfield);
panel.add(slider, BorderLayout.CENTER);
panel.add(panel1, BorderLayout.NORTH);
panel.add(button, BorderLayout.SOUTH);

slider.addChangeListener(this); // ChangeListener stateChanged
textfield.addKeyListener(this); // KeyListener keyTyped, keyPressed, keyReleased
button.addActionListener(this); // ActionListener actionPerformed

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

public static void main(String[] args) {
// TODO Auto-generated method stub
new MainFrame(“SliderTest-5_1_2”);
}

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

@Override
 public void keyTyped(KeyEvent e) { 
 }

 @Override
 public void keyPressed(KeyEvent e) {
 }

 @Override
 public void keyReleased(KeyEvent e) {
     int key = e.getKeyCode();
     if (key == KeyEvent.VK_ENTER) {
      Toolkit.getDefaultToolkit().beep();
      System.out.println(“ENTER pressed”);
      JTextField t = (JTextField) e.getSource();
     int value = Integer.parseUnsignedInt(t.getText());
     slider.setValue(value);
     }
 }

 @Override
 public void actionPerformed(ActionEvent e) {
  JButton b = (JButton)e.getSource();
  if (b == button) {
   this.setVisible(false);
   this.dispose();
  }
 }

}

Event Listener

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 MainFrame 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 MainFrame(String name) {
super(name);

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

// using anonymous class

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) {
// TODO Auto-generated method stub
new MainFrame(“SliderTest_5_1_1”);
}

}


 

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 MainFrame 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 MainFrame(String name) {
super(name);

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

slider.addChangeListener(this); // implements ChangeListener

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

public static void main(String[] args) {
// TODO Auto-generated method stub
new MainFrame(“SliderTest-5_1_2”);
}

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


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 MainFrame 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 MainFrame(String name) {
super(name);

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

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) {
// TODO Auto-generated method stub
new MainFrame(“SliderTest-5_1_3”);
}
}

Arrays stream

Arrays.asList(5,3,4,7,2).stream(); // 스트림 생성
Arrays.asList(5,3,4,7,2).stream().forEach(System.out::println);
Arrays.asList(5,3,4,7,2).stream().map(i-> i*i).forEach(System.out::println);
Arrays.asList(5,3,4,7,2).stream().filter(i-> i>3).forEach(System.out::println);
int sum = Arrays.asList(5,3,4,7,2).stream().reduce((i, j) -> i+j).get(); // ((((5+3)+4)+7)+2)
System.out.println(“sum=” + sum);
List<Integer> intList = Arrays.asList(5,3,4,7,2).stream().filter(i-> i>3).collect(Collectors.toList());
intList.forEach(System.out::println);
int result = Arrays.asList(5,3,4,7,2).stream().filter(i-> i>3).findFirst().orElse(null); // 3보다 큰 첫번째 요소 반환
System.out.println(“result=” + result);
Arrays.asList(5,3,4,7,2).stream().filter(i-> i>3).findAny().ifPresent(System.out::println); // 3보다 큰 요소 반환
boolean result2 = Arrays.asList(5,3,4,7,2).stream().allMatch(i-> i>0); // 모두다 0보다 큰지
System.out.println(“result2=” + result2);

 

Lan4_2 Photo

// Object equals method override – equality (Java7 or higher)
@Override
public boolean equals(Object other)
{
if (other == this) return true;
if (other instanceof Photo) {
Photo that = (Photo)other;
return Objects.equals(this.fullPath, that.fullPath) &&
Objects.equals(this.extension, that.extension) &&
this.width == that.width &&
this.height == that.height;
}
return false;
}

// Object hashCode method override – identity (Java7 or higher)
@Override
public int hashCode()
{
return Objects.hash(this.fullPath, this.extension, this.width, this.height);
}

== vs equal vs hashCode

public static void main(String[] args) {
///////////////////////////////////////////////////////////////////////

Person jason1 = new Person(“Jason”, 10);
System.out.println(“jason1 hashCode=” + jason1.hashCode());
Person jason2 = new Person(“Jason”, 10);
System.out.println(“jason2 hashCode=” + jason2.hashCode());
Person jason3 = jason1;
System.out.println(“jason3 hashCode=” + jason3.hashCode());
Person jason4 = new Person(“Jason”, 20);
System.out.println(“jason4 hashCode=” + jason4.hashCode());

if (jason1 == jason2)
System.out.println(“jason1 == jason2”);
else
System.out.println(“jason1 != jason2”); //동일한 reference를 가리키지 않으므로 jason1 != jason2
if (jason1 == jason3)
System.out.println(“jason1 == jason3”); // 동일한 reference이므로 jason1 == jason3
else
System.out.println(“jason1 != jason3”);
if (jason1.equals(jason4))
System.out.println(“jason1 == jason4”);
else
System.out.println(“jason1 != jason4”); //동일한 reference를 가리키지 않으므로 jason1 != jason4

if (jason1.equals(jason2))
System.out.println(“jason1 equals to jason2”); // 내용이 같으므로 jason1 equals to jason2
else
System.out.println(“jason1 is not equal to jason2”);
if (jason1.equals(jason3))
System.out.println(“jason1 equals to jason3”); // 내용이 같으므로 jason1 equals to jason3
else
System.out.println(“jason1 is not equal to jason3”);
if (jason1.equals(jason4))
System.out.println(“jason1 equals jason4”);
else
System.out.println(“jason1 is not equal to jason4”); // 내용이 다르므로 jason1 is not equal to jason4

///////////////////////////////////////////////////////////////////////

Student john1 = new Student(“John”, 10, 100);
Student john2 = new Student(“John”, 10, 100);
Student john3 = john1;
Student john4 = new Student(“John”, 20, 100);
if (john1.equals(john2))
System.out.println(“john1 equals to john2”); // 내용이 같으므로 john1 equals to john2
else
System.out.println(“john1 is not equal to john2”);

if (john1.equals(john3))
System.out.println(“john1 equals to john3”); // 내용이 같으므로 jogn1 equals to john3
else
System.out.println(“john1 is not equal to john3”);

if (john1.equals(john4))
System.out.println(“john1 equals to john4”);
else
System.out.println(“john1 is not equal to john4”); // 내용이 다르므로 john1 is not equal to john4

System.out.println(“HashSet~~~~~~~~~~~~~~~~~~~~~~”); // hashCode가 일치하면 동일한 것으로 간주
Set sList5 = new HashSet();
sList5.add(john1);
sList5.add(john2);
sList5.add(john3);
sList5.add(john4);
sList5.forEach((s) -> System.out.println(s));
System.out.println(“sList5 contains john1 Student(\”John\”, 10, 100): ” + sList5.contains(john1));
System.out.println(“sList5 contains john2 Student(\”John\”, 10, 100): ” + sList5.contains(john2));
System.out.println(“sList5 contains john3 Student(\”John\”, 10, 100): ” + sList5.contains(john3));
System.out.println(“sList5 contains john4 Student(\”John\”, 20, 100):: ” + sList5.contains(john4));
System.out.println(“sList5 contains Student(\”John\”, 20, 100): ” + sList5.contains(new Student(“John”, 20, 100)));
System.out.println(“sList5 contains Student(\”John\”, 20, 200): ” + sList5.contains(new Student(“John”, 20, 200)));

System.out.println(“HashTable~~~~~~~~~~~~~~~~~~~~~~”); // HashTable의 경우 key 값은 hashCode가 일치하면 동일한 것으로 간주하여 replace함
Hashtable<Student, Integer> sList6 = new Hashtable<Student, Integer>();
sList6.put(john1, 1);
sList6.put(john2, 2);
sList6.put(john3, 3);
sList6.put(john4, 4);
sList6.forEach((s, i) -> System.out.println(s + ” ” + i));
}