Java

자바 질문합니다 !

1
자바 완전 초보예요 .. 과제라 간단한 프로그램 만들기인데 저는 자바 스윙으로 간단한 식당 주문 프로그램을 만들고 있어요 ㅠㅠ 사실 기초가 탄탄하지 않아서 여러가지 소스를 짬뽕하긴 했는데요 ! MainView에 결제 버튼을 누르면 Cu..

자바 완전 초보예요 .. 과제라 간단한 프로그램 만들기인데 저는 자바 스윙으로 간단한 식당 주문 프로그램을 만들고 있어요 ㅠㅠ 사실 기초가 탄탄하지 않아서 여러가지 소스를 짬뽕하긴 했는데요 ! MainView에 결제 버튼을 누르면 Customer에 어떠한 값을 넘기고 싶어요 예를 들어 음식 이름이라던가 ... 근데 결제 프로그램을 누를때마다 Customer 프로그램이 자꾸 실행돼요 ㅠㅠ 값만 넘겨 받았으면 좋겠는데 ... 일단 제가 넣은 코드는 지운 상태입니다 ㅠㅠ 어떻게 수정해야 할까요 ? 메인뷰에 "결제"만 찾아서 봐주세요 ㅠㅠㅠㅠㅠ

MainView.java

package engine.swing.chat;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.JScrollPane;
import java.awt.ScrollPane;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.Component;

public class MainView extends JFrame {

	String PN = JOptionPane.showInputDialog(this, "포트 번호 입력 : ", "입력", JOptionPane.QUESTION_MESSAGE);
	
	static int MenuCheck = 1;
	
	int sum;
	
	String msg;
	
	JLabel TotalBox = new JLabel("메뉴를 선택해주세요 !");
	
	ArrayList<Integer> pay_list = new ArrayList(); //메뉴 합계 계산 어레이
	
	ArrayList Menu_list = new ArrayList(); //메뉴 합계 계산 어레이
	
	String nickName = "hh";

	DataOutputStream out;
	DataInputStream in;

	JPanel ChattingPanel = new JPanel();
	JTextArea Chattinglist = new JTextArea();
	JTextField tf = new JTextField();
	JButton add_btn = new JButton("\uACB0\uC81C");
	JButton del_btn = new JButton("\uC0AD\uC81C");
	JButton EnterBtn = new JButton("\uBCF4\uB0B4\uAE30");
	
	int qut_data;
	

	
	private JTextField IDField;
	private JTextField PWField;
	private JTable table;

