728x90

다이얼로그 Dialog

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Test1 extends JFrame {
    Test1() {
        Container c = getContentPane();
        setVisible(true);
        c.add(new Dia(), BorderLayout.NORTH);
    }

    class Dia extends Panel {
        JButton j = new JButton("이름 입력");
        JTextField j1 = new JTextField(10);
        JButton j2 = new JButton("확인");
        JButton j3 = new JButton("메세지");

        Dia() {
            setBackground(Color.CYAN);
            add(j);
            add(j1);
            add(j2);
            add(j3);

            j.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String name = JOptionPane.showInputDialog("이름 입력");
                    if (name != null) {
                        j1.setText(name);
                    }
                }
            });

            j2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    int r = JOptionPane.showConfirmDialog(null, "계속?", "확인", JOptionPane.YES_NO_OPTION);
                    if (r == JOptionPane.CLOSED_OPTION) {
                        j1.setText("선택안했다");
                    } else if (r == JOptionPane.YES_OPTION) {
                        j1.setText("네");
                    } else {
                        j1.setText("아니오");
                    }
                }
            });

            j3.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null, "경고", "메세지", JOptionPane.WARNING_MESSAGE);
                }
            });
        }
    }

    public static void main(String[] args) {
        new Test1();

    }
}

 

 

 

객체배열 버튼

import javax.swing.*;
import java.awt.*;

public class Test1{
    public static void main(String[] args) {

        Color c[] = {Color.red, Color.orange, Color.yellow, Color.green, Color.blue};

        JFrame j = new JFrame();
        j.setLayout(new GridLayout(1,5));

        JButton [] buttons = new JButton[5]; //객체 배열
        for(int i=0;i<buttons.length;i++){
            buttons[i]=new JButton(String.valueOf(i));
            buttons[i].setBackground(c[i]);
            j.add(buttons[i]);
        }
        j.setVisible(true);
        
        
    }
}

 

 

 

쓰레드와 AWT 연결

import java.awt.*;
import javax.swing.*;
class Th extends Thread{
    JLabel j;

    Th(JLabel j){
        this.j=j;
    }
    @Override
    public void run() {
        int n=0;
        while(true) {
            j.setText(Integer.toString(n));
            n++;
            try {
                sleep(1000);
            }catch(Exception e) {
                return;
            }
        }
    }
}
public class Test1 extends JFrame{
    Test1(){
        Container c=getContentPane();
        c.setLayout(new FlowLayout());
        JLabel j=new JLabel();
        c.add(j);

        Th t=new Th(j);  //쓰레드 객체 생성
        t.start();  //쓰레드 실행

        setVisible(true);
    }

    public static void main(String[] args) {

        new Test1();

    }
}
728x90

+ Recent posts