2017-12-02 326 views
0

我想知道如何使我的JTextArea全局,以及如何使用它作爲整數寫入結果?Java如何使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(); 

}} 

這是Queue類的GUI我試圖讓

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); 

}} 
+0

「*我可以讓我的JTextArea全球」* - 儘可能避免使用任何時間的全局變量。您應該小心地暴露UI元素,因爲它允許其他人以意想不到的方式修改它們(例如將其從容器中移除)。相反,關注「觀察者模式」的使用,它允許您註冊對某些狀態發生變化時可以生成通知的類感興趣 – MadProgrammer

回答

0

這個問題有很多可能的解決方案。這是我想接近它的方式:

創建一個名爲MessageOutput類deneme2內像這樣的接口:

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

而且在同一個班,你需要將MessageOutput的引用,以便您可以追加消息。讓我們把它初始化爲的情況下,一些默認實現無人設置後:

​​

所以你需要一個二傳手的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作爲一個局部變量,你必須讓主機的屬性:

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

最後,更新main()方法的主機傳遞到deneme2實例:

MainFrame frame = new MainFrame(); 
queue.setMessageOutput(frame); 

而且您應該在創建MainFrame後開始處理隊列。

+0

以及如何將jtextarea作爲MainFrame的屬性? –

+0

我已經完成了所有事情,唯一的問題是當我排隊。setMessageOutput(幀);這在主要方法,它給隊列錯誤 –

+0

你可以顯示錯誤信息? –

相關問題