2007.12.02 19:28

자바 동적 컴포넌트 변경

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

public class CompValidateExample extends JFrame implements ActionListener 
{
	JButton b = null;
	JLabel l = null;
	
	public CompValidateExample() {
		this.setLayout(new GridLayout(1,3));
		
		b = new JButton("Show Label");
		b.addActionListener(this);
		
		this.add(b);
		
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(300, 100);
		this.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent evt) {
		if(evt.getSource().equals(b)) {
			l = new JLabel("It's Label");
			
			this.add(l);
			this.validate();
		}
	}
	
	public static void main(String[] args) {
		new CompValidateExample();
	}
}


28번째 줄의 validate() 메소드를 호출하면 변경사항이 반영된다.
Trackback 0 Comment 0