본문 바로가기
GUI

[4] Component | Jbutton

by TR. 2021. 1. 12.

Component  |  Jbutton

ㄴ Component는 Container에 담을 수 있다

 

  1. Jbutton을 담을 수 있는 Jpanel을 준비
  2. Jpanel에 ActionListener 인터페이스 상속
  3. Jbutton 생성 → 설정
    setBounds(x,y,w,h)
    addActionListener(this)
  4. Jpanel에 Jbutton 추가
    setLayout(null) → default 해제 (FlowLayout)
    add(Jbutton b)

* LayoutManager

package GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

class Panel extends JPanel implements ActionListener{
	
	JButton bt;
	
	public Panel() {
		setLayout(null);
		
		setBounds(0,0,800,600);
		setVisible(true);
		
		bt = new JButton();
		bt.setText("click");
//		bt.setLocation(350,250);
//		bt.setSize(100, 100);
		bt.setBounds(350,250,100,100);
		bt.addActionListener(this);
		add(bt);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == bt) {
			bt.setText("return");
		}
		
	}
	
}

public class TestGUI extends JFrame{
	
	Panel panel;
	
	public TestGUI(String title) {
		super(title);
		setBounds(100,100,800,600);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setVisible(true);
		revalidate();
		repaint();
		
		panel = new Panel();
		setContentPane(panel);
	}
	
	
	public static void main(String[] args) {
		
		TestGUI frame = new TestGUI("JButton");
		
	}

}

 

'GUI' 카테고리의 다른 글

[7] 네모 그리기  (0) 2021.01.14
[6] Listener | Mouse, Motion, Action, Key 등  (0) 2021.01.14
[3] Container와 Component  (0) 2021.01.12
[2] JPanel | Container  (0) 2021.01.07
[1] JFrame | Top-level Container  (0) 2021.01.07

댓글