Java如何使JTextArea从不同的类可以访问并写入整数

时间:2017-12-02 23:44:57

标签: java swing jframe jpanel jtextarea

我想知道如何让我的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);

}}

1 个答案:

答案 0 :(得分: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字段的setter:

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后开始处理队列。