자바 실습 수정

private void jBtn1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here: 
        //VECTOR 
         
        this.memVec.add(addMember(jNameTxt.getText(), jNumTxt.getText())); 
        this.pack(); 
         
        System.out.println(memVec); 
        display(1); 
    }                                      
 
    private void jBtn2ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here: 
        for(int i = 0; i < ary.length; i++) { 
            if(ary[i] == null) { 
                ary[i] = addMember(jNameTxt.getText(), jNumTxt.getText()); 
                break; 
            } 
        } 
        display(2); 
         
    }    
 
 
private void display(int flag) { 
      jDisplayPanel.removeAll(); 
      jDisplayPanel.repaint(); 
        if(flag == 1) { 
            int size = memVec.size(); 
            for(int i = 0; i < size; i++) { 
                Member mem = (Member)memVec.get(i); 
                jDisplayPanel.add(new JButton(mem.getName())); 
                 
            } 
        } 
        else { 
            for(int i = 0; i < ary.length; i++) { 
                if(ary[i] != null) { 
                    jDisplayPanel.add(new JButton(ary[i].getName())); 
                } 
                 
                else 
                    continue; 
            } 
        } 
        this.pack(); 
    } 
     
    private Member addMember(String name, String number) { 
        Member newMember = new Member(name, number); 
         
        JButton newBtn = new JButton(name); 
         
        this.jDisplayPanel.add(newBtn); 
         
        return newMember; 
    }

 

자바 첫번째 실습

1

이번 2학기 자바 실습은 NetBeans IDE를 사용하여 GUI를 간편하게 만들 수 있다.

그렇기에 GUI관련 코드부분은 생략하고 중요한 부분만 올려두려고 한다.

 

private void jBtn1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here: 
        String nameStr = this.jNameTxt.getText(); 
 //       String idnumStr = this.jIdNumTxt2.getText(); 
//        JButton newbtn = new JButton(nameStr); 
//         
//        jLowerPanel.add(newbtn); 
//        jLowerPanel.setVisible(true); 
 
        Member newMember = new Member(nameStr); 
         
        JButton newBtn = new JButton(nameStr); 
        this.jDisplayPanel.add(newBtn); 
         
        this.memVec.add(newMember); 
        this.pack(); 
         
        System.out.println(memVec); 
        display(1); 
    }                                      
 
    private void jBtn2ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here: 
        //String nameStr = this.jNameTxt.getText(); 
       // String idnumStr = this.jIdNumTxt2.getText(); 
         
        for(int i = 0; i < str.length; i++) { 
            if(str[i].equals("")) { 
                str[i] = jNameTxt.getText(); 
                jNameTxt.setText(""); 
                break; 
            } 
        } 
         
        display(2); 
         
    } 
 
private void display(int flag) { 
        jDisplayPanel.removeAll(); 
        jDisplayPanel.repaint(); 
 
        if(flag == 1) { 
            int size = memVec.size(); 
            for(int i = 0; i < size; i++) { 
                Member mem = (Member)memVec.get(i); 
                jDisplayPanel.add(new JButton(mem.getName())); 
                 
            } 
        } 
        else { 
            for(int i = 0; i < str.length; i++) { 
                if(!str[i].equals("")) { 
                    jDisplayPanel.add(new JButton(str[i])); 
                } 
                else 
                    continue; 
            } 
        } 
        this.pack(); 
    }

Member.java

package kr.razberry; 
 
/** 
 * 
 * @author capta 
 */ 
public class Member { 
     
    private String name = ""; 
    private String idNum = ""; 
 
    public Member(String name) { 
        this.name = name; 
    } 
     
    public Member(String name, String idNum) { 
        this.name = name; 
        this.idNum = idNum; 
    } 
 
    /** 
     * Get the value of name 
     * 
     * @return the value of name 
     */ 
    public String getName() { 
        return name; 
    } 
 
    /** 
     * Set the value of name 
     * 
     * @param name new value of name 
     */ 
    public void setName(String name) { 
        this.name = name; 
    } 
 
    /** 
     * Get the value of idNum 
     * 
     * @return the value of idNum 
     */ 
    public String getIdNum() { 
        return idNum; 
    } 
 
    /** 
     * Set the value of idNum 
     * 
     * @param idNum new value of idNum 
     */ 
    public void setIdNum(String idNum) { 
        this.idNum = idNum; 
    } 
     
    @Override 
    public String toString() { 
        return "Member{" + "name=" + name + " idnum=" + idNum +"}"; 
    } 
 
}