본문 바로가기

   
Programming/Java

18일차!

반응형
package awt.basic;

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class ComponentDemo3 extends JFrame {
      public ComponentDemo3(){
           setLayout( new FlowLayout());
           
           String[] items = { "인문" , "기술" , "문학" , "외국어" };
           JComboBox<String> combobox = new JComboBox<String>(items );
           
           add( combobox);
           
           JButton btn = new JButton( "선택된 거 가져오기" );
           
           add( btn);
            btn.addActionListener( e->{
                String item = (String)combobox .getSelectedItem();
                System. out.println( item);
           });
           
           JButton btn2 = new JButton( "지정된 거 가져오기" );
           
           add( btn2);
            btn2.addActionListener( e->{
                 combobox.setSelectedItem( "외국어" );
           });
           
           setBounds(100, 100, 400, 300);
           setVisible( true);
           setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE );
     }
     
      public static void main(String[] args) {
            new ComponentDemo3();
     }

}



package awt.basic;

import java.awt.Button;
import java.awt.FlowLayout;


import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import swing.util.GroupButtonUtils;


public class ComponentDemo4 extends JFrame{
     
      public ComponentDemo4(){
           
           setLayout( new FlowLayout());
           
           JRadioButton btn1 = new JRadioButton( "부서명" , true );
           JRadioButton btn2 = new JRadioButton( "직책" );
           JRadioButton btn3 = new JRadioButton( "이름" );
           
           add( btn1);
           add( btn2);
           add( btn3);
           
            btn1.addActionListener( e->{
                 if( btn1.isSelected()){
                }
           });
            btn2.addActionListener( e->{
                 if( btn2.isSelected()){
                }
           });
            btn3.addActionListener( e->{
                 if( btn3.isSelected()){
                }
           });
           
           ButtonGroup group = new ButtonGroup();
           
            group.add( btn1);
            group.add( btn2);
            group.add( btn3);
           
           
           
           JTextField field = new JTextField(20);
           add( field);
           
           JButton btn = new JButton( "찾기" );
           add( btn);
           
            btn.addActionListener( e->{
                String value = GroupButtonUtils. selectedText(group );
                System. out.println( value);
           });
           
           setVisible( true);
           setBounds(200, 200, 300, 300);
           setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE );
     }
     
      public static void main(String[] args) {
            new ComponentDemo4();
     }
}


package swing.util;

import java.util.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;

public class GroupButtonUtils {
      public static String selectedText(ButtonGroup group){
           String text = null;
           
           Enumeration<AbstractButton> buttons = group .getElements();
            while( buttons.hasMoreElements()){
                AbstractButton button = buttons.nextElement();
                 if( button.isSelected()){
                      text = button.getText();
                }
           }
            group.getElements();
           
            return text;
     }
}


도서관리프로그램
package swing.book;

public class Book {
      private int no;
      private String category;
      private String title;
      private String author;
      private String publisher;
      private String date;
      private int price;
      private String grade;
     
      public Book( int no, String category, String title, String author,
                String publisher, String dateint price, String grade) {
            super();
            this. no = no;
            this. category = category;
            this. title = title;
            this. author = author;
            this. publisher = publisher;
            this. date = date;
            this. price = price;
            this. grade = grade;
           
     }
      public int getNo() {
            return no;
     }
      public void setNo( int no) {
            this. no = no;
     }
      public String getCategory () {
            return category;
     }
      public void setCategory(String category) {
            this. category = category;
     }
      public String getTitle() {
            return title;
     }
      public void setTitle(String title) {
            this. title = title;
     }
      public String getAuthor() {
            return author;
     }
      public void setAuthor(String author) {
            this. author = author;
     }
      public String getPublisher() {
            return publisher;
     }
      public void setPublisher(String publisher) {
            this. publisher = publisher;
     }
      public String getDate() {
            return date;
     }
      public void setDate(String date) {
            this. date = date;
     }
      public int getPrice() {
            return price;
     }
      public void setPrice( int price) {
            this. price = price;
     }
      public String getGrade() {
            return grade;
     }
      public void setGrade(String grade) {
            this. grade = grade;
     }
     
     
}


package swing.book;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import product.ProductModel;

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

public class BookApp {

      private JFrame frame;
      private JTable table;
      private BookTableModel model = new BookTableModel();
      private NewBookDialog newBook = new NewBookDialog( model);
      private BookSearchTableModel smodel = new BookSearchTableModel(model );
      private SearchBookDialog searchBook = new SearchBookDialog(smodel );
      /**
      * Launch the application.
      */
      public static void main(String[] args) {
           EventQueue. invokeLater( new Runnable() {
                 public void run() {
                      try {
                           BookApp window = new BookApp();
                            window. frame.setVisible( true);
                     } catch (Exception e) {
                            e.printStackTrace();
                     }
                }
           });
     }

