简体   繁体   English

Java如何使JTextArea Accesible来自不同的类并以整数形式编写

[英]Java how to make JTextArea Accesible from different class and write in integer

I would like to know how can I make my JTextArea global and how can I use that to write in it with the result I get as an integer? 我想知道如何使我的JTextArea全局化,以及如何使用它以整数形式得到的结果写入其中?

I am trying to do program for queues with linked link implementation but I don't actually use the LinkedList class. 我正在尝试为具有链接链接实现的队列编写程序,但实际上不使用LinkedList类。 I have two different classes in my project. 我的项目中有两个不同的类。 There is one class which is deneme2 . 一类是deneme2 In that class I have the queue methods. 在该类中,我有队列方法。 In the second class I have the JFrame , so I would like to get my enqueue and dequeue result in JTextArea . 在第二个类中,我有JFrame ,所以我想在JTextArea获得入队和出队结果。 So far I could only use println but can't manage to get it into the JTextArea . 到目前为止,我只能使用println但无法设法将其放入JTextArea

This is my deneme4 class 这是我的deneme4

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

}}

This is the Queue class for the GUI I tried to make 这是我尝试制作的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;
}}

and this is my QueueFrame which i want when the button clicked it outputs the value to txt1 but cant seem to do it 这是我想要的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);

}}

There are many possible solutions to this problem. 有许多可能的解决方案来解决此问题。 This is the way that I would approach it: 这就是我要处理的方式:

Create an interface called MessageOutput inside the class deneme2 like this: 在类deneme2内创建一个名为MessageOutput的接口,如下所示:

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

And in the same class, you need a reference to the MessageOutput so you can append message to it. 在同一个类中,您需要对MessageOutput的引用,以便可以将消息追加到它。 And let's initialize it to some default implementation in case no one set it later: 让我们将其初始化为一些默认的实现,以防以后没有人设置它:

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

And so you need a setter to the msgOutput field: 因此,您需要对msgOutput字段进行设置:

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

And so you can now change all the println code to use MessageOutput's appendMessage() method: 因此,您现在可以更改所有println代码,以使用MessageOutput的appendMessage()方法:

public int dequeue() {
    . . . 

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

Then let MainFrame implements deneme2.MessageOutput. 然后让MainFrame实现deneme2.MessageOutput。 Note that you can no longer use the JTextArea as a local variable, you must make it an attribute of MainFrame: 请注意,您不能再将JTextArea用作局部变量,而必须使其成为MainFrame的属性:

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

And finally, update the main() method to pass the MainFrame to the deneme2 instance: 最后,更新main()方法以将MainFrame传递给deneme2实例:

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

And you should start processing the queue after the MainFrame is created. 创建MainFrame之后,您应该开始处理队列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM