ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 210823_swing(버튼)
    JAVA 2021. 8. 23. 17:19

    배경색 변경

    (예제1)

    public class SwingBtnEvent_05 extends JFrame{

     

    Container cp;

    JButton btn1,btn2;

     

    public SwingBtnEvent_05(String title) {  

    super(title);

     

    cp=this.getContentPane();

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.setBounds(200,300,400,500);

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

     

    setDesign();

    this.setVisible(true);  

    }

     

    //디자인과 액션이벤트

    public void setDesign() {

     

    this.setLayout(new FlowLayout());

     

    //버튼생성하고 바로 이벤트(익명내부클래스 이용)

    btn1=new JButton("배경색변경_핫핑크");

    //this에 올리기 위해 프레임에 추가

    this.add(btn1);

    //이벤트

    btn1.addActionListener(new ActionListener() {

     

    @Override

    public void actionPerformed(ActionEvent e) {

    //배경색 변경

    cp.setBackground(Color.MAGENTA);

    }

    });

     

    btn2=new JButton("배경색변경_블루");

    this.add(btn2);

    btn2.addActionListener(new ActionListener() {

     

    @Override

    public void actionPerformed(ActionEvent e) {

    cp.setBackground(Color.BLUE);

    }

    }); 

    }

     

    public static void main(String[] args) {

     

    new SwingBtnEvent_05("버튼이벤트를 익명내부클래스로");

    }

    }

     

     

     

    ~~~~~~~~~~~~~~~~~~~~

     

     

    버튼 누르면 팝업창

     

    public class SwingBtnEvent_06 extends JFrame implements ActionListener {

     

    Container cp;

    JButton btn1,btn2;

     

    public SwingBtnEvent_06(String title) {  

    super(title);

     

    cp=this.getContentPane();

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.setBounds(200,300,400,500);

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

     

    initDesign();

    this.setVisible(true);

    }

     

     

     public void initDesign() {

     

    this.setLayout(new FlowLayout());

    btn1=new JButton("<<");

    btn2=new JButton(">>");

     

    this.add(btn1);

    this.add(btn2);

     

    btn1.addActionListener(this);

    btn2.addActionListener(this);

     }

     

     

    public static void main(String[] args) {

     

    new SwingBtnEvent_06("이벤트버튼 #06");

    }

     

    @Override

    public void actionPerformed(ActionEvent e) {

     

    Object ob=e.getSource();

     

    if(ob==btn1)

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

    else if(ob==btn2)

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

    }

    }

     

     

    'JAVA' 카테고리의 다른 글

    210823_Swing(버튼)+image+Null  (0) 2021.08.23
    210823_Swing(버튼)+image  (0) 2021.08.23
    210823_swing+layout(버튼 생성)  (0) 2021.08.23
    210823_Swing  (0) 2021.08.23
    210823_Array+List  (0) 2021.08.23

    댓글

Designed by Tistory.