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