Results 1 to 2 of 2
  1. #1
    Steven Park is offline Member
    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    Default My project was slowed down after applying GUI...

    CRUD

    UI.java

    import java.util.Scanner;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class UI extends JFrame {
    static final long serialVersionUID = 42L;
    Service service = new Service();
    int c=0;
    User temp, ttemp = new User("0", "0", "0");
    String name, idNum, type;
    static Scanner n = new Scanner(System.in);

    JLabel lbtemp, up2_lb1, up2_lb2, up2_lb3, up2_lb4;
    JButton up2_bt1;
    JTextField up2_tf1, up2_tf2, up2_tf3;
    JPanel up, up1, up2, lo, jtemp;

    UI() {
    setTitle("회원관리시스템");
    setLayout(new BorderLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // 초기 등록, 수정, 삭제화면
    up = new JPanel();
    up.setLayout(new GridLayout(1, 2));

    up1 = new JPanel();
    up1.setLayout(new GridLayout(3, 1));
    JButton up1_reg = new JButton("사용자 등록");
    JButton up1_dis = new JButton("사용자 조회");
    JButton up1_del = new JButton("사용자 삭제");
    RegisterListener regl = new RegisterListener();
    RemoveListener rl = new RemoveListener();
    DisplayListener dl = new DisplayListener();

    up1_reg.addActionListener(regl);
    up1_dis.addActionListener(dl);
    up1_del.addActionListener(rl);
    up1.add(up1_reg);
    up1.add(up1_dis);
    up1.add(up1_del);

    up2 = new JPanel();
    up2.setLayout(null);
    up.add(up1);
    up.add(up2);

    lo = new JPanel();
    JButton closeB = new JButton("닫기");
    closeB.setPreferredSize(new Dimension(80, 30));
    closeB.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    System.exit(0);
    }
    });
    lo.add(closeB);

    add(lo, BorderLayout.SOUTH);
    add(up, BorderLayout.CENTER);

    setSize(500, 250);
    setVisible(true);

    }

    private class DisplayListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
    up.remove(1); // 기존패널 제거
    User[] t = service.search(temp, 1);
    Font font = new Font("Gulim", Font.BOLD, 25);
    jtemp = new JPanel();

    jtemp.setLayout(new GridLayout(t.length+1,3));

    jtemp.setPreferredSize(new Dimension(50,30*(t.length+1)));
    jtemp.setBackground(Color.YELLOW);
    jtemp.setFont(font);

    JLabel lb1 = new JLabel("ID", SwingConstants.CENTER);
    JLabel lb2 = new JLabel("Name", SwingConstants.CENTER);
    JLabel lb3 = new JLabel("Type", SwingConstants.CENTER);
    lb1.setFont(font);
    lb2.setFont(font);
    lb3.setFont(font);
    lb1.setPreferredSize(new Dimension(50,30));
    lb2.setPreferredSize(new Dimension(50,30));
    lb3.setPreferredSize(new Dimension(50,30));
    jtemp.add(lb1);
    jtemp.add(lb2);
    jtemp.add(lb3);


    for (int i = 0; i < t.length; i++) {
    jtemp.add(new JLabel(t[i].getIDNum()));
    jtemp.add(new JLabel(t[i].getName()));
    jtemp.add(new JLabel(t[i].changeType(t[i].getType()), SwingConstants.CENTER));
    }
    up.remove(up2);
    up.add(jtemp);
    up.revalidate();
    }
    }

    private class RemoveListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    up.remove(1);
    jtemp = new JPanel();
    jtemp.setLayout(new GridLayout(2,2));

    up2_lb3 = new JLabel("ID", SwingConstants.CENTER);
    up2_tf3 = new JTextField("");

    up2_lb4 = new JLabel("");
    up2_bt1 = new JButton("Register");
    up2_bt1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    String t1 = up2_tf3.getText();
    temp = service.delete(new User(t1, "", ""));
    if (temp.equals(ttemp)) {
    JOptionPane.showMessageDialog(jtemp, "그런 사용자는 없습니다.");
    return;
    }
    JOptionPane.showMessageDialog(jtemp, temp.getName() + " "
    + temp.changeType(temp.getType()) + "님이 삭제되었습니다.");
    up2_lb3.setText("");
    }
    });

    jtemp.add(up2_lb3);
    jtemp.add(up2_tf3);

    jtemp.add(up2_lb4);
    jtemp.add(up2_bt1);
    up.remove(up2);
    up.add(jtemp);
    up.revalidate();
    }
    }

    private class RegisterListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    // JButton b = (JButton)e.getSource();
    up.remove(1);
    jtemp = new JPanel();
    jtemp.setLayout(new GridLayout(4, 2));
    jtemp.setBackground(Color.white);

    up2_lb1 = new JLabel("ID", SwingConstants.CENTER);
    up2_tf1 = new JTextField("");
    up2_lb2 = new JLabel("Type", SwingConstants.CENTER);
    up2_tf2 = new JTextField("");
    up2_lb3 = new JLabel("Name", SwingConstants.CENTER);
    up2_tf3 = new JTextField("");

    up2_lb4 = new JLabel("");
    up2_bt1 = new JButton("Proceed");
    up2_bt1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    String t1 = up2_tf1.getText();
    String t2 = up2_tf2.getText();
    String t3 = up2_tf3.getText();
    if(t1.equals("") || t2.equals("") || t3.equals("")){
    JOptionPane.showMessageDialog(jtemp,"빈칸이 있습니다. 채워주세요.");
    return;
    }
    if(!(t1.length() == 7)) {
    JOptionPane.showMessageDialog(jtemp,"앞 4자리는 입학 년도, 뒤 3자리는 임의 수를 입력하세요.");
    return;
    }
    up2_tf3.setText(up2_tf2.getText());
    if (!(t2.equals("f") || t2.equals("F") || t2.equals("s") || t2.equals("S") || t2.equals("p") || t2.equals("P"))) {
    JOptionPane.showMessageDialog(jtemp,"타입이 잘못되었습니다. 다시 입력해주세요.");
    return;
    }
    if (!service.register(new User(t1, t2, t3))) {
    JOptionPane.showMessageDialog(jtemp, "중복된 사람이 있습니다.");
    return;
    }
    JOptionPane.showMessageDialog(jtemp,t3 + new User("",t2,"").changeType(t2) + "님이 등록 되었습니다.");
    up2_tf1.setText("");
    up2_tf2.setText("");
    up2_tf3.setText("");

    }
    });

    jtemp.add(up2_lb1);
    jtemp.add(up2_tf1);

    jtemp.add(up2_lb2);
    jtemp.add(up2_tf2);

    jtemp.add(up2_lb3);
    jtemp.add(up2_tf3);

    jtemp.add(up2_lb4);
    jtemp.add(up2_bt1);
    up.remove(up2);

    up.add(jtemp);
    up.revalidate();
    }

    }

    public static void main(String[] args) {
    new UI();
    } // main 끝
    }
    =====================================

    IO.java

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;


    public class IO {
    private static final String fileName = "D:\\sql1.txt";

    public ArrayList<User> loadData(ArrayList<User> temp){
    try{
    //파일 여부 검사
    File f = new File(fileName);
    if(!f.exists()){
    return temp;
    }
    //파일 여부 검사 끝
    BufferedReader inputStream = new BufferedReader(new FileReader(fileName));
    String t="";
    while((t = inputStream.readLine()) != null){
    String[] at = t.split("\t");
    temp.add(new User(at[0], at[1], at[2]));
    }
    inputStream.close();
    } catch(IOException e){
    System.out.println("애러 출력");
    }
    return temp;
    }


    //등록, 삭제를 담당
    public void writeInTextFile(ArrayList<User> temp){
    try {
    BufferedWriter bf = new BufferedWriter(new FileWriter(fileName));
    for(int i=0; i<temp.size(); i++){
    User ttemp = temp.get(i);
    bf.write(ttemp.toString2()); // toString
    bf.newLine();
    }
    bf.close();
    } catch(IOException e){
    System.out.println("에러");
    }
    }
    }
    ================================================== ==================
    Service.java


    public class Service {
    DAO dao = new DAO();

    public User[] search(User temp, int c){
    if(c == 1){
    return dao.getEmp(); // 1 = 전체 사용자 조회
    }
    return dao.search(temp); // 다른 사용자 조회
    }

    public boolean register(User temp){
    return dao.register(temp);
    }

    public User delete(User temp){
    return dao.delete(temp);
    }

    }
    ================================================== ============
    User.java


    public class User {
    private String name, idNum, type;
    public User(){
    }
    public User(String idNum, String type, String name){
    this.idNum = idNum;
    this.name = name;
    this.type = type;
    }
    public String getIDNum() {
    return idNum;
    }
    public void setIDNum(String idNum) {
    this.idNum = idNum;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getType() {
    return type;
    }
    public void setType(String type) {
    this.type = type;
    }

    public String toString(){
    String t = idNum + "\t" + changeType(type) + "\t" + name;
    return t;
    }

    public String toString2(){
    String t = idNum + "\t" + type + "\t" + name;
    return t;
    }

    public String changeType(String t){
    switch(t){
    case "f":
    case "F":
    t = "직원";
    break;
    case "s":
    case "S":
    t = "학생";
    break;
    case "p":
    case "P":
    t = "교수";
    }
    return t;
    }

    public boolean equals(User temp){
    if(idNum.equals(temp.getIDNum()))
    return true;
    return false;
    } //equals 끝
    }
    ================================================== =========
    DAO.java

    import java.util.ArrayList;
    import java.util.Iterator;

    public class DAO {
    private ArrayList<User> emp = new ArrayList<User>();
    private boolean ck = false;
    private IO io = new IO();

    public DAO(){
    if(ck == false){ // 처음 실행할 때 자료 불러옴
    emp = io.loadData(emp);
    ck = true;
    }
    }

    public User[] getEmp(){ // 전체 사용자 조회
    User[] t = new User[emp.size()];
    return emp.toArray(t); // ArrayList emp를 User[] 배열에 반환
    }

    public User[] search(User temp){
    ArrayList<User> tempArrayList = new ArrayList<User>();
    Iterator<User> it = emp.iterator();
    while(it.hasNext()){
    User tempUser = it.next();
    if(temp.equals(tempUser)){
    tempArrayList.add(tempUser);
    }
    }
    User[] t = new User[tempArrayList.size()];
    return tempArrayList.toArray(t);
    }

    public boolean register(User temp){
    Iterator<User> it = emp.iterator();
    while(it.hasNext()){
    User t = it.next();
    if(temp.equals(t))
    return false;
    }
    emp.add(temp);
    io.writeInTextFile(emp); // emp를 writeInTextFile 반환형을 저장
    return true;
    }

    public User delete(User temp){
    Iterator<User> it = emp.iterator();
    while(it.hasNext()){
    User t = it.next();
    if(temp.equals(t)){
    emp.remove(t);
    io.writeInTextFile(emp);
    return t;
    }
    }
    return new User("0", "0", "0");
    }
    }

    ==============================================
    ==============================================


    It originally wasn't slow before applying GUI(All members in swings and awt)
    WHY IS IT TOO SLOW WHEN I PRESS "F11"?
    I think it is related to the Service class and the UI class.
    Last edited by Steven Park; 11-29-2013 at 11:52 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: My project was slowed down after applying GUI...

    When posting code please wrap it in [code] tags [/code].
    Large amounts of unformatted code is hard to follow.

    Also, since the problem lies with F11 then why not cut down the code to show just the relevant bits that are executed when F11 is pressed.
    And how did you time this?
    What did the non-GUI code look like that did whatever F11 does?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Applying Custom clors to cells using style
    By Praveen198226 in forum Apache POI
    Replies: 7
    Last Post: 09-26-2013, 05:22 PM
  2. Applying Sound to Java Game
    By VTechJava in forum New To Java
    Replies: 2
    Last Post: 04-26-2013, 06:11 PM
  3. Applying Aspectj in EJB 2.0 without using Spring
    By hgupta1204 in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 04-25-2012, 09:59 AM
  4. Applying Vista Buttons for Java JButton
    By viswanadh7 in forum AWT / Swing
    Replies: 4
    Last Post: 03-13-2009, 12:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •