import javax.swing.*;
import java.util.*;
public class Main extends JFrame{
class MyThread implements Runnable{
public void run() {
System.out.println("스레드 실행 시작");
for(int i=1;i<=10;i++)
System.out.print(i+" ");
System.out.println();
System.out.println("스레드 종료");
}
}
Main(){
Scanner scanner=new Scanner(System.in);
System.out.println("아무 키나 입력>> ");
scanner.nextLine();
scanner.close();
Thread th=new Thread(new MyThread());
th.start();
}
public static void main(String[] args) {
new Main();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame{
class MyPanel extends JPanel{
public void paintComponent(Graphics g) {
g.setColor(Color.MAGENTA);
int x=(int)(Math.random()*180);
int y=(int)(Math.random()*180);
g.drawOval(x, y, 50, 50);
}
}
class MyThread extends Thread{
MyPanel panel;
public MyThread(MyPanel p) {
panel=p;
}
public void run() {
for(;;) {
try {
sleep(400);
panel.repaint();
}
catch (Exception e) {
// TODO: handle exception
}
}
}
}
Main(){
MyPanel panel=new MyPanel();
MyThread th=new MyThread(panel);
setTitle("1");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setContentPane(panel);
panel.setLayout(null);
panel.setOpaque(true);
panel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
th.start();
}
});
setVisible(true);
setSize(300,300);
}
public static void main(String[] args) {
new Main();
}
}
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame{
Main(){
setTitle("진동하는 프레임 만들기");
setDefaultCloseOperation(EXIT_ON_CLOSE);
MyThread th=new MyThread(this);
th.start();
setSize(500,500);
setVisible(true);
}
class MyThread extends Thread{
JFrame frame;
int x=300;
public MyThread(JFrame jf) {
this.frame=jf;
}
public void run() {
while(true) {
if(x==300) x+=3;
else x-=3;
frame.setLocation(x, 500);
try {
Thread.sleep(100);
} catch (Exception e) {
// TODO: handle exception
return;
}
}
}
}
public static void main(String[] args) {
new Main();
}
}
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame{
Main(){
setTitle("진동하는 프레임 만들기");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c=getContentPane();
c.setLayout(new FlowLayout());
MyThread th=new MyThread();
c.add(th);
setSize(500,500);
setVisible(true);
}
class MyThread extends JLabel implements Runnable{
int x=250;
public MyThread() {
setText("진동레이블");
setLocation(250,200);
setFont(new Font("Arial",Font.PLAIN,30));
Thread th=new Thread(this);
th.start();
}
public void run() {
while(true) {
if(x==250)x+=3;
else x-=3;
this.setLocation(x,200);
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
return;
}
}
}
}
public static void main(String[] args) {
new Main();
}
}
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame{
ImageIcon img=new ImageIcon("왼쪽.png");
Image nImg=img.getImage().getScaledInstance(30,30, Image.SCALE_SMOOTH);
ImageIcon rImg=new ImageIcon(nImg);
MyThread th;
Main(){
setTitle("버블 게임");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c=getContentPane();
c.setLayout(null);
c.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
th=new MyThread(c,e);
th.start();
}
});
setSize(300, 300);
setVisible(true);
}
class MyThread extends Thread{
int x,y;
JLabel label=new JLabel(rImg);
public MyThread(Container c,MouseEvent e) {
// TODO Auto-generated constructor stub
x=e.getX();
y=e.getY();
label.setLocation(x, y);
label.setSize(30,30);
c.add(label);
repaint();
}
public void run() {
while(true) {
try {
y-=5;
label.setLocation(x, y);
if(label.getY()+label.getHeight()<0) {
return;
}
repaint();
Thread.sleep(200);
}
catch (Exception e) {
// TODO: handle exception
return;
}
}
}
}
public static void main(String[] args) {
new Main();
}
}