      /**
      * Create the application.
      */
      public BookApp() {
           initialize();
     }

      /**
      * Initialize the contents of the frame.
      */
      private void initialize() {
            frame = new JFrame();
            frame.setBounds(100, 100, 581, 405);
            frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE );
            frame.getContentPane().setLayout( null);
           
           JButton addButton = new JButton( "\uC2E0\uADDC");
            addButton.addActionListener( new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                      newBook.setVisible( true);
                }
           });
            addButton.setBounds(87, 44, 97, 23);
            frame.getContentPane().add( addButton);
           
           JButton searchbutton new JButton("\uAC80\uC0C9" );
            searchbutton .addActionListener( new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                      searchBook.setVisible( true);
                }
           });
            searchbutton .setBounds(259, 44, 97, 23);
            frame.getContentPane().add( searchbutton );
           
           JPanel panel = new JPanel();
            panel.setBorder( new TitledBorder( new EtchedBorder(), "도서목록" ));
            panel.setBounds(12, 99, 545, 264);
            frame.getContentPane().add( panel);
            panel.setLayout( null);
           
           JScrollPane scrollPane = new JScrollPane();
            scrollPane.setBounds(12, 22, 521, 232);
            panel.add( scrollPane);
           
            table = new JTable( model);
            scrollPane.setViewportView( table);
           
     }
}


package swing.book;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

public class BookSearchTableModel extends AbstractTableModel{
      private String[] names = { "번호" "분야" "도서명" "저자" "출판사" "출판일" "가격" "평점" };
      private ArrayList<Book> searchBooks = new ArrayList<Book>();
     
     BookTableModel model;
     
      public BookSearchTableModel(BookTableModel model) {
            this. model = model;
     }
     
      @Override
      public String getColumnName( int column) {
            return names[ column];
     }
     
      @Override
      public int getColumnCount() {
            return names. length;
     }
     
      @Override
      public int getRowCount() {
            return searchBooks.size();
     }
     
      @Override
      public Object getValueAt( int rowIndexint columnIndex) {
           Book b = searchBooks.get( rowIndex);
           
            if( columnIndex == 0){
                 return b.getNo();
           } else if( columnIndex == 1){
                 return b.getCategory();
           } else if( columnIndex == 2){
                 return b.getTitle();
           } else if( columnIndex == 3){
                 return b.getAuthor();
           } else if( columnIndex == 4){
                 return b.getPublisher();
           } else if( columnIndex == 5){
                 return b.getDate();
           } else if( columnIndex == 6){
                 return b.getPrice();
           } else if( columnIndex == 7){
                 return b.getGrade();
           }
            return null;
     }
     
      //검색
      public void searchBook(String value, String searchText){
           ArrayList<Book> books = model.getBooks();
            //전달받은 값을 기준으로 도서명이면 .도서명으로 검색한 리스트를 temp리스트에 담는다.
            if( value.equals( "분야" )){
                 for(Book b : books){
                     System. out.println( b.getCategory());
                      if( b.getCategory().equals( searchText)){
                            searchBooks.add( b);                        
                     }
                }
           } else if( value.equals( "도서명" )){
                 for(Book b : books){
                      if( b.getTitle().equals( searchText)){
                            searchBooks.add( b);
                     }
                }
                
           } else if( value.equals( "출판사" )){
                 for(Book b : books){
                      if( b.getPublisher().equals( searchText)){
                            searchBooks.add( b);
                     }
                }
                
           } else if( value.equals( "가격" )){
                 for(Book b : books){
                      if(Integer. toString( b.getPrice()).equals( searchText)){
                            searchBooks.add( b);
                     }
                }
           }
           
            //검색한값을 searcbooks에 있는거를 table에 넣어준다.           
            this.fireTableDataChanged();
     }    
     
}


package swing.book;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import swing.table.Contact;

public class BookTableModel extends AbstractTableModel {

      private String[] names = { "번호" "분야" "도서명" "저자" "출판사" "출판일" "가격" "평점" };
      private ArrayList<Book> books = new ArrayList<Book>();

      public ArrayList<Book> getBooks() {
            return books;
     }
     
      @Override
      public String getColumnName( int column) {
            return names[ column];
     }
     
      @Override
      public int getColumnCount() {
            return names. length;
     }
     
      @Override
      public int getRowCount() {
            return books.size();
     }
     
      @Override
      public Object getValueAt(int rowIndex int columnIndex ) {
           Book b = books.get( rowIndex);
           
            if( columnIndex == 0){
                 return b .getNo();
           } else if( columnIndex == 1){
                 return b .getCategory();
           } else if( columnIndex == 2){
                 return b .getTitle();
           } else if( columnIndex == 3){
                 return b .getAuthor();
           } else if( columnIndex == 4){
                 return b .getPublisher();
           } else if( columnIndex == 5){
                 return b .getDate();
           } else if( columnIndex == 6){
                 return b .getPrice();
           } else if( columnIndex == 7){
                 return b .getGrade();
           }
            return null;
     }
     
