2017-12-02 16 views
0

어떻게하면 JTextArea 전역을 만들 수 있으며 어떻게 정수로 작성하여 그 결과를 쓸 수 있습니까?Java 다른 클래스에서 JTextArea Accesible을 만들고 정수로 작성하는 방법

링크 된 링크 구현을 사용하여 대기열에 대한 프로그램을 작성하려고하지만 실제로는 LinkedList 클래스를 사용하지 않습니다. 내 프로젝트에는 두 개의 다른 클래스가 있습니다. 하나의 클래스는 deneme2입니다. 그 클래스에는 큐 메서드가 있습니다. 두 번째 클래스에서 나는 JFrame을 가지고 있으므로 에서 대기열에 넣기와 빼내기 결과를 얻고 싶습니다. 지금까지 나는 println 만 사용할 수 있었지만 JTextArea에 들어가서는 안되었습니다.

이이 내가

public class Queue { 

public static interface MessageOutput { 

    void appendMessage(String message); 

    void appendHead(String message); 
} 

private MessageOutput msgOutput = new MessageOutput() { 
    @Override 
    public void appendMessage(String message) { 
     System.out.println(message); 
    } 

    @Override 
    public void appendHead(String head) { 
     System.out.println(head); 
    } 
}; 

public void setMessageOutput(MessageOutput value) { 
    msgOutput = value; 
} 

public void setHeadOutput(MessageOutput value) { 
    msgOutput = value; 
} 

private Node front, rear; 
private int currentSize; 

private class Node { 

    int data; 
    Node next; 
} 

public Queue() { 
    front = null; 
    rear = null; 
    currentSize = 0; 
} 

public boolean isEmpty() { 
    if (currentSize == 0) { 
     msgOutput.appendMessage("Que is Empty\n"); 
    } 
    return currentSize == 0; 
} 

public int dequeue() { 
    int data = front.data; 
    front = front.next; 
    if (isEmpty()) { 
     rear = null; 
    } 
    currentSize--; 
    msgOutput.appendMessage(data + " removed from the queue\n"); 

    return data; 
} 

public int enqueue(int data) throws FileNotFoundException { 
    Node oldRear = rear; 
    rear = new Node(); 
    rear.data = data; 
    rear.next = null; 
    if (isEmpty()) { 
     front = rear; 
    } else { 
     oldRear.next = rear; 
    } 
    currentSize++; 
    msgOutput.appendMessage(data + " added to the queue\n"); 
    return data; 
} 

public int queueSize() { 
    msgOutput.appendMessage("Size of the Que is" + currentSize + "\n"); 
    return currentSize; 
} 

public int getHead() { 
    int data = front.data; 
    msgOutput.appendHead("Head of the Que is " + data + "\n"); 
    return data; 
}} 

를 만들기 위해 시도 GUI에 대한 Queue 클래스 내 deneme4 클래스

public class Deneme4 extends JFrame { 
public static void main(String a[]) throws FileNotFoundException { 
    SecondFrame frame = new SecondFrame(); 

}} 

이며,이 버튼이는 출력 클릭하면 내가 원하는 내 QueueFrame입니다 값을 txt1로 설정했지만 할 수 없다.

public class QueueFrame extends JFrame implements Queue.MessageOutput { 

private JTextArea txt1; 
private JTextArea txt2; 
private JTextArea txt3; 
private JButton b1; 
private JButton b2; 
private Queue queue = new Queue(); 

public static interface MessageOutput { 

    void appendMessage(String message); 

    void appendHead(String message); 
} 

private MessageOutput msgOutput = new MessageOutput() { 
    @Override 
    public void appendMessage(String message) { 
     System.out.println(message); 
    } 

    @Override 
    public void appendHead(String head) { 
     System.out.println(head); 
    } 
}; 

public void setMessageOutput(MessageOutput value) { 
    msgOutput = value; 
} 

public void setHeadOutput(MessageOutput value) { 
    msgOutput = value; 
} 

@Override 
public void appendHead(String head) { 
    txt2.append(head); 
} 

public QueueFrame() throws FileNotFoundException { 

    JFrame frame = new JFrame(); 
    b1 = new JButton("Load up the Que"); 
    b1.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent ae) { 
      try { 
       Scanner s = new Scanner(new File("list.txt")); 
       while (s.hasNext()) { 
        queue.setMessageOutput((Queue.MessageOutput) queue. 
        queue.enqueue(s.nextInt()); 
       } 
       s.close(); 
       queue.queueSize(); 
       queue.getHead(); 
      } catch (FileNotFoundException ex) { 
       Logger.getLogger(QueueFrame.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
    }); 
    b2 = new JButton("Head of the Que"); 
    b2.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent ae) { 
      queue.getHead(); 
     } 
    }); 

    txt1 = new JTextArea(); 
    txt2 = new JTextArea(); 
    txt3 = new JTextArea(); 

    txt1.setEditable(false); 
    txt2.setEditable(false); 
    txt3.setEditable(true); 

    b1.setBounds(50, 100, 180, 100); 
    b2.setBounds(50, 300, 180, 100); 
    txt1.setBounds(600, 100, 200, 600); 
    txt2.setBounds(300, 300, 180, 100); 
    txt3.setBounds(300, 100, 180, 100); 
    frame.add(b1); 
    frame.add(b2); 
    frame.add(txt1); 
    frame.add(txt2); 
    frame.add(txt3); 
    frame.setLayout(null); 
    frame.setSize(1000, 1500); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

}} 
+0

"* joh는 내 JTextArea를 전역으로 만들 수 있습니다"* - 가능한 한 전역 변수를 사용하지 않아야합니다. UI 요소는 예기치 않은 방법으로 수정 (컨테이너에서 제거하는 등) 할 수 있으므로 항상주의해야합니다. 대신, "observer patterns"의 사용에 초점을 맞 춥니 다. 클래스에 관심을 등록 할 수 있습니다.이 클래스는 어떤 상태가 변경 될 때 알림을 생성 할 수 있습니다. – MadProgrammer

답변

0

이 문제에 대한 여러 가지 가능한 해결책이 있습니다. 이것은 내가 그것을 접근하는 것이 방법입니다 :

같은 클래스 deneme2 내부 MessageOutput라는 인터페이스를 만들기 :

public class deneme2 { 
    public static interface MessageOutput { 
     void appendMessage(String message); 
    } 
} 

그리고 당신은 추가 할 수 있도록 같은 클래스에서, 당신은 MessageOutput에 대한 참조를 필요

메시지.

private MessageOutput msgOutput = new MessageOutput() { 
    @override 
    public void appendMessage(String message) { 
     System.out.println(message); 
    } 
}; 

을 그리고 당신이 msgOutput 필드에 세터가 필요합니다 :

public void setMessageOutput(MessageOutput value) { 
    msgOutput = value; 
} 

을 그리고 당신이 지금 모든 println 메소드를 변경할 수 있습니다 그리고 아무도 나중에 설정되어 있지의이 경우 몇 가지 기본 구현을 초기화 할 수 코드는 MessageOutput의 appendMessage() 메서드를 사용합니다 :

public int dequeue() { 
    . . . 

    msgOutput.appendMessage(data + " removed from the queue"); 
    return data; 
} 

그런 다음 메인 프레임 deneme2.MessageOutput 구현하자.

public class MainFrame implements deneme2.MessageOutput { 
    @override 
    public void appendMessage(String message) { 
     txt2.append(message); 
    } 
} 

그리고 마지막으로는의 deneme2 인스턴스에 메인 프레임을 전달하는 main() 메소드를 업데이트 :

더 이상 로컬 변수로 JTextArea에 사용하지 수 있습니다, 당신은 메인 프레임의 속성해야합니다
MainFrame frame = new MainFrame(); 
queue.setMessageOutput(frame); 

MainFrame을 만든 후에 큐 처리를 시작해야합니다.

+0

또한 jtextarea를 MainFrame의 속성으로 만들 수 있습니까? –

+0

나는 모든 것을 해냈다. 내가 가지고있는 유일한 문제는 대기열에있을 때이다.setMessageOutput (frame); 이 메인 메서드에서 큐에 오류가 발생했습니다 –

+0

오류 메시지를 표시 할 수 있습니까? –