국비 14일차_1

강지수·2024년 1월 3일
0

국비교육

목록 보기
26/97

지난 시간 복습

지금 기능적으로 보여주는 method들이 있는데, 이 method 외에 다른 부분, 다른 기능에서도 유사한 방향으로 진행이 되며, 유사한 형태로 사용이 된다.
모든 method를 외울 수는 없다. 하지만 현재 보여주는 method들이 어떤 형태로, 어떤 flow 로 사용되는지 눈에 익히고, 감각을 익히는 것이 중요하다.

Thread.sleep(ms);

sleep() 안에는 millisecond 가 들어간다. (1s = 1000ms)


package com.tech.gt003;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class FileChooserEx extends JFrame implements ActionListener{
	private JButton openButton,saveButton;
	private JFileChooser fc;
	
	public FileChooserEx() {
		setTitle("파일추서");
		setSize(300,200);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		fc=new JFileChooser();
		
		JLabel label=new JLabel("파일선택기 컴포넌트 활용하기 샘플");
		openButton=new JButton("파일오픈");
		saveButton=new JButton("파일저장");
		openButton.addActionListener(this);
		saveButton.addActionListener(this);
		
		JPanel panel=new JPanel();
		panel.add(label);
		panel.add(openButton);
		panel.add(saveButton);
		
		add(panel);
		
		setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println("수신");
		if(e.getSource()==openButton) {
			int returnVal=fc.showOpenDialog(this);
//			System.out.println(returnVal);
			if(returnVal==JFileChooser.APPROVE_OPTION) {
				File file=fc.getSelectedFile();
				System.out.println("파일이름"+file.getName());
			}
		}else if(e.getSource()==saveButton) {
			int returnVal=fc.showSaveDialog(this);
		}
	}
	public static void main(String[] args) {
		new FileChooserEx();
	}

}

어제 하다 만 파일추서,

fileOpen 과 마찬가지로 fileSave 기능도 가능하다.

아직 file 이 실제로 열리는 것 까지는 구현하지 못했으나, open/save 를 할 때, 지정한 fileName 까지 getName으로 받아오는 것까지는 가능


calculator 에 기능 넣기

getActionCommand() -> 누른 button의 값을 불러줌
charAt() -> 배열의 ()번째 char 를 가져와서 str 로 불러줌


package com.tech.gt001;

import java.awt.Color;
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.JPanel;
import javax.swing.JTextField;

public class CalculatorEx3 extends JFrame implements ActionListener{
//	배열과 반복문 사용
	private JPanel panel;
	private JTextField tField;
	private JButton[] buttons;
	private String[] labels= {
			"Backspace", "", "", "CE", "C",
			"7", "8", "9", "/", "sqrt",
			"4", "5", "6", "x", "%",
			"1", "2", "3", "-","1/x",
			"0", "+/-", ".", "+", "="
	};
	
	public CalculatorEx3() {
		setTitle("계산기");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(600, 250);
		
		tField=new JTextField(35);
		tField.setEnabled(false);;
		
		panel=new JPanel();
		tField.setText("0.");
		panel.setLayout(new GridLayout(0,5,3,3));
		buttons=new JButton[25];
		int index=0;
		for (int row = 0; row < 5; row++) {
			for (int col = 0; col < 5; col++) {
				buttons[index]=new JButton(labels[index]);
				if(col>=3) {
					buttons[index].setForeground(Color.red);
				}else {
					buttons[index].setForeground(Color.blue);
				}
				buttons[index].setBackground(Color.cyan);
				buttons[index].addActionListener(this);
				panel.add(buttons[index]);
				index++;
			}
		}
//		판넬을 프레임에 부착
		add(tField,"North");
		
		add(panel,"Center");
		
		setVisible(true);
	}

	public static void main(String[] args) {
		new CalculatorEx3();
	}
	private double result=0;
	private String operator="=";
	private boolean startNumber=true; // 새로 시작인지 아닌지 판단할 때 사용

	@Override
	public void actionPerformed(ActionEvent e) {
//		System.out.println("신호");
//		System.out.println(e.getActionCommand());
//		String str="hello";
//		System.out.println("charAt:"+str.charAt(0));
		String command=e.getActionCommand();
		
		if(command.charAt(0)=='C') {
			startNumber=true;
			result=0;
			operator="=";
			tField.setText("0.0");			
		}else if(command.charAt(0)>='0' && command.charAt(0)<='9' || command.equals(".")) { // 숫자의 개념을 가지고 판별
//			시작인지 아닌지 판별
			if(startNumber==true) {
				tField.setText(command);
			}else {
				tField.setText(tField.getText()+command);
			}
			startNumber=false;
		}else {
			if(startNumber) {
				if(command.equals("-")) { // 음수
					tField.setText(command);
					startNumber=false;
				}else {
					operator=command;
				}
			}else {
				double x=Double.parseDouble(tField.getText());
				calculator(x);
				operator=command;
				startNumber=true;
			}
		}
	}

	private void calculator(double n) {
		if(operator.equals("+")) {
			result+=n;
		}else if(operator.equals("-")) {
			result-=n;
		}else if(operator.equals("x")) {
			result*=n;
		}else if(operator.equals("/")) {
			result/=n;
		}else if(operator.equals("=")) {
			result=n;
		}
		tField.setText(result+"");
	}
}

사칙연산까지 구현


카카오 오븐 - semi project 디자인
-> 키오스크 화면을 디자인하면 됨.
주제는 꼭 그 내용이 아니어도 됨.
분량은 3일 내에 작업할 수 있을 정도의 분량. (주말 껴서)


피자 주문 창 만들기

package com.tech.gt001;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

class MyFrame2 extends JFrame implements ActionListener{
	
	private JButton order_button, cancel_button;
	private JPanel down_panel;
	private JTextField text;
	
	WelcomePanel welcomePanel=new WelcomePanel();
	TypePanel typePanel=new TypePanel();
	ToppingPanel toppingPanel=new ToppingPanel();
	SizePanel sizePanel=new SizePanel();
	
	public MyFrame2() {
		setSize(500,200);
		setTitle("피자주문");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		add(welcomePanel,"North");
		JPanel centerPanel=new JPanel(new GridLayout(0,3));
		centerPanel.add(typePanel);
		centerPanel.add(toppingPanel);
		centerPanel.add(sizePanel);
		
		order_button=new JButton("주문");
		cancel_button=new JButton("취소");
		order_button.addActionListener(this);
		cancel_button.addActionListener(this);
		
		text=new JTextField();
		text.setEditable(false);
		text.setColumns(25);
		
		down_panel=new JPanel();
		down_panel.add(order_button);
		down_panel.add(cancel_button);
		down_panel.add(text);

		add(centerPanel,"Center");
		add(down_panel,"South");
		setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println("버튼");
		int kindPrice=0,topPrice=0;
		
		if(e.getSource()==order_button) {
			if(typePanel.typeStr=="combo") {
				switch (sizePanel.sizeStr) {
				case "small":
					kindPrice = 10000;
					break;
				case "medium":
					kindPrice = 15000;
					break;
				case "large":
					kindPrice = 20000;
					break;
				default:
					break;
				}
			}else if(typePanel.typeStr=="potato"){
				switch (sizePanel.sizeStr) {
				case "small":
					kindPrice = 10000;
					break;
				case "medium":
					kindPrice = 15000;
					break;
				case "large":
					kindPrice = 20000;
					break;
				default:
					break;
				}
			}else if(typePanel.typeStr=="bulgogi"){
				switch (sizePanel.sizeStr) {
				case "small":
					kindPrice = 10000;
					break;
				case "medium":
					kindPrice = 15000;
					break;
				case "large":
					kindPrice = 20000;
					break;
				default:
					break;
				}
			}
			switch (toppingPanel.topStr) {
			case "pepper":
				topPrice=1000;
				break;
			case "cheese":
				topPrice=2000;
				break;
			case "peperoni":
				topPrice=3000;
				break;
			case "bacon":
				topPrice=4000;
				break;
			default:
				break;
			}
			text.setText("kindPrice:"+kindPrice+", topPrice:"+topPrice+", Tot:"+(kindPrice+topPrice));
		}else if(e.getSource()==cancel_button) {
			kindPrice=0;
			topPrice=0;
			typePanel.typeStr="";
			toppingPanel.topStr="";
			sizePanel.sizeStr="";
			typePanel.bg.clearSelection();
			toppingPanel.bg.clearSelection();
			sizePanel.bg.clearSelection();
			text.setText("");
		}
		
	}
	class WelcomePanel extends JPanel{
		private JLabel message;
		public WelcomePanel() {
			message=new JLabel("피자샵에 오신걸 환영합니다.");
			this.add(message);
		}
	}
	class TypePanel extends JPanel implements ActionListener{
		protected JRadioButton combo,potato,bulgogi;
		protected ButtonGroup bg;
		protected String typeStr="";
		