      //도서등록
      public void addBook(Book book){
            books.add( book);
            this.fireTableDataChanged();
     }
     
      public ArrayList<Book> book(){
            return books;
     }
}


package swing.book;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;

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

public class NewBookDialog extends JDialog {

      private final JPanel contentPanel = new JPanel();
      private JTextField noField;
      private JTextField titletextField;
      private JTextField authortextField;
      private JTextField publishertextField;
      private JTextField datetextField;
      private JTextField pricetextField;
      private BookTableModel model;
      private JComboBox gradecomboBox ;
      private JComboBox categorycomboBox ;

      public NewBookDialog(BookTableModel model) {
           
           setBounds(100, 100, 450, 315);
           getContentPane().setLayout( new BorderLayout());
            contentPanel.setBorder( new EmptyBorder(5, 5, 5, 5));
           getContentPane().add( contentPanel, BorderLayout.CENTER );
            contentPanel.setLayout( null);
           {
                JPanel panel = new JPanel();
                 panel.setBorder( new TitledBorder( new EtchedBorder(), "신규책등록" ));
                 panel.setBounds(12, 10, 410, 226);
                 contentPanel.add( panel);
                 panel.setLayout( null);
                {
                     JLabel lblNewLabel = new JLabel("\uB3C4\uC11C\uBC88\uD638" );
                      lblNewLabel.setBounds(12, 20, 57, 15);
                      panel.add( lblNewLabel);
                }
                {
                     JLabel label = new JLabel( "\uBD84    \uC57C");
                      label.setBounds(12, 45, 57, 15);
                      panel.add( label);
                }
                {
                     JLabel label = new JLabel( "\uB3C4 \uC11C \uBA85");
                      label.setBounds(12, 70, 57, 15);
                      panel.add( label);
                }
                {
                     JLabel label = new JLabel( " \uC800    \uC790 ");
                      label.setBounds(12, 95, 57, 15);
                      panel.add( label);
                }
                {
                     JLabel label = new JLabel( "\uCD9C \uD310 \uC0AC");
                      label.setBounds(12, 119, 57, 15);
                      panel.add( label);
                }
                {
                     JLabel label = new JLabel( "\uCD9C \uD310 \uC77C");
                      label.setBounds(12, 145, 57, 15);
                      panel.add( label);
                }
                {
                     JLabel label = new JLabel( "\uAC00    \uACA9");
                      label.setBounds(12, 173, 57, 15);
                      panel.add( label);
                }
                {
                     JLabel label = new JLabel( "\uD3C9    \uC810");
                      label.setBounds(12, 198, 57, 15);
                      panel.add( label);
                }
                {
                      noField = new JTextField();
                      noField.setBounds(81, 17, 302, 21);
                      panel.add( noField);
                      noField.setColumns(10);
                }
                {
                      titletextField = new JTextField();
                      titletextField.setColumns(10);
                      titletextField.setBounds(81, 67, 302, 21);
                      panel.add( titletextField);
                }
                {
                      authortextField = new JTextField();
                      authortextField.setColumns(10);
                      authortextField.setBounds(81, 92, 302, 21);
                      panel.add( authortextField);
                }
                {
                      publishertextField = new JTextField();
                      publishertextField.setColumns(10);
                      publishertextField.setBounds(81, 116, 302, 21);
                      panel.add( publishertextField);
                }
                {
                      datetextField = new JTextField();
                      datetextField.setColumns(10);
                      datetextField.setBounds(81, 139, 302, 21);
                      panel.add( datetextField);
                }
                {
                      pricetextField = new JTextField();
                      pricetextField.setColumns(10);
                      pricetextField.setBounds(81, 170, 302, 21);
                      panel.add( pricetextField);
                }
                
                String[] items = { "철학" "예술" "역사" "과학" "사회과학" };
                String[] list = {"★" ,"★★" ,"★★★" ,"★★★★" ,"★★★★★" };
                
                 gradecomboBox = new JComboBox(list );
                 gradecomboBox.addActionListener( new ActionListener() {
                      public void actionPerformed(ActionEvent e) {
                           
                     }
                });
                 gradecomboBox.setBounds(81, 195, 302, 21);
                 panel.add( gradecomboBox);
                
                 categorycomboBox = new JComboBox(items );
                 categorycomboBox.addActionListener( new ActionListener() {
                      public void actionPerformed(ActionEvent e) {
                     }
                });
                 categorycomboBox.setBounds(81, 42, 302, 21);
                 panel.add( categorycomboBox);
                
           }
           {
                JPanel buttonPane = new JPanel();
                 buttonPane.setLayout( new FlowLayout(FlowLayout.RIGHT ));
                getContentPane().add( buttonPane, BorderLayout.SOUTH );
                {
                     JButton okButton = new JButton("\uB4F1\uB85D" );
                      okButton.addActionListener( new ActionListener() {
                            public void actionPerformed(ActionEvent e ) {
                                 int no = Integer.parseInt( noField.getText());
                                String category = (String)categorycomboBox .getSelectedItem();
                                String title = titletextField .getText();
                                String author = authortextField .getText();
                                String publisher = publishertextField .getText();
                                String date = datetextField .getText();
                                 int price = Integer.parseInt( pricetextField.getText());
                                String grade = (String)gradecomboBox .getSelectedItem();
                                
                                Book book = new Book( nocategorytitleauthorpublisherdatepricegrade);                              
                                
                                 model.addBook( book);
                                NewBookDialog. this.setVisible( false);
                           }
                     });
                      okButton.setActionCommand( "OK");
                      buttonPane.add( okButton);
                     getRootPane().setDefaultButton( okButton);
                }
                {
                     JButton cancelButton = new JButton("\uCDE8\uC18C" );
                      cancelButton.addActionListener( new ActionListener() {
                            public void actionPerformed(ActionEvent e ) {
                                NewBookDialog. this.setVisible( false);
                           }
                     });
                      cancelButton.setActionCommand( "Cancel");
                      buttonPane.add( cancelButton);
                }
           }
     }
}



