• 명품 자바 프로그래밍(개정4판) 제 9장 실습문제

    2020. 12. 7.

    by. Hyeon-Uk

    반응형

    [1번]

    import javax.swing.JFrame;
    
    class MyFrame extends JFrame{
    	public MyFrame() {
    		setTitle("Let's study Java");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		
    		setSize(400,200);
    		setVisible(true);
    	}
    }
    
    public class p0901 {
    	
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame();
    	}
    
    }
    

    [2번]

    import java.awt.BorderLayout;
    import java.awt.Container;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    class MyFrame1 extends JFrame{
    	public MyFrame1() {
    		setTitle("BorderLayout Practice");
    		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 class p0902 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame1();
    	}
    
    }
    

    [3번]

    import java.awt.Container;
    import java.awt.GridLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    class MyFrame2 extends JFrame{
    	public MyFrame2() {
    		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));
    			c.add(btn[i]);
    		}
    		
    		setSize(500,200);
    		setVisible(true);
    	}
    }
    
    
    public class p0903 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame2();
    	}
    
    }
    

    [4번]

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.GridLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    class MyFrame3 extends JFrame{
    	public MyFrame3() {
    		Color color[]= {Color.RED,Color.ORANGE,Color.YELLOW,Color.GREEN,Color.CYAN,Color.BLUE,Color.magenta,Color.GRAY,Color.PINK,Color.LIGHT_GRAY};
    		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 class p0904 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame3();
    	}
    
    }
    

    [5번]

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.GridLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    class MyFrame4 extends JFrame{
    	public MyFrame4() {
    		Color color[]= {Color.RED,Color.ORANGE,Color.YELLOW,Color.GREEN,Color.CYAN,Color.BLUE,Color.magenta,Color.DARK_GRAY,Color.PINK,Color.LIGHT_GRAY,Color.WHITE,Color.BLACK,Color.BLACK,Color.ORANGE,Color.BLUE,Color.MAGENTA};
    		System.out.println(color.length);
    		setTitle("4x4 Color Frame");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Container c=getContentPane();
    		c.setLayout(new GridLayout(4,4));
    		
    		for(int i=0;i<16;i++) {
    			JLabel l=new JLabel(Integer.toString(i));
    			l.setBackground(color[i]);
    			l.setOpaque(true);
    			c.add(l);
    		}
    		
    		setSize(400,200);
    		setVisible(true);
    	}
    }
    
    public class p0905 {
    	
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame4();
    	}
    
    }
    

    반응형

    [6번]

    import java.awt.Color;
    import java.awt.Container;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    class MyFrame5 extends JFrame{
    	public MyFrame5() {
    		setTitle("Random Labels");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Container c=getContentPane();
    		
    		c.setLayout(null);
    
    		for(int i=0;i<20;i++) {
    			int x=(int )(Math.random()*200)+50;
    			int y=(int )(Math.random()*200)+50;
    			JLabel l=new JLabel(" ");
    			l.setBounds(x,y,10,10);
    			l.setOpaque(true);
    			l.setBackground(Color.blue);
    			c.add(l);
    		}
    		
    		setSize(300,300);
    		setVisible(true);
    	}
    }
    
    public class p0906 {
    	
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame5();
    	}
    
    }
    

    [7번]

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    class MyFrame6 extends JFrame{
    	public MyFrame6() {
    		setTitle("계산기 프레임");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Container c=getContentPane();
    		c.setLayout(new BorderLayout());
    		
    		JPanel top=new JPanel();
    		top.setLayout(new FlowLayout());
    		top.setBackground(Color.LIGHT_GRAY);
    		top.add(new JLabel("수식입력"));
    		top.add(new JTextField(" ",20));
    		
    		c.add(top,BorderLayout.NORTH);
    		
    		JPanel center=new JPanel();
    		center.setLayout(new GridLayout(4,4,5,5));
    		JButton btn[]=new JButton[16];
    		String name[]= {"0","1","2","3","4","5","6","7","8","9","CE","계산","+","-","x","/"};
    		for(int i=0;i<16;i++) {
    			btn[i]=new JButton(name[i]);
    			if(i>=12) {
    				btn[i].setBackground(Color.green);
    			}
    			center.add(btn[i]);
    		}
    		c.add(center,BorderLayout.CENTER);
    		
    		JPanel bottom=new JPanel();
    		bottom.setLayout(new FlowLayout());
    		bottom.setBackground(Color.yellow);
    		bottom.add(new JLabel("계산 결과"));
    		bottom.add(new JTextField(" ",20));
    		c.add(bottom,BorderLayout.SOUTH);
    		
    		setSize(400,200);
    		setVisible(true);
    	}
    }
    
    public class p0907 {
    	
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame6();
    	}
    
    }
    

    [8번]

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    class MyFrame7 extends JFrame{
    	public MyFrame7() {
    		setTitle("여러개의 패널을 가진 프레임");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Container c=getContentPane();
    		c.setLayout(new BorderLayout());
    		
    		JPanel top=new JPanel();
    		top.setLayout(new FlowLayout());
    		top.setBackground(Color.LIGHT_GRAY);
    		top.add(new JButton("열기"));
    		top.add(new JButton("닫기"));
    		top.add(new JButton("나가기"));
    		
    		c.add(top,BorderLayout.NORTH);
    		
    		JPanel center=new JPanel();
    		center.setLayout(null);
    
    		for(int i=0;i<10;i++) {
    			int x=(int )(Math.random()*200)+50;
    			int y=(int )(Math.random()*200)+50;
    			JLabel l=new JLabel("*");
    			l.setBounds(x,y,10,10);
    			l.setOpaque(true);
    			l.setBackground(Color.red);
    			center.add(l);
    		}
    		
    		c.add(center,BorderLayout.CENTER);
    		
    		JPanel bottom=new JPanel();
    		bottom.setLayout(new FlowLayout());
    		bottom.setBackground(Color.yellow);
    		bottom.add(new JButton("Word Input"));
    		bottom.add(new JTextField(" ",20));
    		c.add(bottom,BorderLayout.SOUTH);
    		
    		setSize(600,400);
    		setVisible(true);
    	}
    }
    
    public class p0908 {
    	
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame7();
    	}
    
    }
    
    반응형

    댓글