		public TypePanel() {
			setLayout(new GridLayout(3,1));
			combo=new JRadioButton("콤보");
			potato=new JRadioButton("포테이토");
			bulgogi=new JRadioButton("불고기");
//			수신기부착
			combo.addActionListener(this);
			potato.addActionListener(this);
			bulgogi.addActionListener(this);
			
			bg=new ButtonGroup();
			bg.add(combo);
			bg.add(potato);
			bg.add(bulgogi);
			
			this.setBorder(BorderFactory.createTitledBorder("Type"));
			
			add(combo);
			add(potato);
			add(bulgogi);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==combo) {
				typeStr="combo";
			}else if(e.getSource()==potato) {
				typeStr="potato";
			}else if (e.getSource()==bulgogi) {
				typeStr="bulgogi";
			}
			System.out.println("type : "+typeStr);
		}
	}
	class ToppingPanel extends JPanel implements ActionListener{
		protected JCheckBox pepper,cheese,peperoni,bacon;
		protected ButtonGroup bg;
		protected String topStr="";
		
		public ToppingPanel() {
//			토핑 판넬 구성
			setLayout(new GridLayout(4,1));
			pepper=new JCheckBox("피망");
			cheese=new JCheckBox("치즈");
			peperoni=new JCheckBox("페페로니");
			bacon=new JCheckBox("베이컨");
			
			pepper.addActionListener(this);
			cheese.addActionListener(this);
			peperoni.addActionListener(this);
			bacon.addActionListener(this);
			
			bg=new ButtonGroup();
			bg.add(pepper);
			bg.add(cheese);
			bg.add(peperoni);
			bg.add(bacon);
			
			this.setBorder(BorderFactory.createTitledBorder("Topping"));
			
			add(pepper);
			add(cheese);
			add(peperoni);
			add(bacon);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==pepper) {
				topStr="pepper";
			}else if(e.getSource()==cheese) {
				topStr="cheese";
			}else if (e.getSource()==peperoni) {
				topStr="peperoni";
			}else if (e.getSource()==bacon) {
				topStr="bacon";
			}
			System.out.println("topping : "+topStr);
		}
	}
	class SizePanel extends JPanel implements ActionListener{
		protected JRadioButton small,medium,large;
		protected ButtonGroup bg;
		protected String sizeStr="";
		
		public SizePanel() {
			setLayout(new GridLayout(3,1));
			small=new JRadioButton("small");
			medium=new JRadioButton("medium");
			large=new JRadioButton("large");
	//			수신기부착
			small.addActionListener(this);
			medium.addActionListener(this);
			large.addActionListener(this);
			
			bg=new ButtonGroup();
			bg.add(small);
			bg.add(medium);
			bg.add(large);
			
			this.setBorder(BorderFactory.createTitledBorder("Size"));
			
			add(small);
			add(medium);
			add(large);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==small) {
				sizeStr="small";
			}else if(e.getSource()==medium) {
				sizeStr="medium";
			}else if (e.getSource()==large) {
				sizeStr="large";
			}
			System.out.println("size : "+sizeStr);
		}
	}
}
public class PizzaOrderEx2{
	public static void main(String[] args) {
		new MyFrame2();
	}
}

결과


profile
개발자 준비의 준비준비중..

0개의 댓글