import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyModalDialog extends JDialog{
private JTextField tf=new JTextField(10);
private JTextField tf1=new JTextField(10);
private JButton okButton=new JButton("OK");
public MyModalDialog(JFrame frame,String title)
{
super(frame,title,true);
setLayout(new FlowLayout());
add(tf);
add(tf1);
add(okButton);
setSize(500,500);
okButton.addActionListener(new MyActionListener());
}
class MyActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
}
public String getInput()
{
if(tf.getText().length()==0)
{
return null;
}
else {
return Integer.toString(Integer.valueOf(tf.getText())+Integer.valueOf(tf1.getText()));
}
}
}
public class Main extends JFrame{
JLabel l=new JLabel("계산결과를 출력합니다");
private MyModalDialog dialog;
Main()
{
super("dialogEx 예제 프레임");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=getContentPane();
JButton b=new JButton("계산");
c.add(l);
c.add(b);
c.setLayout(null);
b.setSize(80, 80);
b.setLocation(20, 20);
b.setVisible(true);
l.setSize(200, 50);
l.setBackground(Color.green);
l.setLocation(200, 20);
l.setVisible(true);
l.setOpaque(true);
b.setOpaque(true);
dialog=new MyModalDialog(this,"Test Modal Dialog");
b.addActionListener(new MyActionListener());
setSize(500,400);
setVisible(true);
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
dialog.setVisible(true);
String Text=dialog.getInput();
if(Text==null)
{
return;
}
l.setText(Text);
}
}
public static void main(String[] args) {
new Main();
}
}
*/
/*
import java.awt.*;
import javax.swing.*;
class TimerThread extends Thread
{
private JLabel timerLabel;
public TimerThread(JLabel timerLabel)
{
this.timerLabel =timerLabel;
}
public void run()
{
int n=0;
int m=0;
int h=0;
while(true){
if(n==60)
{
n=0;
m++;
}
if(m==60)
{
m=0;
h++;
}
timerLabel.setText(h+":"+m+":"+Integer.toString(n));
m++;
try {
Thread.sleep(1000);
}
catch(InterruptedException e)
{
return;
}
}
}
}
public class Main extends JFrame{
Main()
{
setTitle("Thread를 상속받은 타이머 스레드 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=getContentPane();
c.setLayout(new FlowLayout());
JLabel timerLabel=new JLabel();
timerLabel.setFont(new Font("Gothic",Font.ITALIC,80));
c.add(timerLabel);
TimerThread th=new TimerThread(timerLabel);
setSize(300,170);
setVisible(true);
th.start();
}
public static void main(String[] args) {
new Main();
}
}
*/
/*
import java.awt.*;
import javax.swing.*;
class TimerRunnable implements Runnable{
private JLabel timerLabel;
public TimerRunnable(JLabel timerLabel) {
this.timerLabel=timerLabel;
}
public void run()
{
int n=0;
int m=0;
int h=0;
while(true){
if(n==60)
{
n=0;
m++;
}
if(m==60)
{
m=0;
h++;
}
timerLabel.setText(h+":"+m+":"+Integer.toString(n));
n++;
try {
Thread.sleep(1000);
}
catch(InterruptedException e)
{
return;
}
}
}
}
public class Main extends JFrame{
Main()
{
setTitle("Runnable을 구현한 타이머 스레드 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=getContentPane();
c.setLayout(new FlowLayout());
JLabel timerLabel=new JLabel();
timerLabel.setFont(new Font("Gothic",Font.ITALIC,80));
c.add(timerLabel);
TimerRunnable runnable=new TimerRunnable(timerLabel);
Thread th=new Thread(runnable);
setSize(450,275);
setVisible(true);
th.start();
System.out.println("이름"+th.getName());
System.out.println("ID"+th.getId());
System.out.println("State"+th.getState());
}
public static void main(String[] args) {
new Main();
}
}
*/
/*
import java.awt.*;
import javax.swing.*;
class FlickeringLabel extends JLabel implements Runnable{
private long delay;
public FlickeringLabel(String text,long delay)
{
super(text);
this.delay=delay;
setOpaque(true);
Thread th=new Thread(this);
th.start();
}
public void run()
{
int n=0;
while(true)
{
if(n==0)
{
setBackground(Color.yellow);
}
else
{
setBackground(Color.green);
}
if(n==0)
{
n=1;
}
else
{
n=0;
}
try {
Thread.sleep(delay);
}
catch(InterruptedException e){
return;
}
}
}
}
public class Main extends JFrame{
public Main()
{
setTitle("FlickeringLabelEx 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=getContentPane();
c.setLayout(new FlowLayout());
FlickeringLabel fLabel=new FlickeringLabel("깜빡",500);
JLabel label =new JLabel("안깜빡");
FlickeringLabel fLabel1=new FlickeringLabel("여기도 깜빡",300);
System.out.println("이름"+fLabel.getName());
System.out.println("이름"+fLabe);
c.add(fLabel);
c.add(label);
c.add(fLabel1);
setSize(300,150);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}*/
/*
public class Main{
public static void main(String[] args) {
long id=Thread.currentThread().getId();
String name=Thread.currentThread().getName();
int priorty =Thread.currentThread().getPriority();
Thread.State s=Thread.currentThread().getState();
System.out.println("현재 스레드 이름 = "+ name);
System.out.println("현재 스레드 ID = "+id);
System.out.println("현재 스레드 우선순위 값 = "+ priorty);
System.out.println("현재 스레드 상태 = "+s);
}
}
*/
/*
import java.awt.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TimerRunnable implements Runnable{
private JLabel timerLabel;
public TimerRunnable(JLabel timerLabel) {
this.timerLabel=timerLabel;
}
public void run()
{
int n=0;
int m=0;
int h=0;
while(true){
if(n==60)
{
n=0;
m++;
}
if(m==60)
{
m=0;
h++;
}
timerLabel.setText(h+":"+m+":"+Integer.toString(n));
n++;
try {
Thread.sleep(1000);
}
catch(InterruptedException e)
{
return;
}
}
}
}
public class Main extends JFrame{
private Thread th;
Main()
{
setTitle("ThreadInterttuptEx 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=getContentPane();
c.setLayout(new FlowLayout());
JLabel timerLabel=new JLabel();
timerLabel.setFont(new Font("Gothic",Font.ITALIC,80));
TimerRunnable runnable=new TimerRunnable(timerLabel);
Thread th=new Thread(runnable);
c.add(timerLabel);
JButton btn=new JButton("Kill Timer");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
th.interrupt();
JButton btn=(JButton)e.getSource();
btn.setEnabled(false);
}
});
c.add(btn);
setSize(450,275);
setVisible(true);
th.start();
}
public static void main(String[] args) {
new Main();
}
}
*/
/*
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class RandomThread extends Thread
{
private Container contentPane;
private boolean flag=false;
public RandomThread(Container contentPane)
{
this.contentPane=contentPane;
}
public void finish()
{
flag=true;
}
public void run()
{
while(true)
{
int x=((int)(Math.random()*contentPane.getWidth()));
int y=((int)(Math.random()*contentPane.getHeight()));
JLabel label=new JLabel("java");
label.setSize(250,100);
label.setFont(new Font("Gothic",Font.BOLD,80));
label.setLocation(x, y);
contentPane.add(label);
contentPane.repaint();
try {
Thread.sleep(0);
if(flag==true)
{
contentPane.removeAll();
label=new JLabel("finish");
label.setSize(80,30);
label.setLocation(100, 100);
label.setForeground(Color.red);
contentPane.add(label);
contentPane.repaint();
return;
}
}
catch(InterruptedException e)
{
return;
}
}
}
}
public class Main extends JFrame{
private RandomThread th;
Main()
{
setTitle("ThreadFinishFlagEx 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=getContentPane();
c.setLayout(null);
c.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
th.finish();
}
});
setSize(300,200);
setVisible(true);
th=new RandomThread(c);
th.start();
}
public static void main(String[] args) {
new Main();
}
}
*/
/*
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Calendar;
class CalendarThread extends Thread {
JLabel l = new JLabel();
private Container contentPane;
public CalendarThread(Container contentPane) {
this.contentPane = contentPane;
}
public void run() {
l.setSize(200, 50);
l.setFont(new Font("Gothic", Font.ITALIC, 50));
l.setLocation(50, 60);
l.setOpaque(true);
l.setVisible(true);
contentPane.add(l);
while (true) {
Calendar c=Calendar.getInstance();
int 시=c.get(Calendar.HOUR_OF_DAY);
int 분=c.get(Calendar.MINUTE);
int 초=c.get(Calendar.SECOND);
l.setText(Integer.toString(시) + ":" + Integer.toString(분) + ":" + Integer.toString(초));
}
}
}
public class Main extends JFrame {
private CalendarThread th;
Main() {
setTitle("ThreadFinishFlagEx 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
setSize(300, 200);
setVisible(true);
th = new CalendarThread(c);
th.start();
}
public static void main(String[] args) {
new Main();
}
}
pan
*/
/*
public class Main{
public static void main(String[] args) {
SharedBoard board=new SharedBoard();
Thread th1=new StudentThread("Kitea",board);
Thread th2=new StudentThread("htosoo",board);
th1.start();
th2.start();
}
}
class SharedBoard{
private int sum=0;
public void add()
{
int n=sum;
Thread.yield();
n+=10;
sum=n;
System.out.println(Thread.currentThread().getName()+" : "+sum);
}
public int getSum()
{
return sum;
}
}
class StudentThread extends Thread{
private SharedBoard board;
public StudentThread(String name,SharedBoard board)
{
super(name);
this.board=board;
}
public void run()
{
for( int i=0;i<10;i++)
{
board.add();
}
}
}
*/