ffgg
10
2021-11-28 21:38:54
1
118

JPanel 질문입니다


사각형 버튼을 한번 누를때마다 사각형의 한변을 그려서 4번누르면 사각형이 완성이되고 버튼은 비활성화 되는 프로그램을 만들고싶은데, 한번 누르면 버튼이 사라져 진행이되지 않습니다. 

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

public class Report7 extends JFrame {
	int count = 0;
	public Report7() {
		MyPanel1 panel1 = new MyPanel1();
		MyPanel2 panel2 = new MyPanel2();
		MyPanel3 panel3 = new MyPanel3();
		MyPanel4 panel4 = new MyPanel4();
		
		setTitle("사각형 만들기");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		
		JButton b1 = new JButton("사각형");
		
		b1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JButton b = (JButton)e.getSource();
				Report7 frame = (Report7)b.getTopLevelAncestor();
				count++;
				if(count == 1) {
					setContentPane(panel1);
					setVisible(true);
				}
				else if(count == 2) {
					setContentPane(panel2);
					setVisible(true);
					b1.setEnabled(true);
				}
				else if(count == 3) {
					setContentPane(panel3);
					setVisible(true);
					b1.setEnabled(true);
				}
				else if(count == 4) {
					setContentPane(panel4);
					setVisible(true);
					b1.setEnabled(false);
				}
				
			}
			
		});
		
		c.add(b1);
		
		setSize(400,400);
		setVisible(true);
	}
	
	class MyPanel1 extends JPanel{
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(Color.RED);
			g.drawLine(100,100,100,300);
			repaint();
		}
	}
	
	class MyPanel2 extends JPanel{
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(Color.RED);
			g.drawLine(100,300,300,300);
			repaint();
		}
	}
	
	class MyPanel3 extends JPanel{
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(Color.RED);
			g.drawLine(300,300,300,100);
			repaint();
		}
	}
	
	class MyPanel4 extends JPanel{
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(Color.RED);
			g.drawLine(300,100,100,100);
			repaint();
		}
	}
	
	
	public static void main(String[] args) {
		new Report7();
	}

}


0
  • 답변 1

  • 종로3가
    550
    2021-11-30 07:28:47 작성 2021-11-30 07:30:36 수정됨

    아래 소스를 카피해서 여기에서 테스트해 보기 바랍니다. 

    https://codehs.com/sandbox/id/java-swing-tAhwpq


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MyProgram extends JFrame {
    	int count = 0;
    	public MyProgram() {
    		MyPanel1 panel1 = new MyPanel1();
    		MyPanel2 panel2 = new MyPanel2();
    		MyPanel3 panel3 = new MyPanel3();
    		MyPanel4 panel4 = new MyPanel4();
    		
    		setTitle("Square Building");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container c = getContentPane();
    		c.setLayout(new FlowLayout());
    		
    		JButton b1 = new JButton("Square");
    		
    		b1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				JButton b = (JButton)e.getSource();
    				MyProgram frame = (MyProgram)b.getTopLevelAncestor();
    				count++;
    				if(count == 1) {
    					setContentPane(panel1);
    					setVisible(true);					
    		            panel1.add(b1);
    					b1.setEnabled(true);
    				}
    				else if(count == 2) {
    					setContentPane(panel1);
    					setContentPane(panel2);
    					setVisible(true);				
    		            panel2.add(b1);
    					b1.setEnabled(true);
    				}
    				else if(count == 3) {
    					setContentPane(panel1);
    					setContentPane(panel2);
    					setContentPane(panel3);
    					setVisible(true);
    					panel3.add(b1);
    					b1.setEnabled(true);
    				}
    				else if(count == 4) {
    					setContentPane(panel1);
    					setContentPane(panel2);
    					setContentPane(panel3);
    					setContentPane(panel4);
    					setVisible(true);
    					panel4.add(b1);
    					b1.setEnabled(false);
    				}
    				
    			}
    			
    		});
    		
    		c.add(b1);
    		
    		setSize(400,400);
    		setVisible(true);
    	}
    	
    	class MyPanel1 extends JPanel{
    		public void paintComponent(Graphics g) {
    			super.paintComponent(g);
    			g.setColor(Color.RED);
    			g.drawLine(100,100,100,300);
    			repaint();
    		}
    	}
    	
    	class MyPanel2 extends JPanel{
    		public void paintComponent(Graphics g) {
    			super.paintComponent(g);
    			g.setColor(Color.RED);
    			g.drawLine(100,100,100,300);
    			g.drawLine(100,300,300,300);
    			repaint();
    		}
    	}
    	
    	class MyPanel3 extends JPanel{
    		public void paintComponent(Graphics g) {
    			super.paintComponent(g);
    			g.setColor(Color.RED);
    			g.drawLine(100,100,100,300);
    			g.drawLine(100,300,300,300);
    			g.drawLine(300,300,300,100);
    			repaint();
    		}
    	}
    	
    	class MyPanel4 extends JPanel{
    		public void paintComponent(Graphics g) {
    			super.paintComponent(g);
    			g.setColor(Color.RED);
    			g.drawLine(100,100,100,300);
    			g.drawLine(100,300,300,300);
    			g.drawLine(300,300,300,100);
    			g.drawLine(300,100,100,100);
    			repaint();
    		}
    	}
    	
    	
    	public static void main(String[] args) {
    		new MyProgram();
    	}
    
    }
  • 로그인을 하시면 답변을 등록할 수 있습니다.