	MainView(String nickName) {
		this.nickName = nickName;

		setSize(907, 401);
		setTitle("메뉴 주문 프로그램");
		setLocationRelativeTo(null);
		setDefaultCloseOperation(3);
		ChattingPanel.setBackground(Color.DARK_GRAY);
		ChattingPanel.setBounds(690, 0, 201, 362);
		ChattingPanel.setLayout(null);
		tf.setBackground(Color.LIGHT_GRAY);
		tf.setBounds(12, 335, 86, 21);
		ChattingPanel.add(tf);
		getContentPane().setLayout(null);
		getContentPane().add(ChattingPanel);
				

				Chattinglist.setFont(new Font("나눔고딕", Font.BOLD, 13));
				Chattinglist.setBackground(Color.LIGHT_GRAY);
				Chattinglist.setForeground(Color.BLACK);
				Chattinglist.setBounds(1, 1, 200, 324);
				ChattingPanel.add(Chattinglist);
				JScrollPane scrollPane_1 = new JScrollPane(Chattinglist);
				scrollPane_1.setBounds(0, 0, 201, 325);
				ChattingPanel.add(scrollPane_1);

				EnterBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						try {
							out.writeUTF("\r\n" + nickName + ">" + msg); //클라에게 전송 될 내용
							//out.writeUTF(nickName + "내용이다" + msg);}
						} catch (Exception e2) {
							e2.printStackTrace();
						}
						Chattinglist.append("\r\n" + nickName + ">" + msg); //여기에 쳤을때 텍필에 올라감			
						tf.setText("");
					}
				});
				EnterBtn.setBounds(110, 334, 79, 23);
				ChattingPanel.add(EnterBtn);
		
		ChattingPanel.setVisible(false);
		
		ArrayList<Integer> pay_list = new ArrayList(); //메뉴 합계 계산 어레이
		
		JPanel MainPanel = new JPanel();
		MainPanel.setVisible(false);
		MainPanel.setBounds(0, 0, 691, 362);
		getContentPane().add(MainPanel);
		MainPanel.setLayout(null);
		
		
		String tittleArray[] = {"음식", "가격", "홀/포장"};
		DefaultTableModel model = new DefaultTableModel(tittleArray, 0);
		JTable table = new JTable(model);
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBounds(12, 50, 334, 213);
		MainPanel.add(scrollPane);
		
		JRadioButton rb1 = new JRadioButton("\uD640");		
		rb1.setFont(new Font("나눔고딕", Font.PLAIN, 15));
		rb1.setBounds(22, 269, 53, 18);
		JRadioButton rb2 = new JRadioButton("\uD3EC\uC7A5");
		rb2.setFont(new Font("나눔고딕", Font.PLAIN, 15));
		rb2.setBounds(73, 269, 56, 18);
		TotalBox.setFont(new Font("나눔고딕", Font.BOLD, 16));
		TotalBox.setHorizontalAlignment(SwingConstants.CENTER);
		TotalBox.setForeground(Color.RED);
		

		TotalBox.setBounds(137, 263, 204, 35);
		MainPanel.add(TotalBox);
		rb1.setSelected(true); //초기 홀, 포장 설정
		
		MainPanel.add(rb1);
		MainPanel.add(rb2);
		
		
		add_btn.setForeground(new Color(255, 255, 255));
		add_btn.setFont(new Font("나눔고딕", Font.BOLD, 20));
		add_btn.setBackground(new Color(153, 0, 0));
		add_btn.setBounds(22, 299, 97, 44);
		MainPanel.add(add_btn);
		

		del_btn.setBackground(new Color(153, 0, 0));
		del_btn.setFont(new Font("나눔고딕", Font.BOLD, 20));
		del_btn.setForeground(new Color(255, 255, 255));
		del_btn.setBounds(129, 299, 97, 44);
		MainPanel.add(del_btn);
		
		JButton AllRemove = new JButton("\uBAA8\uB450 \uC0AD\uC81C");
		AllRemove.setBackground(new Color(153, 0, 0));
		AllRemove.setForeground(new Color(255, 255, 255));
		AllRemove.setFont(new Font("나눔고딕", Font.BOLD, 17));
		AllRemove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
						DefaultTableModel model = (DefaultTableModel)table.getModel();
						model.setNumRows(0); //제이리스트 데이터 모두 삭제
						pay_list.clear(); // 페이리스트 데이터 초기화
						Menu_list.clear();
					}
				});
	

		AllRemove.setBounds(238, 299, 103, 44);
		
		MainPanel.add(AllRemove);
		
		JButton Jja = new JButton("\uC9DC\uC7A5\uBA74");
		Jja.setForeground(new Color(255, 255, 255));
		Jja.setBackground(new Color(153, 0, 0));
		Jja.setFont(new Font("나눔바른고딕", Font.PLAIN, 20));
		Jja.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getActionCommand().equals("짜장면")) { // 추가 버튼 클릭이벤트 처리
					//TODO 입력된 값 테이블에 추가하기
					String info[] = new String[3]; //한행 (row) 에 저장할 데이터 모음
					info[0] = "짜장면";
					info[1] = "4,000";
					Menu_list.add("짜장면");
					pay_list.add(4000);
					sum = pay_list.stream().mapToInt(Integer::intValue).sum();
					TotalBox.setText(Integer.toString(sum));
					if(rb1.isSelected()) {
						info[2] = "홀";
					} 
					else {
						info[2] = "포장";
					} 
					model.addRow(info);	
			}}
		});
		
		Jja.setBounds(358, 53, 144, 50);
		MainPanel.add(Jja);
		
		JButton Jjam = new JButton("\uC9EC\uBF55");
		Jjam.setForeground(new Color(255, 255, 255));
		Jjam.setBackground(new Color(153, 0, 0));
		Jjam.setFont(new Font("나눔바른고딕", Font.PLAIN, 20));
		Jjam.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getActionCommand().equals("짬뽕")) { // 추가 버튼 클릭이벤트 처리
					//TODO 입력된 값 테이블에 추가하기
					String info[] = new String[3]; //한행 (row) 에 저장할 데이터 모음
					info[0] = "짬뽕";
					info[1] = "5,000";
					pay_list.add(5000);
					Menu_list.add("짬뽕");
					sum = pay_list.stream().mapToInt(Integer::intValue).sum();
					TotalBox.setText(Integer.toString(sum));
					if(rb1.isSelected()) {
						info[2] = "홀";
					} 
					else {
						info[2] = "포장";
					} 
					model.addRow(info);
				
			}}
		});
			

		Jjam.setBounds(524, 53, 144, 50);
		MainPanel.add(Jjam);
		
		JButton Bok = new JButton("\uBCF6\uC74C\uBC25");
		Bok.setForeground(new Color(255, 255, 255));
		Bok.setBackground(new Color(153, 0, 0));
		Bok.setFont(new Font("나눔바른고딕", Font.PLAIN, 20));
		Bok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getActionCommand().equals("볶음밥")) { // 추가 버튼 클릭이벤트 처리
					//TODO 입력된 값 테이블에 추가하기
					String info[] = new String[3]; //한행 (row) 에 저장할 데이터 모음
					info[0] = "볶음밥";
					info[1] = "8,000";
					Menu_list.add("볶음밥");
					pay_list.add(8000);
					sum = pay_list.stream().mapToInt(Integer::intValue).sum();
					TotalBox.setText(Integer.toString(sum));
					if(rb1.isSelected()) {
						info[2] = "홀";
					} 
					else {
						info[2] = "포장";
					} 
					model.addRow(info);

			}}
		});
		Bok.setBounds(358, 113, 144, 50);
		MainPanel.add(Bok);
		
		
		JButton Tang = new JButton("\uD0D5\uC218\uC721");
		Tang.setForeground(new Color(255, 255, 255));
		Tang.setBackground(new Color(153, 0, 0));
		Tang.setFont(new Font("나눔바른고딕", Font.PLAIN, 20));
		Tang.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getActionCommand().equals("탕수육")) { // 추가 버튼 클릭이벤트 처리
					//TODO 입력된 값 테이블에 추가하기
					String info[] = new String[3]; //한행 (row) 에 저장할 데이터 모음
					info[0] = "탕수육";
					info[1] = "10,000";
					Menu_list.add("볶음밥");
					pay_list.add(10000);
					sum = pay_list.stream().mapToInt(Integer::intValue).sum();
					TotalBox.setText(Integer.toString(sum));
					if(rb1.isSelected()) {
						info[2] = "홀";
					} 
					else {
						info[2] = "포장";
					} 
					model.addRow(info);
			}}
		});
		Tang.setBounds(524, 113, 144, 50);
		MainPanel.add(Tang);
		
		JButton Woo = new JButton("\uC6B0\uB3D9");
		Woo.setForeground(new Color(255, 255, 255));
		Woo.setBackground(new Color(153, 0, 0));
		Woo.setFont(new Font("나눔바른고딕", Font.PLAIN, 20));
		Woo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getActionCommand().equals("우동")) { // 추가 버튼 클릭이벤트 처리
					//TODO 입력된 값 테이블에 추가하기
					String info[] = new String[3]; //한행 (row) 에 저장할 데이터 모음
					info[0] = "우동";
					info[1] = "5,000";
					Menu_list.add("우동");
					pay_list.add(5000);
					sum = pay_list.stream().mapToInt(Integer::intValue).sum();
					TotalBox.setText(Integer.toString(sum));
					if(rb1.isSelected()) {
						info[2] = "홀";
					} 
					else {
						info[2] = "포장";
					} 
					model.addRow(info);
		
			}}
		});
		Woo.setBounds(358, 173, 144, 50);
		MainPanel.add(Woo);
		
		JButton Wol = new JButton("\uC6B8\uBA74");
		Wol.setForeground(new Color(255, 255, 255));
		Wol.setBackground(new Color(153, 0, 0));
		Wol.setFont(new Font("나눔바른고딕", Font.PLAIN, 20));
		Wol.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getActionCommand().equals("울면")) { // 추가 버튼 클릭이벤트 처리
					//TODO 입력된 값 테이블에 추가하기
					String info[] = new String[3]; //한행 (row) 에 저장할 데이터 모음
					info[0] = "울면";
					info[1] = "5,000";
					Menu_list.add("울면");
					pay_list.add(5000);
					sum = pay_list.stream().mapToInt(Integer::intValue).sum();
					TotalBox.setText(Integer.toString(sum));
					if(rb1.isSelected()) {
						info[2] = "홀";
					} 
					else {
						info[2] = "포장";
					} 
					model.addRow(info);
			
				}}
		});
		Wol.setBounds(524, 173, 144, 50);
		MainPanel.add(Wol);
		
		JButton JangBan = new JButton("\uC7C1\uBC18\uC9DC\uC7A5");
		JangBan.setForeground(new Color(255, 255, 255));
		JangBan.setBackground(new Color(153, 0, 0));
		JangBan.setFont(new Font("나눔바른고딕", Font.PLAIN, 20));
		JangBan.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getActionCommand().equals("쟁반짜장")) { // 추가 버튼 클릭이벤트 처리
					//TODO 입력된 값 테이블에 추가하기
					String info[] = new String[3]; //한행 (row) 에 저장할 데이터 모음
					info[0] = "쟁반짜장";
					info[1] = "8,000";
					Menu_list.add("쟁반짜장");
					pay_list.add(8000);
					sum = pay_list.stream().mapToInt(Integer::intValue).sum();
					TotalBox.setText(Integer.toString(sum));
					if(rb1.isSelected()) {
						info[2] = "홀";
					} 
					else {
						info[2] = "포장";
					} 
					model.addRow(info);
				}
			}
		});
		JangBan.setBounds(358, 233, 144, 50);
		MainPanel.add(JangBan);
		
		JButton Chadol = new JButton("\uCC28\uB3CC\uC9EC\uBF55");
		Chadol.setForeground(new Color(255, 255, 255));
		Chadol.setBackground(new Color(153, 0, 0));
		Chadol.setFont(new Font("나눔바른고딕", Font.PLAIN, 20));
		Chadol.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getActionCommand().equals("차돌짬뽕")) { // 추가 버튼 클릭이벤트 처리
					//TODO 입력된 값 테이블에 추가하기
					String info[] = new String[3]; //한행 (row) 에 저장할 데이터 모음
					info[0] = "차돌짬뽕";
					info[1] = "8,000";
					Menu_list.add("차돌짬뽕");
					pay_list.add(8000);
					sum = pay_list.stream().mapToInt(Integer::intValue).sum();
					TotalBox.setText(Integer.toString(sum));
					if(rb1.isSelected()) {
						info[2] = "홀";
					} 
					else {
						info[2] = "포장";
					} 
					model.addRow(info);
			}}
		});
		Chadol.setBounds(524, 233, 144, 50);
		MainPanel.add(Chadol);
		
		JButton SaChun = new JButton("\uC0AC\uCC9C\uC9DC\uC7A5");
		SaChun.setForeground(new Color(255, 255, 255));
		SaChun.setBackground(new Color(153, 0, 0));
		SaChun.setFont(new Font("나눔바른고딕", Font.PLAIN, 20));
		SaChun.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getActionCommand().equals("사천짜장")) { // 추가 버튼 클릭이벤트 처리
					//TODO 입력된 값 테이블에 추가하기
					String info[] = new String[3]; //한행 (row) 에 저장할 데이터 모음
					info[0] = "사천짜장";
					info[1] = "8,000";
					Menu_list.add("사천짜장");
					pay_list.add(8000);
					sum = pay_list.stream().mapToInt(Integer::intValue).sum();
					TotalBox.setText(Integer.toString(sum));
					if(rb1.isSelected()) {
						info[2] = "홀";
					} 
					else {
						info[2] = "포장";
					} 
					model.addRow(info);
			}
			}
		});
		SaChun.setBounds(358, 293, 144, 50);
		MainPanel.add(SaChun);
		
		JButton Gun = new JButton("\uAD70\uB9CC\uB450");
		Gun.setForeground(new Color(255, 255, 255));
		Gun.setBackground(new Color(153, 0, 0));
		Gun.setFont(new Font("나눔바른고딕", Font.PLAIN, 20));
		Gun.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getActionCommand().equals("군만두")) { // 추가 버튼 클릭이벤트 처리
					//TODO 입력된 값 테이블에 추가하기
					String info[] = new String[3]; //한행 (row) 에 저장할 데이터 모음
					info[0] = "군만두";
					info[1] = "5,000";
					Menu_list.add("군만두");
					pay_list.add(5000);
					sum = pay_list.stream().mapToInt(Integer::intValue).sum();
					TotalBox.setText(Integer.toString(sum));
					if(rb1.isSelected()) {
						info[2] = "홀";
					} 
					else {
						info[2] = "포장";
					} 
					model.addRow(info);
			}
			}
		});
		Gun.setBounds(524, 293, 144, 50);
		MainPanel.add(Gun);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(MainView.class.getResource("/image/BG.png")));
		lblNewLabel.setBounds(0, 0, 691, 362);
		MainPanel.add(lblNewLabel);

		
		JPanel Loginpanel = new JPanel();
		Loginpanel.setBounds(0, 0, 690, 490);
		getContentPane().add(Loginpanel);
		Loginpanel.setLayout(null);
		
		IDField = new JTextField();
		IDField.setText("ad");
		IDField.setBounds(297, 215, 116, 21);
		Loginpanel.add(IDField);
		IDField.setColumns(10);
		
		PWField = new JPasswordField();
		PWField.setBounds(297, 239, 116, 21);
		Loginpanel.add(PWField);
		PWField.setColumns(10);
		
		JButton Loingbtn = new JButton("");
		Loingbtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String id = "ad";
				String pass = "1234";
				if (id.equals(IDField.getText())&&pass.equals(PWField.getText())) {
					JOptionPane.showMessageDialog(null, "로그인 성공");
				Loginpanel.setVisible(false);
				ChattingPanel.setVisible(true);
				MainPanel.setVisible(true);

				}
				
				else {
					JOptionPane.showMessageDialog(null, "로그인 실패");
				
					}
				}
			});
		
		
				
			

		Loingbtn.setIcon(new ImageIcon(MainView.class.getResource("/image/Loginbtn.png")));
		Loingbtn.setBounds(419, 215, 78, 46);
		Loginpanel.add(Loingbtn);
		
		JLabel BGLabel = new JLabel("");
		BGLabel.setIcon(new ImageIcon(MainView.class.getResource("/image/BG.png")));
		BGLabel.setBounds(0, 0, 690, 490);
		Loginpanel.add(BGLabel);
		
		ActionListener action = new ActionListener(){			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub	
				if (e.getActionCommand().equals("결제")) { // 추가 버튼 클릭이벤트
					if(sum == 0 && e.getActionCommand().equals("결제")) {
						 JOptionPane.showMessageDialog(null, "선택 된 메뉴가 없습니다! 메뉴를 선택 해주세요");
					}
					if (sum > 0 && e.getActionCommand().equals("결제")) {
					qut_data = JOptionPane.showConfirmDialog(MainPanel,Menu_list+"\n"+pay_list.size()+"개의 음식이 결제되었습니다."+"\n"+"총합 : "+sum+"원","주문 성공!",JOptionPane.YES_NO_OPTION);
					if(qut_data == 0) { //예 버튼			
				        sum = pay_list.stream().mapToInt(Integer::intValue).sum();
//						JOptionPane.showMessageDialog(MainPanel, Menu_list+"\n"+pay_list.size()+"개의 음식이 결제되었습니다."+"\n"+"총합 : "+sum+"원","주문 확인", JOptionPane.YES_NO_CANCEL_OPTION); 
						DefaultTableModel model = (DefaultTableModel)table.getModel();
						model.setNumRows(0);
						TotalBox.setText("메뉴를 선택해주세요!");
							try {
//							if (Menu_list.isEmpty()) 
//									return;
								out.writeUTF("\r\n" + nickName + ">" + Menu_list + "\n 합계 : " + sum +"원");
								Chattinglist.append("\r\n" + nickName + ">" + Menu_list.toString() + "\n 합계 : " + sum +"원");
							} catch (IOException e1) {
								e1.printStackTrace();
							}

							Menu_list.clear();
							pay_list.clear();
					}
					}
					
					
					
					else if(qut_data == 1) { //아니오 버튼
						
					}
					
					//입력후 텍스트 필드 값 제거
					rb1.setSelected(true); //라디오 초기화
					rb2.setSelected(false); //라디오 초기화
					
					int rowIndex = table.getSelectedRow();
					//선택 안하고 누를 경우 리턴값 -1
					if(rowIndex == -1) {
						return;	
					}
					model.removeRow(rowIndex);
				}
				else if(e.getActionCommand().equals("홀")) { // 라디오 버튼 클릭이벤트 처리
					if(rb1.isSelected() == true) { //선택된 경우						
						rb2.setSelected(false); //라디오 변경
					}					
				}
				else if(e.getActionCommand().equals("포장")) { // 라디오 버튼 클릭이벤트 처리
					if(rb2.isSelected() == true) { //선택된 경우
						rb1.setSelected(false); //라디오 변경
					}					
				}
			}
			
		};
		
		add_btn.addActionListener(action);
		del_btn.addActionListener(action);
		EnterBtn.addActionListener(action);
		rb1.addActionListener(action);
		rb2.addActionListener(action);

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		EventHandler handle = new EventHandler();
		Chattinglist.addFocusListener(handle);
		tf.addFocusListener(handle);
		tf.addActionListener(handle);
		add_btn.addFocusListener(handle);
		add_btn.addActionListener(handle);
		EnterBtn.addFocusListener(handle);
		EnterBtn.addActionListener(handle);
		
		setVisible(true);
		tf.requestFocus();
		
	}
	
	public static int Menupass(){
		return 1;
	}

	
	public void startServer() {
		Socket soc = null;
		ServerSocket sk = null;
		int PN2 = Integer.parseInt(PN);


		try {
			
			sk = new ServerSocket(PN2);
			Chattinglist.append("포트 넘버 : "+ PN2 +"\n서버가 준비 되었습니다.\r\n클라이언트를 준비 해주세요 !\n");
			//Chattinglist.append("포트 넘버 : "+ PN2 +"\n서버가 준비 되었습니다.\r\n클라이언트를 준비 해주세요 !\n");
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			
			soc = sk.accept();
			Chattinglist.append("클라이언트와 연결되었습니다.");
			//Chattinglist.append("클라이언트와 연결되었습니다.");		
			OutputStream os = soc.getOutputStream();
			out = new DataOutputStream(os);

			InputStream is = soc.getInputStream();
			in = new DataInputStream(is);

			while (in != null) {
				Chattinglist.append("\r\n"+in.readUTF());
			}
			
			soc.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
		}
		
	
	class EventHandler extends FocusAdapter implements ActionListener {
		@Override
				public void actionPerformed(ActionEvent e) {
		
			msg = tf.getText();				
		
	}
		
		
//		public void Texting() {
//			if (MenuCheck.equals("1")) {
//			System.out.println("테스트 성공 /n");
//			}
//		}
	
		@Override
		public void focusGained(FocusEvent e) {
			
			tf.requestFocus();
				
	}
}

	
//	
	public static void main(String[] args) {
		MainView server = new MainView("손님");
		server.startServer();
	}
}

