Component | Jbutton
ㄴ Component는 Container에 담을 수 있다
- Jbutton을 담을 수 있는 Jpanel을 준비
- Jpanel에 ActionListener 인터페이스 상속
- Jbutton 생성 → 설정
ㄴ setBounds(x,y,w,h)
ㄴ addActionListener(this) - Jpanel에 Jbutton 추가
ㄴ setLayout(null) → default 해제 (FlowLayout)
ㄴ add(Jbutton b)

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 |
댓글