반응형

문제

컨텐트팬의 배경색은 초록색으로 하고 마우스를 드래깅하는 동안만 노란색으로 유지하는 스윙 응용프로그램을 작성하라.

 

소스 코드

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

public class Main2 extends JFrame{
	public Main2() {
		setTitle("드래깅동안 YELLOW...");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container c = getContentPane();
		Panel p = new Panel();
		p.setBackground(Color.green);
		
		p.addMouseMotionListener(new MouseAdapter() {
			public void mouseDragged(MouseEvent e) {
				p.setBackground(Color.yellow);
			}
		});
		p.addMouseListener(new MouseAdapter() {
			public void mouseReleased(MouseEvent e) {
				p.setBackground(Color.green);
			}
		});
		c.add(p);
		
		setSize(300,200);
		setVisible(true);
	}
	public static void main(String[] args) {
		new Main2();
		
	}

}

 

반응형
반응형

문제

JLabel 컴포넌트는 Mouse 이벤트를 받을 수 있다. JLabel 컴포넌트에 마우스를 올리면 "LOVE JAVA"가, 내리면 "사랑해"가 출력되도록 스윙 응용프로그램을 작성하라.

 

소스 코드

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

public class Main1 extends JFrame{
	Main1(){
		setTitle("마우스 올리기 내리기");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container c = getContentPane();
		
		c.setLayout(new FlowLayout());
		
		JLabel label = new JLabel("사랑해");
		
		label.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				label.setText("Love Java");
			}
			public void mouseExited(MouseEvent e) {
				label.setText("사랑해");
			}
		});
		
		c.add(label);
				
		setSize(300,200);
		setVisible(true);
	}
	public static void main(String[] args) {
		new Main1();
	}

}

기본 화면 입니다.
마우스 올린 상태에서 윈도우 캡처한 화면입니다.

 

반응형
반응형

문제

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

 

소스코드

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

public class Main8 extends JFrame{
	public Main8() {
		setTitle("여러 개의 패널을 가진 프레임");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container c = getContentPane();
		
		c.setLayout(new BorderLayout());
		
		c.add(new NorthPanel(),BorderLayout.NORTH);
		c.add(new CenterPanel(),BorderLayout.CENTER);
		c.add(new SouthPanel(),BorderLayout.SOUTH);
		
		setSize(300,300);
		setVisible(true);
	}
	class NorthPanel extends Panel {
		public NorthPanel(){
		setBackground(Color.gray);
		JButton btn[] = new JButton[3];
		btn[0] = new JButton("열기");
		btn[1] = new JButton("닫기");
		btn[2] = new JButton("나가기");
		for(int i =0;i<3;i++)
			add(btn[i]);		
		}
	}
	class CenterPanel extends Panel {
		public CenterPanel() {
			setBackground(Color.white);
			setLayout(null);
			JLabel[] label = new JLabel[10];
			for(int i = 0;i<10;i++) {
				label[i] = new JLabel("*");
				label[i].setForeground(Color.red);
				label[i].setBackground(Color.white);
				int x = (int)(Math.random()*200)+10;
				int y = (int)(Math.random()*200)+10;
				label[i].setLocation(x, y);
				label[i].setSize(10,10);
				label[i].setOpaque(true);
				add(label[i]);
			}
		}
	}
	class SouthPanel extends Panel {
		public SouthPanel() {
			setBackground(Color.yellow);
			JButton btn = new JButton("Word Input");
			TextField tf = new TextField(20);
			add(btn);
			add(tf);
		}
	}
	public static void main(String[] args) {
		new Main8();
	}

}

반응형
반응형

문제

20개의 10 x 10 크기의 JLabel 컴포넌트가 프레임 내에 (50, 50) 위치에서 (250, 250) 영역에서 랜덤한 위치에 출력되도록 스윙 프로그램을 작성하라. 프레임의 크기를 300 X 300으로 하고, JLabel의 배경색은 모두 파란색으로 하라.

 

소스 코드

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

public class Main6 extends JFrame{
	public Main6() {
		setTitle("Random Labels");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container c = getContentPane();
		
		c.setLayout(null);
		JLabel [] label = new JLabel[20];
		
		for(int i = 0;i<20;i++) {
			label[i] = new JLabel();
			label[i].setBackground(Color.BLUE);
			int x = (int)(Math.random()*200)+50;
			int y = (int)(Math.random()*200)+50;
			label[i].setLocation(x, y);
			label[i].setSize(10,10);
			label[i].setOpaque(true);
			c.add(label[i]);
		}
		setSize(300,300);
		setVisible(true);
	}
	
	public static void main(String[] args)
	{
		new Main6();
	}
}

반응형
반응형

문제

문제 3을 수정하여 다음 결과와 같이 각 버튼의 배경색을 서로 다르게 설정하라.

 

소스 코드

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

public class Main4 extends JFrame{
	Color [] color = { Color.RED, Color.ORANGE, Color.YELLOW, 
			Color.GREEN, Color.CYAN, Color.BLUE, Color.MAGENTA, Color.GRAY, 
			Color.PINK, Color.LIGHT_GRAY };
	public Main4(){
		setTitle("Ten Color buttons Frame");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container c = getContentPane();
		
		c.setLayout(new GridLayout(1,10));
		
		JButton btn[] = new JButton[10];
		
		for(int i = 0;i<10;i++)
		{
			btn[i] = new JButton(Integer.toString(i));
			btn[i].setBackground(color[i]);
			c.add(btn[i]);
		}
		
		setSize(500,200);
		setVisible(true);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Main4();
	}

}

반응형
반응형

문제

GridLayout을 사용하여 다음 그림과 같이 한 줄에 10개의 버튼을 동일한 크기로 배치하는 스윙 프로그램을 작성하라.

 

소스 코드

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

public class Main3 extends JFrame{
	public Main3() {
		setTitle("Ten Color button Frame");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container c = getContentPane();
		c.setLayout(new GridLayout(1,10));
		JButton btn[] = new JButton[10];
		for(int i = 0;i<10;i++)
		{
			btn[i] = new JButton(Integer.toString(i));
			c.add(btn[i]);
		}
		
		setSize(500,200);
		setVisible(true);
	}
	public static void main(String[] args) {
		new Main3();
	}

}

 

반응형
반응형

문제

BorderLayout을 사용하여 컴포넌트 사이의 수평 수직 간격이 각가 5픽셀, 7픽셀이 되도록 스윙 응용프로그램을 작성하라.

 

소스 코드

import java.awt.*;
import javax.swing.*;
public class Main2 extends JFrame{
	public Main2(){
		setTitle("BorderLayout");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container c = getContentPane();
		
		c.setLayout(new BorderLayout(5,7));
		c.add(new JButton("North"),BorderLayout.NORTH);
		c.add(new JButton("West"),BorderLayout.WEST);
		c.add(new JButton("Center"),BorderLayout.CENTER);
		c.add(new JButton("East"),BorderLayout.EAST);
		c.add(new JButton("South"),BorderLayout.SOUTH);
		setSize(300,200);
		setVisible(true);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Main2();
	}

}

 

반응형

+ Recent posts