본문 바로가기

JAVA/공부

9. 자바 GUI 기초, AWT와 스윙(Swing)

7. 다음과 같은 GUI 모양을 가진 스윙 프레임을 작성하라.

 

import javax.swing.*;
import java.awt.*;
@SuppressWarnings("serial")
class cal extends JFrame{
	public cal(String name) {
		String []str= {"0","1","2","3","4","5","6","7","8","9","CE","계산","+","-","x","/"};
		JButton []jb=new JButton[str.length];
		setTitle(name);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel jp1=new JPanel();
		JPanel jp2=new JPanel();
		JPanel jp3=new JPanel();
		
		Container c=getContentPane();
		c.setLayout(new BorderLayout());
		c.add(jp1,BorderLayout.NORTH);
		c.add(jp2,BorderLayout.CENTER);
		c.add(jp3,BorderLayout.SOUTH);
		
		jp1.setBackground(Color.lightGray);
		jp1.add(new JLabel("수식입력"));
		jp1.add(new JTextField(20));
		
		jp2.setBackground(Color.white);
		jp2.setLayout(new GridLayout(4,4,2,2));
		for(int i=0;i<str.length;i++){
			jb[i]=new JButton(str[i]);
			if(i>11)
				jb[i].setBackground(Color.cyan);
			jp2.add(jb[i]);}		
		
		jp3.setBackground(Color.yellow);
		jp3.add(new JLabel("계산 결과"));
		jp3.add(new JTextField(20));
		
		setSize(400,400);
		setVisible(true);
		
		}
}
public class seven {
	public static void main(String[]args) {
		new cal("계산기 프레임");
	}
}

 

 

 

8. 다음과 같은 GUI 모양을 가진 스윙 프레임을 작성하라. 10개의 '*' 문자는 10개의 JLabel을 이용하여 랜덤한 위치에 출력하라.

 

import javax.swing.*;
import java.awt.*;
@SuppressWarnings("serial")
public class eight extends JFrame{
	public eight(String name) {
		setTitle(name);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel jp1=new JPanel();
		JPanel jp2=new JPanel();
		JPanel jp3=new JPanel();
		
		jp1.setBackground(Color.lightGray);
		jp1.add(new JButton("열기"));
		jp1.add(new JButton("닫기"));
		jp1.add(new JButton("나가기"));
		
		jp2.setBackground(Color.white);
		jp2.setLayout(null);
		for(int i=0;i<10;i++) {
			JLabel j=new JLabel("*");
			j.setForeground(Color.red);	
			int x=(int)(Math.random()*390);
			int y=(int)(Math.random()*280);
			j.setLocation(x,y);	
			j.setSize(10,10);
			jp2.add(j);
		}
		
		jp3.add(new JButton("Word Input"));
		jp3.add(new JTextField(20));
		
		Container c=getContentPane();
		c.add(jp1,BorderLayout.NORTH);
		c.add(jp2,BorderLayout.CENTER);
		c.add(jp3,BorderLayout.SOUTH);
		
		setSize(400,400);
		setVisible(true);
	}
	public static void main(String[]args) {
		new eight("여러 개의 패널을 가진 프레임");
	}
}