Customer.java

package engine.swing.chat;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

import java.awt.Font;
import javax.swing.JTable;
import javax.swing.JButton;

public class Customer extends JFrame {

	
	int test;
	String nickName = "";
	String ip = "";
	int port = 0;

	DataOutputStream out;
	DataInputStream in;

	JPanel ChattingPanel = new JPanel();
	JTextArea ta = new JTextArea();
	JTextField tf = new JTextField();
	JScrollPane scrollPane = new JScrollPane();
	JPanel MainPanel = new JPanel();
	JLabel MenuLabel = new JLabel();


	public Customer(String nickName, String ip, String port) {
		System.out.print(test);
		
		getContentPane().setBackground(new Color(51, 153, 102));
		
		String PN = JOptionPane.showInputDialog(this, "포트 번호 입력 : ", "입력", JOptionPane.QUESTION_MESSAGE);
		this.nickName = nickName;
		this.ip = ip;
		this.port = Integer.parseInt(PN);
		setSize(771, 456);
		setTitle("주방 서버");
		setLocationRelativeTo(null);
		setDefaultCloseOperation(3);
		getContentPane().setLayout(null);
		ChattingPanel.setBounds(496, 0, 259, 417);
		ChattingPanel.setBackground(new Color(204, 102, 102));
		ChattingPanel.setLayout(null);
		tf.setBounds(6, 358, 247, 41);
		ChattingPanel.add(tf);
		getContentPane().add(ChattingPanel);

		scrollPane.setBounds(6, 53, 247, 293);
		ChattingPanel.add(scrollPane);
				scrollPane.setViewportView(ta);
		
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		EventHandler handle = new EventHandler();
		tf.addFocusListener(handle);
		tf.addActionListener(handle);
		ta.addFocusListener(handle);
		ta.setEditable(false);
		
		JLabel lblNewLabel = new JLabel("\uD640 \uCC44\uD305");
		lblNewLabel.setFont(new Font("나눔고딕", Font.BOLD, 25));
		lblNewLabel.setForeground(new Color(255, 255, 255));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(6, 6, 247, 47);
		ChattingPanel.add(lblNewLabel);
		MainPanel.setBounds(0, 0, 497, 417);
		getContentPane().add(MainPanel);

		setVisible(true);
		tf.requestFocus();
		
		String tittleArray[] = {"음식", "가격", "홀/포장"};
		DefaultTableModel model = new DefaultTableModel(tittleArray, 0);
		MainPanel.setLayout(null);
		JTable table = new JTable(model);
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBounds(6, 32, 485, 134);
		MainPanel.add(scrollPane);
		MenuLabel.setText("\uD5EC\uB85C");
		MenuLabel.setFont(new Font("나눔고딕", Font.BOLD, 20));
		MenuLabel.setHorizontalAlignment(SwingConstants.CENTER);
		MenuLabel.setBounds(16, 178, 475, 63);
		
		
		MainPanel.add(MenuLabel);
		
		JButton btnNewButton = new JButton("New button");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					MenuLabel.setText("주문완료");
					System.out.print(test);
				
			}

		});
		btnNewButton.setBounds(80, 178, 90, 30);
		MainPanel.add(btnNewButton);


		
		
		String info[] = new String[3]; //한행 (row) 에 저장할 데이터 모음
		info[0] = "짜장면";
		info[1] = "4,000";
		model.addRow(info);	

	}








	void startClient() {
		try {
			Socket soc = new Socket(ip, port);
			ta.append("상대방과 연결되었습니다.");

			OutputStream os = soc.getOutputStream();
			out = new DataOutputStream(os);

			InputStream is = soc.getInputStream();
			in = new DataInputStream(is);
			while (in != null) {
				ta.append("\n"+in.readUTF());
			}
			soc.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	class EventHandler extends FocusAdapter implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			String msg = tf.getText();

			if (msg.isEmpty())
				return;
			
			try {
				out.writeUTF(nickName + ">" + msg);
			} catch (Exception e2) {
				e2.printStackTrace();
			}

			ta.append("\n" + nickName + ">" + msg);
			tf.setText("");
		}

		@Override
		public void focusGained(FocusEvent e) {
			tf.requestFocus();
		}
	}

	public static void main(String[] args) {
		Customer chat = new Customer("주방", "127.0.0.1","port");	
		chat.startClient();
		@SuppressWarnings("unused")
		Customer MenuCheck2 = new Customer(1);
	}
	}

 

상덕 2021-11-07
상덕 님께서 2021-11-07에 Java에 올린 질문

댓글

조회수 508
답글 0
URL