[英]Java how to make JTextArea Accesible from different class and write in integer
我想知道如何使我的JTextArea
全局化,以及如何使用它以整數形式得到的結果寫入其中?
我正在嘗試為具有鏈接鏈接實現的隊列編寫程序,但實際上不使用LinkedList
類。 我的項目中有兩個不同的類。 一類是deneme2
。 在該類中,我有隊列方法。 在第二個類中,我有JFrame
,所以我想在JTextArea
獲得入隊和出隊結果。 到目前為止,我只能使用println
但無法設法將其放入JTextArea
。
這是我的deneme4
課
public class Deneme4 extends JFrame {
public static void main(String a[]) throws FileNotFoundException {
SecondFrame frame = new SecondFrame();
}}
這是我嘗試制作的GUI的Queue
類
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;
}}
這是我想要的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);
}}
有許多可能的解決方案來解決此問題。 這就是我要處理的方式:
在類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;
}
然后讓MainFrame實現deneme2.MessageOutput。 請注意,您不能再將JTextArea用作局部變量,而必須使其成為MainFrame的屬性:
public class MainFrame implements deneme2.MessageOutput {
@override
public void appendMessage(String message) {
txt2.append(message);
}
}
最后,更新main()方法以將MainFrame傳遞給deneme2實例:
MainFrame frame = new MainFrame();
queue.setMessageOutput(frame);
創建MainFrame之后,您應該開始處理隊列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.