'한빛아카데미 - JAVA 마스터' 교재의 프로젝트 내용입니다.
PART13 화면 GUI 구성하기
BookMarketSwing - 프로젝트 생성
BookMarket 프로젝트의 com.market.member 패키지 이동
- UserInit 클래스 생성
package com.market.member;
public class UserInIt {
private static User mUser;
public static void setmUser(User mUser) {
UserInIt.mUser = mUser;
}
public static void init(String name, int phone) {
mUser = new User(name, phone);
}
public static User getmUser() {
return mUser;
}
}
BookMarket 프로젝트의 com.market.bookitem 패키지 이동
- BookInIt 클래스 생성
package com.market.bookitem;
import java.io.*;
import java.util.ArrayList;
public class BookInIt {
private static ArrayList<Book> mBookList;
private static int mTotalBook = 0;
public static void init() {
mTotalBook = totalFileToBookList();
mBookList = new ArrayList<Book>();
setFileToBookList(mBookList);
}
public static int totalFileToBookList() { // 파일에서 도서의 개수를 얻는 메서드 // BookMarket 프로젝트의 Welcome 클래스 메서드
try {
FileReader fr = new FileReader("book.txt"); // book.txt 파일을 읽기 위한 FileReder 객체 생성
BufferedReader reader = new BufferedReader(fr); // 파일에서 한 행씩 읽기 위한 BufferedReader 객체 생성
String str;
int num = 0;
while ((str = reader.readLine()) != null) { // 파일에서 읽을 행이 없을 때 까지 반복
if (str.contains("ISBN")) // 파일에서 읽은 한 행에 문자열 "ISBN"이 포함되어 있으면 도서의 개수 num을 1 증가시킴
++num;
}
reader.close(); // BufferedReader 객체 종료
fr.close(); // FileReader 객체 종료
return num;
} catch (Exception e) {
System.out.println(e);
}
return 0;
}
public static void setFileToBookList(ArrayList<Book> booklist) { // 파일에서 도서 정보 목록을 읽어 저장하는 매서드 // BookMarket 프로젝트의 Welcome 클래스 메서드
try {
FileReader fr = new FileReader("book.txt"); // book.txt 파일을 읽기 위한 FileReder 객체 생성
BufferedReader reader = new BufferedReader(fr); // 파일에서 한 행씩 읽기 위한 BufferedReader 객체 생성
String str2;
String[] readBook = new String[7];
while ((str2 = reader.readLine()) != null) { // 파일에서 읽을 행이 없을 때까지 반복
if (str2.contains("ISBN")) { // 파일에서 읽은 한 행에 문자열 "ISBN"이 포함되어 있으면 도서 정보에 대해 한 행씩 읽어 지역변수 readBook에 저장
readBook[0] = str2;
readBook[1] = reader.readLine();
readBook[2] = reader.readLine();
readBook[3] = reader.readLine();
readBook[4] = reader.readLine();
readBook[5] = reader.readLine();
readBook[6] = reader.readLine();
}
Book bookitem = new Book(readBook[0], readBook[1], Integer.parseInt(readBook[2]), readBook[3], readBook[4], readBook[5], readBook[6]);
booklist.add(bookitem);
}
reader.close(); // BufferedReader 객체 종료
fr.close(); // FileReader 객체 종료
} catch (Exception e) {
System.out.println(e);
}
}
public static ArrayList<Book> getmBookList() { // 도서 목록 불러오는 메서드
return mBookList;
}
public static void setmBookList(ArrayList<Book> mBookList) { // 도서 목록 저장하는 메서드
BookInIt.mBookList = mBookList;
}
public static int getmTotalBook() { // 총 도서 수량 불러오는 메서드
return mTotalBook;
}
public static void setmTotalBook(int mTotalBook) { // 총 도서 수량 저장하는 메서드
BookInIt.mTotalBook = mTotalBook;
}
}
BookMarket 프로젝트의 com.market.cart 패키지 이동
- Cart.java 의 getter(), setter() 메서드 추가
package com.market.cart;
import java.util.ArrayList;
import com.market.bookitem.Book; // Book 클래스 사용하기 위한 선언
public class Cart implements CartInterface { // CartInterface 인터페이스의 자식 클래스 Cart 생성
public ArrayList<CartItem> mCartItem = new ArrayList<CartItem>(); // mCartItem은 ArrayList 클래스를 이용하여 장바구니에 항목을 담는 객체 변수
public static int mCartCount = 0; // 장바구니에 담긴 항목의 총 개수를 저장하는 변수
public Cart() { // Cart 클래스의 기본 생성자
}
public ArrayList<CartItem> getmCartItem(){ // 장바구니의 도서 항목 불러오는 메서드
return mCartItem;
}
public void setmCartItem(ArrayList<CartItem> mCartItem) { // 장바구니의 도서 항목 저장하는 메서드
this.mCartItem = mCartItem;
}
public void printBookList(ArrayList<Book> booklist) { // Book[] booklist -> ArrayList<Book> 전체 도서 정보 목록 출력 구현
for (int i = 0; i < booklist.size(); i++) {
Book bookitem = booklist.get(i);
System.out.print(bookitem.getBookId() + " | ");
System.out.print(bookitem.getName() + " | ");
System.out.print(bookitem.getUnitPrice() + " | ");
System.out.print(bookitem.getAuthor() + " | ");
System.out.print(bookitem.getDescription() + " | ");
System.out.print(bookitem.getCategory() + " | ");
System.out.print(bookitem.getReleaseDate() + " | ");
System.out.println("");
}
}
public void insertBook(Book book) { // CartItem에 도서 정보를 등록하는 메서드 구현
CartItem bookitem = new CartItem(book);
mCartItem.add(bookitem);
mCartCount = mCartItem.size();
}
public void deleteBook() { // 장바구니의 모든 항목을 삭제하는 메서드 구현
mCartItem.clear();
mCartCount = 0;
}
public void printCart() {
System.out.println("장바구니 상품 목록 :");
System.out.println("-----------------------------------------------");
System.out.println(" 도서ID \t| 수 량 \t| 합 계");
for (int i = 0; i < mCartCount; i++) {
System.out.print(" " + mCartItem.get(i).getBookID() + "\t| ");
System.out.print(" " + mCartItem.get(i).getQuantity() + "\t| ");
System.out.print(" " + mCartItem.get(i).getTotalPrice());
System.out.println(" ");
}
System.out.println("-----------------------------------------------");
}
public boolean isCartInBook(String bookId) { // 장바구니에 담긴 도서의 ID와 장바구니에 담을 도서의 ID를 비교하는 메서드 구현
boolean flag = false;
for (int i = 0; i < mCartItem.size(); i++) {
if (bookId == mCartItem.get(i).getBookID()) {
mCartItem.get(i).setQuantity(mCartItem.get(i).getQuantity() + 1);
flag = true;
}
}
return flag;
}
public void removeCart(int numId) { // 장바구니 순번 numId의 항목을 삭제하는 메서드 구현
mCartItem.remove(numId);
mCartCount = mCartItem.size();
}
}
com.market.main 패키지 생성
- GuestWindow 클래스 생성
- MainWindow 클래스 생성
package com.market.main;
import javax.swing.*;
import java.awt.*;
public class GuestWindow extends JFrame {
public GuestWindow(String title, int x, int y, int width, int height) {
initContainer(title, x, y, width, height); // initContainer() 메서드 호출
setVisible(true); // 프레임 보이기 설정
setResizable(true); // 프레임 크기 조절 가능 설정
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // 프레임 닫기 버튼 가능 설정
setIconImage(new ImageIcon("src/images/shop.png").getImage()); // 프레임 아이콘 표시 // ./images/shop.png 이미지 불러오지 못함
}
private void initContainer(String title, int x, int y, int width, int height) {
setTitle(title); // 프레임 제목 설정
setBounds(x, y, width, height); // 프레임 위치, 크기 설정
setLayout(null); // 프레임 레이아웃 미설정
Font ft;
ft = new Font("함초롬돋움", Font.BOLD, 15); // 글꼴, 스타일, 크기 설정
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screenSize.width - 1000) / 2, (screenSize.height - 750 ) / 2); // 컴퓨터 화면에 맞춰 프레임 창을 화면 중앙에 출력
// user.png 이미지 표시를 위한 패널 영역 설정 및 출력
JPanel userPanel = new JPanel();
userPanel.setBounds(0, 100, 1000, 256);
ImageIcon imageIcon = new ImageIcon("src/images/user.png"); // ./images/user.png 이미지 불러오지 못함
imageIcon.setImage(imageIcon.getImage().getScaledInstance(160, 160, Image.SCALE_SMOOTH));
JLabel userLabel = new JLabel(imageIcon);
userPanel.add(userLabel);
add(userPanel);
// -- 고객 정보를 입력하세요 -- 표시를 위한 패널 영역 설정 및 출력
JPanel titlePanel = new JPanel();
titlePanel.setBounds(0, 350, 1000, 50);
add(titlePanel);
JLabel titleLabel = new JLabel("-- 고객 정보를 입력하세요 --");
titleLabel.setFont(ft); // JLabel인 titleLabel 글꼴 설정
titleLabel.setForeground(Color.BLUE); // JLabel인 titleLabel 문자열 색상 설정
titlePanel.add(titleLabel);
// 이름 표시를 위한 패널 영역 설정 및 출력
JPanel namePanel = new JPanel();
namePanel.setBounds(0, 400, 1000, 50);
add(namePanel);
JLabel nameLabel = new JLabel("이 름 : ");
nameLabel.setFont(ft);
namePanel.add(nameLabel);
// 이름 표시를 위한 패널 영역 설정 및 출력
JTextField nameField = new JTextField(10);
nameField.setFont(ft);
namePanel.add(nameField);
// 연락처 표시를 위한 패널 영역 설정 및 출력
JPanel phonePanel = new JPanel();
phonePanel.setBounds(0, 450, 1000, 50);
add(phonePanel);
JLabel phoneLabel = new JLabel("연락처 : ");
phonePanel.setFont(ft);
phonePanel.add(phoneLabel);
JTextField phoneField = new JTextField(10);
phonePanel.setFont(ft);
phonePanel.add(phoneField);
// <쇼핑하기> 버튼 표시를 위한 패널 영역 설정 및 출력
JPanel buttonPanel = new JPanel();
buttonPanel.setBounds(0, 500, 1000, 100);
add(buttonPanel);
JLabel buttonLabel = new JLabel("쇼핑하기", new ImageIcon("src/images/shop.png"), JLabel.LEFT); // images/shop.png 이미지 불러오지 못함
buttonLabel.setFont(ft);
JButton enterButton = new JButton();
enterButton.add(buttonLabel);
buttonPanel.add(enterButton);
}
// public static void main(String[] args) {
// new GuestWindow("고객 정보 입력", 0, 0, 1000, 750); // 테스트 화면 창을 띄우기 위한 실행용 메서드
// }
}
package com.market.main;
import javax.swing.*;
import java.awt.*;
public class MainWindow extends JFrame {
static JPanel mMenuPanel, mPagePanel;
public MainWindow(String title, int x, int y, int width, int height) {
initContainer(title, x, y, width, height); // initContainer() 메서드 호출
setVisible(true); // 프레임 보이기 설정
setResizable(true); // 프레임 크기 조절 가능 설정
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // 프레임 닫기 버튼 가능 설정
setIconImage(new ImageIcon("src/images/shop.png").getImage()); // 프레임 아이콘 표시 // ./images/shop.png 이미지 불러오지 못함
}
private void initContainer(String title, int x, int y, int width, int height) {
setTitle(title); // 프레임 제목 설정
setBounds(x, y, width, height); // 프레임 위치, 크기 설정
setLayout(null); // 프레임 레이아웃 미설정
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screenSize.width - 1000) / 2, (screenSize.height - 750 ) / 2); // 컴퓨터 화면에 맞춰 프레임 창을 화면 중앙에 출력
//메뉴 버튼 표시를 위한 프레임 상단의 패널 영역 설정 및 출력
mMenuPanel = new JPanel();
mMenuPanel.setBounds(0, 20, width, 130);
menuIntroduction();
add(mMenuPanel);
// 메뉴 버튼별 클릭 시 페이지 표시를 위한 프레임 하단의 패널 영역 설정 및 출력
mPagePanel = new JPanel();
mPagePanel.setBounds(0, 150, width, height);
add(mPagePanel);
}
private void menuIntroduction() {
Font ft;
ft = new Font("함초롬돋움", Font.BOLD, 15); // 글꼴, 스타일, 크기 설정
// <고객 정보 확인하기> 버튼 표시를 위한 패널 영역 설정 및 출력
JButton bt1 = new JButton("고객 정보 확인하기", new ImageIcon("src/images/1.png"));
bt1.setBounds(0, 0, 100, 50);
bt1.setFont(ft);
mMenuPanel.add(bt1);
// <장바구니 상품 목록보기> 버튼 표시를 위한 패널 영역 설정 및 출력
JButton bt2 = new JButton("장바구니 상품 목록보기", new ImageIcon("src/images/2.png"));
bt2.setBounds(0, 0, 100, 30);
bt2.setFont(ft);
mMenuPanel.add(bt2);
// <장바구니 비우기> 버튼 표시를 위한 패널 영역 설정 및 출력
JButton bt3 = new JButton("장바구니 비우기", new ImageIcon("src/images/3.png"));
bt3.setBounds(0, 0, 100, 30);
bt3.setFont(ft);
mMenuPanel.add(bt3);
// <장바구니에 항목 추가하기> 버튼 표시를 위한 패널 영역 설정 및 출력
JButton bt4 = new JButton("장바구니에 항목 추가하기", new ImageIcon("src/images/4.png"));
bt4.setFont(ft);
mMenuPanel.add(bt4);
// <장바구니의 항목 수량 줄이기> 버튼 표시를 위한 패널 영역 설정 및 출력
JButton bt5 = new JButton("장바구니의 항목 수량 줄이기", new ImageIcon("src/images/5.png"));
bt5.setFont(ft);
mMenuPanel.add(bt5);
// <장바구니의 항목 삭제하기> 버튼 표시를 위한 패널 영역 설정 및 출력
JButton bt6 = new JButton("장바구니의 항목 삭제하기", new ImageIcon("src/images/6.png"));
bt6.setFont(ft);
mMenuPanel.add(bt6);
// <주문하기> 버튼 표시를 위한 패널 영역 설정 및 출력
JButton bt7 = new JButton("주문하기", new ImageIcon("src/images/7.png"));
bt7.setFont(ft);
mMenuPanel.add(bt7);
// <종료> 버튼 표시를 위한 패널 영역 설정 및 출력
JButton bt8 = new JButton("종료", new ImageIcon("src/images/8.png"));
bt8.setFont(ft);
mMenuPanel.add(bt8);
// <관리자> 버튼 표시를 위한 패널 영역 설정 및 출력
JButton bt9 = new JButton("관리자", new ImageIcon("src/images/9.png"));
bt9.setFont(ft);
mMenuPanel.add(bt9);
}
// public static void main(String[] args) {
// new MainWindow("도서 쇼핑몰", 0, 0, 1000, 750); // 테스트 화면 창을 띄우기 위한 실행용 메서드
// }
}
com.market.page 패키지 생성
- GuestInfoPage, CartAddItemPage, CartItemListPage, CartShippingPage, CartOrderBillPage, AdminPage 클래스 생성
package com.market.page;
import javax.swing.*;
import java.awt.*;
public class GuestInfoPage extends JPanel {
public GuestInfoPage(JPanel panel) {
Font ft;
ft = new Font("함초롬돋움", Font.BOLD, 15); // 글꼴, 스타일, 크기 설정
setLayout(null);
Rectangle rect = panel.getBounds();
System.out.println(rect);
setPreferredSize(rect.getSize());
// 이름 표시를 위한 패널 영역 설정 및 출력
JPanel namePanel = new JPanel();
namePanel.setBounds(0, 100, 1000, 50);
add(namePanel);
JLabel nameLabel = new JLabel("이 름 : ");
nameLabel.setFont(ft);
nameLabel.setBackground(Color.BLUE);
JLabel nameField = new JLabel();
nameField.setText("입력된 고객 이름");
nameField.setFont(ft);
namePanel.add(nameLabel);
namePanel.add(nameField);
// 연락처 표시를 위한 패널 영역 설정 및 출력
JPanel phonePanel = new JPanel();
phonePanel.setBounds(0, 150, 1000, 100);
add(phonePanel);
JLabel phoneLabel = new JLabel("연락처 : ");
phoneLabel.setFont(ft);
JLabel phoneField = new JLabel();
phoneField.setText("입력된 고객 연락처");
phoneField.setFont(ft);
phonePanel.add(phoneLabel);
phonePanel.add(phoneField);
}
// public static void main(String[] args) { // 테스트 화면 창을 띄우기 위한 실행용 메서드
// JFrame frame = new JFrame();
// frame.setBounds(0, 0, 1000, 750);
// frame.setLayout(null);
//
// JPanel mPagePanel = new JPanel();
// mPagePanel.setBounds(0, 150, 1000, 750);
//
// frame.add(mPagePanel);
// mPagePanel.add("고객 정보 확인하기", new GuestInfoPage(mPagePanel));
// frame.setVisible(true);
// }
}
package com.market.page;
import javax.swing.*;
import com.market.bookitem.Book;
import com.market.bookitem.BookInIt;
import com.market.cart.Cart;
import java.awt.*;
import java.util.ArrayList;
public class CartAddItemPage extends JPanel {
ImageIcon imageBook;
int mSelectRow = 0;
Cart mCart;
public CartAddItemPage(JPanel panel, Cart cart) {
Font ft;
ft = new Font("함초롬돋움", Font.BOLD, 15); // 글꼴, 스타일, 크기 설정
setLayout(null);
Rectangle rect = panel.getBounds();
System.out.println(rect);
setPreferredSize(rect.getSize());
mCart = cart;
// 도서 이밎 표시를 위한 패널 영역 설정 및 출력
JPanel imagePanel = new JPanel();
imagePanel.setBounds(20, 0, 300, 400);
imageBook = new ImageIcon("src/images/ISBN1234.jpg");
imageBook.setImage(imageBook.getImage().getScaledInstance(250, 300, Image.SCALE_DEFAULT));
JLabel label = new JLabel(imageBook);
imagePanel.add(label);
add(imagePanel);
// 도서 목록 테이블 표시를 위한 패널 영역 설정 및 출력
JPanel tablePanel = new JPanel();
tablePanel.setBounds(300, 0, 700, 400);
add(tablePanel);
ArrayList<Book> booklist = BookInIt.getmBookList();
Object[] tableHeader = { "도서ID", "도서명", "가격", "저자", "설명", "분야", "출판일" };
Object[][] content = new Object[booklist.size()][tableHeader.length];
for (int i = 0; i < booklist.size(); i++) {
Book bookitem = booklist.get(i);
content[i][0] = bookitem.getBookId();
content[i][1] = bookitem.getName();
content[i][2] = bookitem.getUnitPrice();
content[i][3] = bookitem.getAuthor();
content[i][4] = bookitem.getDescription();
content[i][5] = bookitem.getCategory();
content[i][6] = bookitem.getReleaseDate();
}
JTable bookTable = new JTable(content, tableHeader);
bookTable.setRowSelectionInterval(0, 0);
bookTable.getSelectedColumn();
JScrollPane jScrollPane = new JScrollPane();
jScrollPane.setPreferredSize(new Dimension(600, 350));
jScrollPane.setViewportView(bookTable);
tablePanel.add(jScrollPane);
// <장바구니에 담기> 버튼 표시를 위한 패널 영역 설정 및 출력
JPanel buttonPanel = new JPanel();
buttonPanel.setBounds(0, 400, 1000, 400);
add(buttonPanel);
JLabel buttonLabel = new JLabel("장바구니에 담기");
buttonLabel.setFont(ft);
JButton addButton = new JButton();
addButton.add(buttonLabel);
buttonPanel.add(addButton);
}
// public static void main(String[] args) { // 테스트 화면 창을 띄우기 위한 실행용 메서드
// Cart mCart = new Cart();
// JFrame frame = new JFrame();
// frame.setBounds(0, 0, 1000, 750);
// frame.setLayout(null);
//
// JPanel mPagePanel = new JPanel();
// mPagePanel.setBounds(0, 150, 1000, 750);
// frame.add(mPagePanel);
//
// BookInIt.init();
// mPagePanel.add("장바구니에 항목 추가하기", new CartAddItemPage(mPagePanel, mCart));
// frame.setVisible(true);
// }
}
package com.market.page;
import javax.swing.*;
import com.market.cart.Cart;
import com.market.cart.CartItem;
import java.awt.*;
import java.util.ArrayList;
public class CartItemListPage extends JPanel {
JTable cartTable;
Object[] tableHeader = { "도서ID", "도서명", "단가", "수량", "총가격" };
Cart mCart = new Cart();
public static int mSelectRow = -1;
public CartItemListPage(JPanel panel, Cart cart) {
Font ft;
ft = new Font("함초롬돋움", Font.BOLD, 15); // 글꼴, 스타일, 크기 설정
this.mCart = cart;
this.setLayout(null);
Rectangle rect = panel.getBounds();
System.out.println(rect);
this.setPreferredSize(rect.getSize());
// 도서가 담긴 장바구니의 테이블 표시를 위한 패널 영역 설정 및 출력
JPanel bookPanel = new JPanel();
bookPanel.setBounds(0, 0, 1000, 400);
add(bookPanel);
ArrayList<CartItem> cartItem = mCart.getmCartItem();
Object[][] content = new Object[cartItem.size()][tableHeader.length];
Integer totalPrice = 0;
for (int i = 0; i < cartItem.size(); i++) {
CartItem item = cartItem.get(i);
content[i][0] = item.getBookID();
content[i][1] = item.getItemBook().getName();
content[i][2] = item.getItemBook().getUnitPrice();
content[i][3] = item.getQuantity();
content[i][4] = item.getTotalPrice();
totalPrice += item.getQuantity() * item.getItemBook().getUnitPrice();
}
cartTable = new JTable(content, tableHeader);
JScrollPane jScrollPane = new JScrollPane();
jScrollPane.setPreferredSize(new Dimension(600, 350));
jScrollPane.setViewportView(cartTable);
bookPanel.add(jScrollPane);
// 총 금액 표시를 위한 패널 영역 설정 및 출력
JPanel totalPricePanel = new JPanel();
totalPricePanel.setBounds(0, 400, 1000, 50);
// totalPricePanel.setBackground(Color.RED);
JLabel totalPricelabel = new JLabel("총금액: " + totalPrice + " 원");
totalPricelabel.setForeground(Color.red);
totalPricelabel.setFont(ft);
totalPricePanel.add(totalPricelabel);
add(totalPricePanel);
// <장바구니 비우기>, <장바구니의 항목 삭제하기>, <장바구니 새로 고침> 버튼 표시를 위한 패널 영역 설정 및 출력
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBounds(0, 450, 1000, 50);
add(buttonPanel);
JLabel buttonLabel = new JLabel("장바구니 비우기");
buttonLabel.setFont(ft);
JButton clearButton = new JButton();
clearButton.add(buttonLabel);
buttonPanel.add(clearButton);
JLabel removeLabel = new JLabel("장바구니의 항목 삭제하기");
removeLabel.setFont(ft);
JButton removeButton = new JButton();
removeButton.add(removeLabel);
buttonPanel.add(removeButton);
JLabel refreshLabel = new JLabel("장바구니 새로 고침");
refreshLabel.setFont(ft);
JButton refreshButton = new JButton();
refreshButton.add(refreshLabel);
buttonPanel.add(refreshButton);
}
// public static void main(String[] args) { // 테스트 화면 창을 띄우기 위한 실행용 메서드
//
// Cart mCart = new Cart();
// JFrame frame = new JFrame();
// frame.setBounds(0, 0, 1000, 750);
// frame.setLayout(null);
//
// JPanel mPagePanel = new JPanel();
// mPagePanel.setBounds(0, 150, 1000, 750);
//
// frame.add(mPagePanel);
// mPagePanel.add("장바구니의 상품 목록 보기", new CartItemListPage(mPagePanel, mCart));
// frame.setVisible(true);
// }
}
package com.market.page;
import javax.swing.*;
import java.awt.*;
public class CartShippingPage extends JPanel {
JPanel shippingPanel;
JPanel radioPanel;
public CartShippingPage(JPanel panel) {
Font ft;
ft = new Font("함초롬돋움", Font.BOLD, 15); // 글꼴, 스타일, 크기 설정
setLayout(null);
Rectangle rect = panel.getBounds();
System.out.println(rect);
setPreferredSize(rect.getSize());
// 고객 정보 라디오버튼 선택 표시를 위한 패널 영역 설정 및 출력
radioPanel = new JPanel();
radioPanel.setBounds(300, 0, 700, 50);
radioPanel.setLayout(new FlowLayout());
add(radioPanel);
JLabel radioLabel = new JLabel("배송받을 분은 고객 정보와 같습니까?");
radioLabel.setFont(ft);
JRadioButton radioOk = new JRadioButton("예");
radioOk.setFont(ft);
JRadioButton radioNo = new JRadioButton("아니요");
radioNo.setFont(ft);
radioPanel.add(radioLabel);
radioPanel.add(radioOk);
radioPanel.add(radioNo);
// 배송지 입력, 영수증 표시를 위한 패널 영역 설정 및 출력
shippingPanel = new JPanel();
shippingPanel.setBounds(200, 50, 700, 500);
shippingPanel.setLayout(null);
add(shippingPanel);
radioOk.setSelected(true);
radioNo.setSelected(false);
UserShippingInfo(true);
}
public void UserShippingInfo(boolean select) {
Font ft;
ft = new Font("함초롬돋움", Font.BOLD, 15);
// 고객명 표시를 위한 패널 영역 설정 및 출력
JPanel namePanel = new JPanel();
namePanel.setBounds(0, 100, 700, 50);
// namePanel.setBackground(Color.GRAY);
JLabel nameLabel = new JLabel("고객명 : ");
nameLabel.setFont(ft);
namePanel.add(nameLabel);
JTextField nameLabel2 = new JTextField(15);
nameLabel2.setFont(ft);
if (select) {
nameLabel2.setBackground(Color.LIGHT_GRAY);
nameLabel2.setText("입력된 고객 이름");
}
namePanel.add(nameLabel2);
shippingPanel.add(namePanel);
// 연락처 표시를 위한 패널 영역 설정 및 출력
JPanel phonePanel = new JPanel();
phonePanel.setBounds(0, 150, 700, 50);
JLabel phoneLabel = new JLabel("연락처 : ");
phoneLabel.setFont(ft);
phonePanel.add(phoneLabel);
JTextField phoneLabel2 = new JTextField(15);
phoneLabel2.setFont(ft);
if (select) {
phoneLabel2.setBackground(Color.LIGHT_GRAY);
phoneLabel2.setText("입력된 고객 연락처");
}
phonePanel.add(phoneLabel2);
shippingPanel.add(phonePanel);
// 배송지 표시를 위한 패널 영역 설정 및 출력
JPanel addressPanel = new JPanel();
addressPanel.setBounds(0, 200, 700, 50);
JLabel label = new JLabel("배송지 : ");
label.setFont(ft);
addressPanel.add(label);
JTextField addressText = new JTextField(15);
addressText.setFont(ft);
addressPanel.add(addressText);
shippingPanel.add(addressPanel);
// <주문 완려> 버튼 표시를 위한 패널 영역 설정 및 출력
JPanel buttonPanel = new JPanel();
buttonPanel.setBounds(0, 300, 700, 100);
JLabel buttonLabel = new JLabel("주문 완료");
buttonLabel.setFont(new Font("함초롬돋움", Font.BOLD, 15));
JButton orderButton = new JButton();
orderButton.add(buttonLabel);
buttonPanel.add(orderButton);
shippingPanel.add(buttonPanel);
}
// public static void main(String[] args) { // 테스트 화면 창을 띄우기 위한 실행용 메서드
//
// JFrame frame = new JFrame();
// frame.setBounds(0, 0, 1000, 750);
// frame.setLayout(null);
//
// JPanel mPagePanel = new JPanel();
// mPagePanel.setBounds(0, 150, 1000, 750);
//
// frame.add(mPagePanel);
// mPagePanel.add("주문 배송지", new CartShippingPage(mPagePanel));
// frame.setVisible(true);
//
// }
}
package com.market.page;
import javax.swing.*;
import com.market.bookitem.BookInIt;
import com.market.cart.Cart;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CartOrderBillPage extends JPanel {
Cart mCart;
JPanel shippingPanel;
JPanel radioPanel;
public CartOrderBillPage(JPanel panel, Cart cart) {
Font ft;
ft = new Font("함초롬돋움", Font.BOLD, 15);
setLayout(null);
Rectangle rect = panel.getBounds();
System.out.println(rect);
setPreferredSize(rect.getSize());
this.mCart = cart;
// 주문 내용 결과 표시를 위한 패널 영역 설정 및 출력
shippingPanel = new JPanel();
shippingPanel.setBounds(200, 50, 700, 500);
shippingPanel.setLayout(null);
add(shippingPanel);
printBillInfo("입력된 고객 이름", "입력된 고객 연락처", "입력된 고객 배송지");
}
public void printBillInfo(String name, String phone, String address) {
Font ft;
ft = new Font("함초롬돋움", Font.BOLD, 15); // 글꼴, 스타일, 크기 설정
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String strDate = formatter.format(date);
// -배송받을 고객 정보 표시를 위한 패널 영역 설정 및 출력
JPanel panel01 = new JPanel();
panel01.setBounds(0, 0, 500, 30);
// panel01.setBackground(Color.GRAY);
JLabel label01 = new JLabel("---------------------배송받을 고객 정보-----------------------");
label01.setFont(ft);
panel01.add(label01);
shippingPanel.add(panel01);
// 고객명, 연락처 표시를 위한 패널 영역 설정 및 출력
JPanel panel02 = new JPanel();
panel02.setBounds(0, 30, 500, 30);
JLabel label02 = new JLabel("고객명 : " + name + " 연락처 : " + phone);
label02.setHorizontalAlignment(JLabel.LEFT);
label02.setFont(ft);
panel02.add(label02);
shippingPanel.add(panel02);
// 배송지, 발송일 표시를 위한 패녈 영역 설정 및 출력
JPanel panel03 = new JPanel();
panel03.setBounds(0, 60, 500, 30);
JLabel label03 = new JLabel("배송지 : " + address + " 발송일 : " + strDate);
label03.setHorizontalAlignment(JLabel.LEFT);
label03.setFont(ft);
panel03.add(label03);
shippingPanel.add(panel03);
// 주문 도서 목록 표시를 위한 패널 영역 설정 및 출력
JPanel printPanel = new JPanel();
printPanel.setBounds(0, 100, 500, 300);
printCart(printPanel);
shippingPanel.add(printPanel);
}
public void printCart(JPanel panel) {
Font ft;
ft = new Font("함초롬돋움", Font.BOLD, 12);
// 장바구니 상품 목록 표시를 위한 패널 영역 설정 및 출력
JPanel panel01 = new JPanel();
panel01.setBounds(0, 0, 500, 5);
JLabel label01 = new JLabel(" 장바구니 상품 목록 :");
label01.setFont(ft);
panel01.add(label01);
panel.add(panel01);
// ---- 표시를 위한 패널 영역 설정 및 출력
JPanel panel02 = new JPanel();
panel02.setBounds(0, 20, 500, 5);
JLabel label02 = new JLabel("------------------------------------");
label02.setFont(ft);
panel02.add(label02);
panel.add(panel02);
// 타이틀 표시를 위한 패널 영역 설정 및 출력
JPanel panel03 = new JPanel();
panel03.setBounds(0, 25, 500, 5);
JLabel label03 = new JLabel(" 도서ID | 수량 | 합계 ");
label03.setFont(ft);
panel03.add(label03);
panel.add(panel03);
// ---- 표시를 위한 패널 영역 설정 및 출력
JPanel panel04 = new JPanel();
panel04.setBounds(0, 30, 500, 5);
JLabel label04 = new JLabel("--------------------------------------");
label04.setFont(ft);
panel04.add(label04);
panel.add(panel04);
for (int i = 0; i < mCart.mCartItem.size(); i++) { // 13 // 장바구니 주문 결과 표시를 위한 패널 영역 설정 및 출력
JPanel panel05 = new JPanel();
panel05.setBounds(0, 35 + (i * 5), 500, 5);
JLabel label05 = new JLabel(" " + mCart.mCartItem.get(i).getBookID() + " "
+ mCart.mCartItem.get(i).getQuantity() + " "
+ mCart.mCartItem.get(i).getTotalPrice());
label05.setFont(ft);
panel05.add(label05);
panel.add(panel05);
}
// ---- 표시를 위한 패널 영역 설정 및 출력
JPanel panel06 = new JPanel();
panel06.setBounds(0, 35 + (mCart.mCartItem.size() * 5), 500, 5);
JLabel label06 = new JLabel("--------------------------------------");
label06.setFont(ft);
panel06.add(label06);
panel.add(panel06);
int sum = 0;
for (int i = 0; i < mCart.mCartCount; i++)
sum += mCart.mCartItem.get(i).getTotalPrice();
System.out.println("------------" + mCart.mCartCount);
// 주문 총금액 표시를 위한 패널 영역 설정 및 출력
JPanel panel07 = new JPanel();
panel07.setBounds(0, 40 + (mCart.mCartItem.size() * 5), 500, 5);
JLabel label07 = new JLabel(" 주문 총금액 : " + sum + "원");
// label04.setHorizontalAlignment(JLabel.CENTER);
label07.setFont(new Font("함초롬돋움", Font.BOLD, 15));
panel07.add(label07);
panel.add(panel07);
}
// public static void main(String[] args) { // 테스트 화면 창을 띄우기 위한 실행용 메서드
//
// Cart mCart = new Cart();
// JFrame frame = new JFrame();
// frame.setBounds(0, 0, 1000, 750);
// frame.setLayout(null);
//
// JPanel mPagePanel = new JPanel();
// mPagePanel.setBounds(0, 150, 1000, 750);
//
// frame.add(mPagePanel);
// BookInIt.init();
// mPagePanel.add("주문하기", new CartOrderBillPage(mPagePanel, mCart));
// frame.setVisible(true);
//
// }
}
package com.market.page;
import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AdminPage extends JPanel {
public AdminPage(JPanel panel) {
Font ft;
ft = new Font("함초롬돋움", Font.BOLD, 15); // 글꼴, 스타일, 크기 설정
setLayout(null);
Rectangle rect = panel.getBounds();
System.out.println(rect);
setPreferredSize(rect.getSize());
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyMMddhhmmss");
String strDate = formatter.format(date);
// 도서 ID 표시를 위한 패널 영역 설정 및 출력
JPanel idPanel = new JPanel();
idPanel.setBounds(100, 0, 700, 50);
JLabel idLabel = new JLabel("도서ID : ");
idLabel.setFont(ft);
JLabel idTextField = new JLabel();
idTextField.setFont(ft);
idTextField.setPreferredSize(new Dimension(290, 50));
idTextField.setText("ISBN" + strDate);
idPanel.add(idLabel);
idPanel.add(idTextField);
add(idPanel);
// 도서명 표시를 위한 패널 영역 설정 및 출력
JPanel namePanel = new JPanel();
namePanel.setBounds(100, 50, 700, 50);
JLabel nameLabel = new JLabel("도서명 : ");
nameLabel.setFont(ft);
JTextField nameTextField = new JTextField(20);
nameTextField.setFont(ft);
namePanel.add(nameLabel);
namePanel.add(nameTextField);
add(namePanel);
// 가격 표시를 위한 패널 영역 설정 및 출력
JPanel pricePanel = new JPanel();
pricePanel.setBounds(100, 100, 700, 50);
JLabel priceLabel = new JLabel("가 격 : ");
priceLabel.setFont(ft);
JTextField priceTextField = new JTextField(20);
priceTextField.setFont(ft);
pricePanel.add(priceLabel);
pricePanel.add(priceTextField);
add(pricePanel);
// 저자 표시를 위한 패널 영역 설정 및 출력
JPanel authorPanel = new JPanel();
authorPanel.setBounds(100, 150, 700, 50);
JLabel authorLabel = new JLabel("저 자 : ");
authorLabel.setFont(ft);
JTextField authorTextField = new JTextField(20);
authorTextField.setFont(ft);
authorPanel.add(authorLabel);
authorPanel.add(authorTextField);
add(authorPanel);
// 설명 표시를 위한 패널 영역 설정 및 출력
JPanel descPanel = new JPanel();
descPanel.setBounds(100, 200, 700, 50);
JLabel descLabel = new JLabel("설 명 : ");
descLabel.setFont(ft);
JTextField descTextField = new JTextField(20);
descTextField.setFont(ft);
descPanel.add(descLabel);
descPanel.add(descTextField);
add(descPanel);
//분야 표시를 위한 패널 영역 설정 및 출력
JPanel categoryPanel = new JPanel();
categoryPanel.setBounds(100, 250, 700, 50);
JLabel categoryLabel = new JLabel("분 야 : ");
categoryLabel.setFont(ft);
JTextField categoryTextField = new JTextField(20);
categoryTextField.setFont(ft);
categoryPanel.add(categoryLabel);
categoryPanel.add(categoryTextField);
add(categoryPanel);
//출판일 표시를 위한 패널 영역 설정 및 출력
JPanel datePanel = new JPanel();
datePanel.setBounds(100, 300, 700, 50);
JLabel dateLabel = new JLabel("출판일 : ");
dateLabel.setFont(ft);
JTextField dateTextField = new JTextField(20);
dateTextField.setFont(ft);
datePanel.add(dateLabel);
datePanel.add(dateTextField);
add(datePanel);
//<추가>, <취소> 버튼 표시를 위한 패널 영역 설정 및 출력
JPanel buttonPanel = new JPanel();
buttonPanel.setBounds(100, 350, 700, 50);
add(buttonPanel);
JLabel okLabel = new JLabel("추가");
okLabel.setFont(ft);
JButton okButton = new JButton();
okButton.add(okLabel);
buttonPanel.add(okButton);
JLabel noLabel = new JLabel("취소");
noLabel.setFont(ft);
JButton noButton = new JButton();
noButton.add(noLabel);
buttonPanel.add(noButton);
}
//
// public static void main(String[] args) { // 테스트 화면 창을 띄우기 위한 실행용 메서드
//
// JFrame frame = new JFrame();
// frame.setBounds(0, 0, 1000, 750);
// frame.setLayout(null);
//
// JPanel mPagePanel = new JPanel();
// mPagePanel.setBounds(0, 150, 1000, 750);
//
// frame.add(mPagePanel);
// mPagePanel.add("주문하기", new AdminPage(mPagePanel));
// frame.setVisible(true);
// }
}
'Java > 온라인 서점' 카테고리의 다른 글
온라인 서점 프로젝트 PART14 (0) | 2023.08.09 |
---|---|
온라인 서점 프로젝트 PART12 (0) | 2023.08.03 |
온라인 서점 프로젝트 PART11 (1) | 2023.08.03 |
온라인 서점 프로젝트 PART10 (0) | 2023.08.02 |
온라인 서점 프로젝트 PART9 (0) | 2023.08.02 |