package swing.book;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import swing.util.GroupButtonUtils;

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

public class SearchBookDialog extends JDialog {

      private final JPanel contentPanel = new JPanel();
      private JTextField searcField;
      private JTable table;
      private GroupButtonUtils util;
      private BookTableModel model = new BookTableModel();
      /**
      * Create the dialog.
      */
      public SearchBookDialog(BookSearchTableModel model) {      
           setBounds(100, 100, 671, 391);
           getContentPane().setLayout( new BorderLayout());
            contentPanel.setBorder( new EmptyBorder(5, 5, 5, 5));
           getContentPane().add( contentPanel, BorderLayout.CENTER );
            contentPanel.setLayout( null);
           {
                JPanel panel = new JPanel();               
                 panel.setBorder( new TitledBorder( new EtchedBorder(), "검색 옵션" ));
                 panel.setBounds(12, 10, 410, 91);
                 contentPanel.add( panel);
                 panel.setLayout( null);
                ButtonGroup group = new ButtonGroup();
                JRadioButton rdbtnNewRadioButton = new JRadioButton("\uBD84\uC57C" );
                 rdbtnNewRadioButton.setBounds(8, 20, 57, 23);
                 panel.add( rdbtnNewRadioButton);
                
                JRadioButton radioButton = new JRadioButton("\uB3C4\uC11C\uBA85" );
                 radioButton.setBounds(79, 20, 70, 23);
                 panel.add( radioButton);
                
                JRadioButton radioButton_1 = new JRadioButton("\uCD9C\uD310\uC0AC" );
                 radioButton_1.setBounds(153, 20, 69, 23);
                 panel.add( radioButton_1);
                
                JRadioButton radioButton_2 = new JRadioButton("\uAC00\uACA9" );
                 radioButton_2.setBounds(226, 20, 57, 23);
                 panel.add( radioButton_2);
                
                 group.add( rdbtnNewRadioButton);
                 group.add( radioButton);
                 group.add( radioButton_1);
                 group.add( radioButton_2);
                
                JLabel lblNewLabel = new JLabel("\uAC80\uC0C9\uC5B4" );
                 lblNewLabel.setBounds(8, 49, 44, 15);
                 panel.add( lblNewLabel);
                
                 searcField = new JTextField();
                 searcField.setBounds(64, 46, 219, 21);
                 panel.add( searcField);
                 searcField.setColumns(10);
                
                JButton btnNewButton = new JButton("\uAC80\uC0C9" );
                 btnNewButton.addActionListener( new ActionListener() {
                      public void actionPerformed(ActionEvent e) {
                            //라디오 버튼을 우선 그룹핑한다.
                           String value = util selectedText( group);
                           String searchText = searcField.getText();
                            model.searchBook( valuesearchText);
                     }
                });
                 btnNewButton.setBounds(295, 45, 97, 23);
                 panel.add( btnNewButton);
           }
           
           JScrollPane scrollPane = new JScrollPane();
            scrollPane.setBounds(12, 111, 631, 232);
            contentPanel.add( scrollPane);
           
           
            table = new JTable( model);
            scrollPane.setViewportView( table);
     }
}




반응형