Daily coding
Java Basic : day 14 - AWT & SWING 7 : Window Change 본문
Window Change
화면 전환 방법
1. dispose()
- 현재 화면이 사라지는 메소드. 코드 구현 후 다음 화면을 생성해준다.
< 예제 >
package day15.Lecture01_AWT.test10_windowChanger1;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class WindowOne extends Frame implements WindowListener {
public WindowOne() {
// TODO Auto-generated constructor stub
setLayout(null);
Button btn = new Button("move win");
btn.setBounds(100, 100, 100, 30);
add(btn);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// System.exit(0)
// 위의 코드는 프로세스도 꺼지므로 사용하면 안된다.
// dispose 이용
dispose();
// 그다음 윈도우를 켜준다.
new windowTwo();
}
});
setBackground(Color.red);
setSize(640,480);
setLocation(0, 0);
addWindowListener(this);
setVisible(true);
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}
2. 싱글톤을 이용해서 setVisible 값을 전환해주기
1) 싱글톤 클래스를 만들고 생성자에 전환할 화면을 구성하고 있는 클래스를 생성해준다.
화면 전환할 객체들은 설계시 모두 setVisible을 false로 바꾼다.
package day15.Lecture01_AWT.test11_windowChanger2;
public class Singleton {
private static Singleton singleton =null;
public WindowOne win1;
public windowTwo win2;
private Singleton() {
win1 = new WindowOne();
win2 = new windowTwo();
}
public static Singleton getInstane() {
if(singleton == null) singleton = new Singleton();
return singleton;
}
}
2) 화면 1의 화면 전환 버튼을 만들어 주고,
버튼을 누르면 싱글톤 객체를 불러온 뒤 setVisible 값을 바꾸어 준다.
package day15.Lecture01_AWT.test11_windowChanger2;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
// Window changew
public class WindowOne extends Frame implements WindowListener {
public WindowOne() {
// TODO Auto-generated constructor stub
setLayout(null);
Button btn = new Button("move win");
btn.setBounds(100, 100, 100, 30);
add(btn);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Singleton.getInstane().win1.setVisible(false);
Singleton.getInstane().win2.setVisible(true);
}
});
setBackground(Color.red);
setSize(640,480);
setLocation(0, 0);
addWindowListener(this);
setVisible(false); // singleton이 visible제어함
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}
3) 마찬가지로 전환할 화면의 클래스도 설계해준다.
setVisible은 싱글톤이 제어하도록 꺼준다.
package day15.Lecture01_AWT.test11_windowChanger2;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class windowTwo extends Frame implements WindowListener {
public windowTwo() {
setBackground(Color.BLUE);
setSize(800, 600);
setLocation(0, 0);
addWindowListener(this);
setVisible(false); // singleton이 visible제어함
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}
3. 하나의 클래스에 패널로 화면 바꿔주기
- remove() 메소드를 이용해서 패널을 바꾸어 준다.
package day15.Lecture01_AWT.test12_windowChanger3;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JOptionPane;
// Window changew
public class WindowOne extends Frame implements WindowListener {
public WindowOne() {
// TODO Auto-generated constructor stub
setLayout(new FlowLayout());
Panel p1 = new Panel();
Panel p2 = new Panel();
p1.setBackground(Color.black);
p1.setLayout(null);
p1.setBounds(0, 0, 640, 480);
add(p1);
p2.setBackground(Color.blue);
p2.setBounds(0, 0, 640, 480);
Button btn = new Button("move win");
btn.setBounds(100, 100, 100, 30);
p1.add(btn);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
remove(p1);
add(p2);
}
});
Button btn2 = new Button("move win2");
btn2.setBounds(100, 100, 100, 30);
p2.add(btn2);
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
remove(p2);
add(p1);
}
});
setBackground(Color.red);
setSize(640,480);
setLocation(0, 0);
addWindowListener(this);
setVisible(true); // singleton이 visible제어함
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}
'Language > Java_basic' 카테고리의 다른 글
Java Basic : day 15 - AWT & SWING 9 : Table과 ScrollPane (0) | 2019.12.08 |
---|---|
Java Basic : day 14 - AWT & SWING 8 : 마우스리스너 (0) | 2019.12.08 |
Java Basic : day 14 - AWT & SWING 6 : TextField & TextArea (0) | 2019.12.08 |
Java Basic : day 14 - AWT & SWING 5 : 버튼 Button (0) | 2019.12.08 |
Java Basic : day 14 - AWT & SWING 4 : 패널 Panel (0) | 2019.12.08 |