Language/Java

[Java] 실습 (frame)

arajo 2022. 8. 16. 21:42
728x90
package swing.frame;

import java.awt.*;

import javax.swing.*;

//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
public class ContentPaneExam extends JFrame {
	
	//--------------------------------------------------------------------------
	ContentPaneExam() {
		setTitle("ContentPean과 JFrame 공부하기");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 우측상단의 x버튼을 활성화 시킨다.
		
		Container contentPane = getContentPane(); // 컨텐트팬의 정보를 알아내기
		contentPane.setBackground(Color.ORANGE); // 오렌지색으로 배경을 설정한다.
		contentPane.setLayout(new FlowLayout()); // 컨텐트팬에 배치관리자를 설정한다.
		
		contentPane.add(new JButton("OK"));
		contentPane.add(new JButton("Cancel"));
		contentPane.add(new Button("Ignore")); // J로 구분  
		
		setSize(500, 400); // (width, height)
		setVisible(true);
	}
	
	//--------------------------------------------------------------------------
	//--------------------------------------------------------------------------
	public static void main(String[] args) {
		new ContentPaneExam();

	}

}

실행 결과

package swing.frame;

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

public class FlowLayoutExam extends JFrame {
	
	FlowLayoutExam() {
		setTitle("FlowLayout 연습");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		Container contentPane = getContentPane();
		
		contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 40)); // hGap, vGap
		contentPane.add(new JButton("plus"));
		contentPane.add(new JButton("minus"));
		contentPane.add(new JButton("multiply"));
		contentPane.add(new JButton("divide"));
		contentPane.add(new JButton("Calculate"));
		
		setSize(400, 300);
		setVisible(true);
		
	}

	public static void main(String[] args) {
		new FlowLayoutExam();

	}

}

실행 결과

package swing.frame;

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

public class BorderLayoutExam extends JFrame {
	
	BorderLayoutExam() {
		setTitle("BorderLayout 연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container contentPane = getContentPane();
		
		contentPane.setLayout(new BorderLayout(30, 20));
		contentPane.add(new JButton("Calculate"), BorderLayout.CENTER);
		contentPane.add(new JButton("add"), BorderLayout.NORTH);
		contentPane.add(new JButton("Calculate"), BorderLayout.SOUTH);
		contentPane.add(new JButton("mut"), BorderLayout.EAST);
		contentPane.add(new JButton("div"), BorderLayout.WEST);
		
		setSize(400, 300);
		setVisible(true);
	}

	public static void main(String[] args) {
		new BorderLayoutExam();

	}

}

실행 결과

package swing.frame;

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

public class GridLayoutExam extends JFrame{
	public GridLayoutExam() {
		super("GridLayout 연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container contentPane = getContentPane();
		
		// 1x10의 GridLayout 배치관리자를 설정한다.
		contentPane.setLayout(new GridLayout(1, 10)); // (1행, 10열)
		
		for(int i = 0; i < 10; i++) { // 10개의 버튼을 부착시킨다.
			String text = Integer.toString(i + 1);
			JButton button = new JButton(text);
			contentPane.add(button);
		}
		
		setSize(500, 200);
		setVisible(true);
	}
	
	
	public static void main(String[] args) {
		new GridLayoutExam();

	}

}

실행 결과

package swing.frame;

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

public class NoneLayoutExam extends JFrame{
	NoneLayoutExam() {
		super("배치관리자 없이 절대 위치에 버튼을 배치하기");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container contentPane = getContentPane();
		contentPane.setLayout(null); // 컨텐트팬의 배치관리자를 제거한다.
		
		JLabel la = new JLabel("How are you?");
		la.setLocation(260, 80);
		la.setSize(200, 30);
		contentPane.add(la);
		
		// 버튼을 9개 생성하고 사선방향으로 살짝 겹치게 나열해보자.
		for(int i = 1; i <= 9; i++) {
			JButton b = new JButton(Integer.toString(i));
			b.setLocation(i*15, i*15);
			b.setSize(50,20);
			contentPane.add(b);
		}
		
		
		setSize(400, 300);
		setVisible(true);
	}
	
	
	public static void main(String[] args) {
		new NoneLayoutExam();

	}

}

 

실행 결과

package swing.frame;

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

public class JComponentExam extends JFrame{
	JComponentExam() {
		super("JComponent의 공통 메서드 연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container c = getContentPane();
		
		c.setLayout(new FlowLayout());
		
		JButton b1 = new JButton("Magenta/Yellow Button");
		JButton b2 = new JButton(" Disabled Button");
		JButton b3 = new JButton("getX(), getY()");
		
		b1.setBackground(Color.YELLOW);
		b2.setForeground(Color.MAGENTA);
		b1.setFont(new Font("Arial", Font.ITALIC, 20));
		
		b2.setEnabled(false);
		
		b3.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JButton b = (JButton)e.getSource();
				setTitle(b.getX() + ", " + b.getY());
				
			}
			
		});
		
		c.add(b1); c.add(b2); c.add(b3);
		setSize(260, 200);
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new JComponentExam();

	}

}

실행 결과
getX(), getY() 클릭 후
창 늘린 후 getX(), getY() 클릭 좌표

package swing.frame;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class ButtonImageExam extends JFrame {
	ButtonImageExam() {
		setTitle("이미지 버튼 연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		URL normalUrl = getClass().getResource("images/normalIcon.gif");
		URL rolloverUrl = getClass().getResource("images/rolloverIcon.gif");
		URL pressedUrl = getClass().getResource("images/pressedIcon.gif");
		
		ImageIcon normalIcon = new ImageIcon(normalUrl);
		ImageIcon rolloverIcon = new ImageIcon(rolloverUrl);
		ImageIcon pressedIcon = new ImageIcon(pressedUrl);
		
		JButton btn = new JButton("Call", normalIcon);
		btn.setPressedIcon(pressedIcon);
		btn.setRolloverIcon(rolloverIcon);
		
		btn.setPreferredSize(new Dimension(200, 100));
		c.add(btn);
		setSize(300, 300);
		setVisible(true);
	}

	public static void main(String[] args) {
		new ButtonImageExam();

	}

}

실행 결과

아래처럼 URL을 쓰지 않고 이미지풀경로를 14~16번줄에 입력해줘도 된다.

package swing.frame;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class ButtonImageExam extends JFrame {
	ButtonImageExam() {
		setTitle("이미지 버튼 연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		
		ImageIcon normalIcon = new ImageIcon(이미지풀경로);
		ImageIcon rolloverIcon = new ImageIcon(이미지풀경로);
		ImageIcon pressedIcon = new ImageIcon(이미지풀경로);
		
		JButton btn = new JButton("Call", normalIcon);
		btn.setPressedIcon(pressedIcon);
		btn.setRolloverIcon(rolloverIcon);
		
		btn.setPreferredSize(new Dimension(200, 100));
		c.add(btn);
		setSize(300, 300);
		setVisible(true);
	}

	public static void main(String[] args) {
		new ButtonImageExam();

	}

}