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

    2020. 12. 7.

    by. Hyeon-Uk

    반응형

    [1번]

    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    
    class MyFrame extends JFrame{
    	public MyFrame() {
    		setTitle("CheckBox Practice Frame");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container c=getContentPane();
    		c.setLayout(new FlowLayout());
    		
    		JCheckBox b1,b2;
    		b1=new JCheckBox("버튼 비활성화");
    		b2=new JCheckBox("버튼 감추기");
    		JButton btn1=new JButton("test button");
    		b1.addItemListener(new ItemListener() {
    			public void itemStateChanged(ItemEvent e) {
    				// TODO Auto-generated method stub
    				if(e.getStateChange()==ItemEvent.SELECTED) {
    					btn1.setEnabled(false);
    				}
    				else {
    					btn1.setEnabled(true);
    				}
    			}
    		});
    		b2.addItemListener(new ItemListener() {
    			public void itemStateChanged(ItemEvent e) {
    				// TODO Auto-generated method stub
    				if(e.getStateChange()==ItemEvent.SELECTED) {
    					btn1.setVisible(false);
    				}
    				else {
    					btn1.setVisible(true);
    				}
    			}
    		});
    		
    		
    		c.add(b1);
    		c.add(b2);
    		c.add(btn1);
    		
    		setSize(300,300);
    		setVisible(true);
    	}
    }
    
    public class p1101 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame();
    	}
    }
    
    

    [2번]

    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Vector;
    
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    
    class MyFrame extends JFrame{
    	JTextField jt=new JTextField(10);
    	JComboBox jc=new JComboBox();
    	public MyFrame() {
    		setTitle("JTextField and JComboBox");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container c=getContentPane();
    		c.setLayout(new FlowLayout());
    		
    		jt.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				JTextField j=(JTextField)e.getSource();
    				String temp=j.getText();
    				j.setText("");
    				jc.addItem(temp);
    			}
    		});
    		
    		c.add(jt);
    		c.add(jc);
    		
    		setSize(300,300);
    		setVisible(true);
    	}
    }
    
    public class p1102 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame();
    	}
    }
    
    

    [3번]

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    class MyFrame extends JFrame{
    	JButton cal;
    	JTextField input;
    	JLabel money[]=new JLabel[8];
    	String moneytag[]= {"오만원","만원","천원","500원","100원","50원","10원","1원"};
    	int moneys[]= {50000,10000,1000,500,100,50,10,1};
    	JTextField result[]=new JTextField[8];
    	public MyFrame() {
    		setTitle("Money Changer");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container c=getContentPane();
    		c.setLayout(new BorderLayout());
    		c.setBackground(Color.pink);
    		
    		JPanel North=new JPanel();
    		
    		North.setLayout(new FlowLayout());
    		North.add(new JLabel("금액"));
    		input=new JTextField(10);
    		cal=new JButton("계산");
    		
    	    cal.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				int total=Integer.parseInt(input.getText());
    				for(int i=0;i<8;i++) {
    					result[i].setText(Integer.toString(total/moneys[i]));
    					total%=moneys[i];
    				}
    			}
    		});
    		North.add(input);
    		North.add(cal);
    		North.setBackground(Color.pink);
    		c.add(North,BorderLayout.NORTH);
    		
    		
    		JPanel center=new JPanel();
    		center.setLayout(new GridLayout(8,2));
    		center.setBackground(Color.pink);
    		for(int i=0;i<8;i++) {
    			money[i]=new JLabel(moneytag[i]);
    			result[i]=new JTextField();
    			center.add(money[i]);
    			center.add(result[i]);
    		}
    		c.add(center,BorderLayout.CENTER);
    		
    		
    		JPanel west=new JPanel();
    		west.setBackground(Color.pink);
    		c.add(west,BorderLayout.WEST);
    		
    		JPanel east=new JPanel();
    		east.setBackground(Color.pink);
    		c.add(west,BorderLayout.EAST);
    		
    		JPanel south=new JPanel();
    		south.setBackground(Color.pink);
    		c.add(west,BorderLayout.SOUTH);
    		
    		setSize(300,300);
    		setVisible(true);
    	}
    }
    
    public class p1103 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame();
    	}
    }
    
    

    [4번]

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    class MyFrame extends JFrame{
    	JButton cal;
    	JTextField input;
    	JLabel money[]=new JLabel[8];
    	String moneytag[]= {"오만원","만원","천원","500원","100원","50원","10원","1원"};
    	int moneys[]= {50000,10000,1000,500,100,50,10,1};
    	JTextField result[]=new JTextField[8];
    	JCheckBox check[]=new JCheckBox[7];
    	public MyFrame() {
    		setTitle("Money Changer with CheckBox");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container c=getContentPane();
    		c.setLayout(new BorderLayout());
    		c.setBackground(Color.pink);
    		
    		JPanel North=new JPanel();
    		
    		North.setLayout(new FlowLayout());
    		North.add(new JLabel("금액"));
    		input=new JTextField(10);
    		cal=new JButton("계산");
    		
    	    cal.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				int total=Integer.parseInt(input.getText());
    				for(int i=0;i<7;i++) {
    					if(check[i].isSelected()) {
    						result[i].setText(Integer.toString(total/moneys[i]));
    						total%=moneys[i];
    					}
    					else {
    						result[i].setText("0");
    					}
    				}
    				result[7].setText(Integer.toString(total));
    			}
    		});
    		North.add(input);
    		North.add(cal);
    		North.setBackground(Color.pink);
    		c.add(North,BorderLayout.NORTH);
    		
    		
    		JPanel center=new JPanel();
    		center.setLayout(new GridLayout(8,3));
    		center.setBackground(Color.pink);
    		for(int i=0;i<8;i++) {
    			money[i]=new JLabel(moneytag[i]);
    			result[i]=new JTextField();
    			center.add(money[i]);
    			center.add(result[i]);
    			if(i<7) {
    				check[i]=new JCheckBox();
    				check[i].setBackground(Color.pink);
    				center.add(check[i]);
    			}
    		}
    		c.add(center,BorderLayout.CENTER);
    		
    		
    		JPanel west=new JPanel();
    		west.setBackground(Color.pink);
    		c.add(west,BorderLayout.WEST);
    		
    		JPanel east=new JPanel();
    		east.setBackground(Color.pink);
    		c.add(west,BorderLayout.EAST);
    		
    		JPanel south=new JPanel();
    		south.setBackground(Color.pink);
    		c.add(west,BorderLayout.SOUTH);
    		
    		setSize(300,300);
    		setVisible(true);
    	}
    }
    
    public class p1104 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame();
    	}
    }
    
    

    [5번]

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JSlider;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    class MyFrame extends JFrame{
    	
    	public MyFrame() {
    		setTitle("JSlider Practice Frame");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container c=getContentPane();
    		c.setLayout(new FlowLayout());
    		
    		JSlider js=new JSlider(100, 200, 120);
    		JLabel jl=new JLabel("120");
    		js.setMajorTickSpacing(20);
    		js.setPaintTicks(true);
    		js.setPaintLabels(true);
    		js.addChangeListener(new ChangeListener() {
    			public void stateChanged(ChangeEvent e) {
    				JSlider temp=(JSlider)e.getSource();
    				jl.setText(Integer.toString(temp.getValue()));
    			}
    		});
    		c.add(js);
    		
    		jl.setBackground(Color.green);
    		jl.setOpaque(true);
    		c.add(jl);
    		
    		setSize(300,300);
    		setVisible(true);
    	}
    }
    
    public class p1105 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame();
    	}
    }
    
    

    반응형

    [6번]

    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JSlider;
    import javax.swing.JTextArea;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    class MyFrame extends JFrame{
    	JSlider js=new JSlider(0,100,0);
    	JTextArea jta=new JTextArea();
    	public MyFrame() {
    		setTitle("TextArea Practice Frame");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container c=getContentPane();
    		c.setLayout(new GridLayout(3,1));
    		
    		
    		js.setMajorTickSpacing(20);
    		js.setMinorTickSpacing(5);
    		js.setPaintLabels(true);
    		js.setPaintTicks(true);
    		
    		js.addChangeListener(new ChangeListener() {
    			public void stateChanged(ChangeEvent e) {
    				JSlider temp=(JSlider)e.getSource();
    				String text=jta.getText();
    				if(text.length()<=js.getValue()) {
    					js.setValue(text.length());
    				}
    				else {
    					jta.setText(text.substring(0,temp.getValue()));
    				}
    			}
    		});
    		
    		
    		jta.addKeyListener(new KeyAdapter() {
    			public void keyTyped(KeyEvent e) {
    				// TODO Auto-generated method stub
    				JTextArea t=(JTextArea)e.getSource();
    				String text=t.getText();
    				if(text.length()<=100) {
    					js.setValue(text.length());
    				}
    				else {
    					text=text.substring(0,99);
    					jta.setText(text);
    				}
    			}
    		});
    		c.add(jta);
    		c.add(js);
    		setSize(300,300);
    		setVisible(true);
    	}
    }
    
    public class p1106 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame();
    	}
    }
    
    

    [7번]

    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Font;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JSlider;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    class MyFrame extends JFrame{
    	JSlider js=new JSlider(1,100,56);
    	JLabel jl=new JLabel("I Love Java");
    	public MyFrame() {
    		setTitle("JSlider Practice Frame");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container c=getContentPane();
    		c.setLayout(new BorderLayout());
    		
    		js.setMajorTickSpacing(20);
    		js.setMinorTickSpacing(5);
    		js.setPaintLabels(true);
    		js.setPaintTicks(true);
    		js.setPaintTrack(true);
    		
    		js.addChangeListener(new ChangeListener() {
    			public void stateChanged(ChangeEvent e) {
    				JSlider temp=(JSlider)e.getSource();
    				jl.setFont(new Font("Arial",Font.PLAIN,temp.getValue()));
    			}
    		});
    		c.add(js,BorderLayout.NORTH);
    		
    		jl.setFont(new Font("Arial",Font.PLAIN,56));
    		c.add(jl,BorderLayout.CENTER);
    		
    		setSize(300,300);
    		setVisible(true);
    	}
    }
    
    public class p1107 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame();
    	}
    }
    
    

    [8번]

    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.ButtonGroup;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    
    class MyFrame extends JFrame{
    	JRadioButton jb[]=new JRadioButton[2];
    	//이미지 파일의 위치와 이름에 따라 달라짐
    	ImageIcon image[]= {new ImageIcon("images/red.png"),new ImageIcon("images/blue.png"),new ImageIcon("images/green.png")};
    	int index=0;
    	JLabel jl=new JLabel(image[0]);
    	public MyFrame() {
    		setTitle("Image Gallery Practice Frame");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container c=getContentPane();
    		c.setLayout(new BorderLayout());
    		
    		JPanel north=new JPanel();
    		north.setLayout(new FlowLayout());
    		jb[0]=new JRadioButton("left");
    		jb[0].setSelected(true);
    		jb[1]=new JRadioButton("right");
    		north.add(jb[0]);
    		north.add(jb[1]);
    		
    		ButtonGroup g=new ButtonGroup();
    		g.add(jb[0]);
    		g.add(jb[1]);
    		
    		c.add(north,BorderLayout.NORTH);
    		
    		
    		c.add(jl,BorderLayout.CENTER);
    		jl.addMouseListener(new MouseAdapter() {
    			public void mouseClicked(MouseEvent e) {
    				if(jb[0].isSelected()) {
    					index=(index+1)%image.length;
    				}
    				else {
    					index--;
    					if(index<0) {
    						index=image.length-1;
    					}
    				}
    				jl.setIcon(image[index]);
    			}
    		});
    		
    		setSize(300,300);
    		setVisible(true);
    	}
    }
    
    public class p1108 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame();
    	}
    }
    
    

    [9번]

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class p1109 extends JFrame{	
    	
    	JButton btn[]=new JButton[3];
    	ImageIcon img[]= {new ImageIcon("image/sissor.jpeg"),new ImageIcon("image/rock.jpeg"),new ImageIcon("image/paper.png")};
    	int mySelect,comSelect;
    	JLabel me=new JLabel("me");
    	JLabel com=new JLabel("com");
    	JLabel result=new JLabel();
    	public p1109() {
    		setTitle("가위 바위 보 게임");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container c=getContentPane();
    		c.setLayout(new BorderLayout());
    		
    		JPanel top=new JPanel();
    		top.setLayout(new FlowLayout());
    		for(int i=0;i<3;i++) {
    			btn[i]=new JButton(img[i]);
    			btn[i].addActionListener(new action(i));
    			top.add(btn[i]);
    		}
    		top.setBackground(Color.DARK_GRAY);
    		c.add(top,BorderLayout.NORTH);
    		
    		JPanel center=new JPanel();
    		center.setLayout(new FlowLayout());
    		center.add(me);
    		center.add(com);
    		center.add(result);
    		center.setBackground(Color.YELLOW);
    		c.add(center);
    		
    		setSize(1000,700);
    		setVisible(true);
    	}
    	
    	class action implements ActionListener{
    		int index;
    		action(int i){
    			index=i;
    		}
    		public void actionPerformed(ActionEvent e) {
    			mySelect=index;
    			comSelect=(int)(Math.random()*3);
    			me.setIcon(img[mySelect]);
    			com.setIcon(img[comSelect]);
    			if(mySelect-comSelect==0) {
    				result.setText("Same !!!");
    			}
    			else if(mySelect-comSelect==-2||mySelect-comSelect==1) {
    				result.setText("Me !!!");
    			}
    			else {
    				result.setText("Com !!!");
    			}
    		}
    	}
    	
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new p1109();
    	}
    
    }
    

    [10번]

    import java.awt.Container;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    class MyFrame extends JFrame{
    	JLabel num[]=new JLabel[10];
    	int nowindex;
    	public MyFrame() {
    		setTitle("Ten 레이블 클릭");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container c=getContentPane();
    		c.setLayout(null);
    		
    		for(int i=0;i<10;i++) {
    			num[i]=new JLabel(Integer.toString(i));
    			num[i].setSize(10,10);
    			num[i].addMouseListener(new MouseAdapter() {
    				public void mouseClicked(MouseEvent e) {
    					JLabel temp=(JLabel)e.getSource();
    					int num=Integer.parseInt(temp.getText());
    					if(num==nowindex) {
    						temp.setVisible(false);
    						nowindex++;
    					}
    					if(nowindex==10) {
    						setting();
    					}
    				}
    			});
    			c.add(num[i]);
    		}
    		setting();
    		
    		setSize(300,300);
    		setVisible(true);
    	}
    	
    	public void setting() {
    		nowindex=0;
    		for(int i=0;i<10;i++) {
    			int x=(int)(Math.random()*300);
    			int y=(int)(Math.random()*300);
    			
    			num[i].setLocation(x,y);
    			num[i].setVisible(true);
    		}
    	}
    }
    
    public class p1110 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame();
    	}
    }
    
    
    반응형

    댓글