import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

class MyFrame extends JFrame implements ActionListener {
	⁄**
	 * 
	 *⁄
	private static final long serialVersionUID = -9026464211228409376L;
	
	private JButton setSizeButton = new JButton("setSize");
	private JButton setPreferredSizeButton = new JButton("setPreferredSize");
	private JButton repaintButton = new JButton("Repaint");
	public MyFrame(String title) {
		super(title);
		setSize(400, 200);
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = getSize();
		int x = (int) (screenSize.getWidth() - frameSize.getWidth()) ⁄ 2;
		int y = (int) (screenSize.getHeight() - frameSize.getHeight()) ⁄ 2;
		this.setLocation(x, y);
		
		JPanel panel = new JPanel();
		setSizeButton.addActionListener(this);
		setPreferredSizeButton.addActionListener(this);
		repaintButton.addActionListener(this);
		panel.add(setSizeButton);
		panel.add(setPreferredSizeButton);
		panel.add(repaintButton);
		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				super.componentResized(e);
				System.out.println("componenet Resized(ComponentEvent e)");
			}
		});
		this.add(panel);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);

		System.out.println("paint(Graphics g)");
		System.out.println("Frame getSize() : " + getSize());
		System.out.println("Frame getPreferredSize() : " + getPreferredSize());
		System.out.println();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton actionButton = (JButton)e.getSource();
		Dimension thisSize = getSize();
		thisSize.setSize(thisSize.height, thisSize.width);
		if (actionButton == setSizeButton) {
			this.setSize(thisSize);
		} else if (actionButton == setPreferredSizeButton){
			this.setPreferredSize(thisSize);
		} else if (actionButton == repaintButton) {
			this.repaint();
		}
	}
}

코드를 실행해보면 결과와 같이 setSize 같은경우 해당 Frame을 다시 그리고, setPrefereedSize 같은 경우에는 그리지 않는다.


Writing...

'JAVA Platform > JAVA' 카테고리의 다른 글

[JAVA] - Swing Component Font 설정하기  (0) 2015.04.11
[JAVA] - Swing Look And Feel 적용하기  (0) 2015.04.01
[JAVA] - File 실행하기  (0) 2015.03.22
[JAVA] - JNDI Database Connection  (0) 2015.03.19
[JAVA] - DBMS JDBC Connection  (0) 2015.02.13
블로그 이미지

행복그리고..

,