ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 210823_Swing(버튼)+image+Null
    JAVA 2021. 8. 23. 17:25

    public class SwingNullLay_08 extends JFrame implements ActionListener{

     

    Container cp;

    JButton btn1,btn2,btn3;

     

    //지역변수로 이미지 넣어보기

    static final String ICON1="/Library/sist0730/image/Right.gif";

    static final String ICON2="/Library/sist0730/image/rightDown.gif";

    static final String ICON3="/Library/sist0730/image/img6.gif";

     

     

    public SwingNullLay_08(String title) {

    super(title);

     

    cp=this.getContentPane();

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.setBounds(200,300,400,400);  //정사각형

    cp.setBackground(new Color(155,155,200));

     

    initDesign();

    this.setVisible(true);

    }

     

     

    //디자인

    public void initDesign() {

     

    //레이아웃 없애고 직접배치

    this.setLayout(null);

     

    //버튼1

    btn1=new JButton("Hello"); //버튼생성

    //위치,크기

    btn1.setBounds(10, 20, 80, 40);  //x,y,w,h

    //프레임에추가

    this.add(btn1);

     

    //버튼2

    btn2=new JButton("자바",new ImageIcon(ICON3));  

    btn2.setBounds(10, 70, 100, 50);

    this.add(btn2);

     

    //버튼3

    btn3=new JButton(new ImageIcon(ICON1));

    btn3.setBounds(150, 20, 80, 80);

    btn3.setPressedIcon(new ImageIcon(ICON2));

    this.add(btn3);

     

    //이벤트

    btn1.addActionListener(this);  //this는 이벤트 클래스를 가지고 있는 인스턴스

    btn2.addActionListener(this);

    btn3.addActionListener(this);

    }

     

     

    public static void main(String[] args) {

     

            new SwingNullLay_08("이미지아이콘 연습");

    }

        //이벤트 쇼메세지

    @Override

    public void actionPerformed(ActionEvent e) {

     

    Object ob=e.getSource();  //JButton ob=(JButton)e.getSource(); 버튼으로 형변환해도 됨

     

    if(ob==btn1)

    JOptionPane.showMessageDialog(this, "Hello 클릭");

    else if(ob==btn2)

    JOptionPane.showMessageDialog(this, "자바 클릭");

    else if(ob==btn3)

    JOptionPane.showMessageDialog(this, "오른쪽 클릭");

    }

    }

    'JAVA' 카테고리의 다른 글

    210826_Thread  (0) 2021.08.26
    210823_Swing(버튼)+image+Arrays  (0) 2021.08.23
    210823_Swing(버튼)+image  (0) 2021.08.23
    210823_swing(버튼)  (0) 2021.08.23
    210823_swing+layout(버튼 생성)  (0) 2021.08.23

    댓글

Designed by Tistory.