我如何把我的输出队列在JTextArea?

  • 本文关键字:JTextArea 队列 输出 java
  • 更新时间 :
  • 英文 :


你好,我一直在试图让我的代码在JTextArea上获得我的队列输出。已经4个小时了,哈哈。我是一年级的学生,所以请原谅我。同样,我们的目标是获得一个队列,该队列将输出到JTextArea以及包含Enqueue、Dequeue和Exit的菜单栏。我已经在那里创建了一堆函数,以防我必须在即将到来的考试中使用。

类队列:

import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class Queue {
private int num[];
private int front, rear, capacity;
public int hold;
public Queue() {
capacity=5;
num = new int[capacity];
front=rear=1;
}
public Queue(int capacity) {
this.capacity = capacity;
num = new int[capacity];
front=rear=-1;
}
public boolean isEmpty() {return rear==-1;}
public boolean isFull() {return (rear == capacity -1);}

private void errorMessage(String msg) {
JOptionPane.showMessageDialog(null, msg, "Full", JOptionPane.ERROR_MESSAGE);
}
public void enqueue(int data) {
if(isFull()) {
errorMessage("Queue is Full!");
}else {
num[++rear] = data;
front=0;
}
}
public int dequeue() {
int val = 0;
if (isEmpty()) {
errorMessage("Queue is Empty");
front=-1;           
}else {
val=num[front];

for(int i=0; i<rear; i++) {
num[i]=num[i+1];
}
rear--;
}
return val;
}
public String display() {
String hold="";
if(!isEmpty()) {
for(int i = front; i<=rear; i++) {
hold+=num[i]+" ";
}
}else {
hold="Queue is empty";
}
return hold;
}
public int peek() {
if(isEmpty()) {
System.err.println("Queue is empty");
return -1;
}else {
return num[front];
}
}
public int last() {
if(isEmpty()) {
errorMessage("Queue is empty");
return -1;
}else {
return num[rear];
}

Object menu[]= {"Enqueue", "Dequeue", "Close"};
select = JOptionPane.showInputDialog(null, new JTextArea(hold), 1, null, menu, menu[0].toString());
}
public int frontValue() {return num[front];}
public int rearValue() {return num[rear];}
public int getCurrentSize() { return capacity-(capacity-(rear+1));}
public int getCapacity() {return capacity;} 
}

目前我的输出是这样的:

2
0
0 0 14 24 46 
0
0 14 24 46 
14
14 24 46 

这是我ADTQueue:

public class ADTQueue {
public static void main(String[] args) {
Queue q =new Queue();
q.peek();
System.out.println(q.getCurrentSize());
System.out.println(q.last());
q.enqueue(14);
q.enqueue(24);
q.enqueue(46);
System.out.println(q.display());
q.enqueue(16);
System.out.println(q.dequeue());
System.out.println(q.display());
q.dequeue();
System.out.println(q.peek());
System.out.println(q.display());
}
}

我修改了你的代码如果发生故障,Queue应该以某种有意义的方式报告该故障,在本例中,我使用了自定义的Exception

你需要考虑的其他一些因素是

  1. 文本区只支持String内容,所以你需要转换你的输出
  2. 添加文本到文本区域不会自动插入新行,所以你需要这样做。

参见如何使用文本区域

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public final class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTextArea textArea = new JTextArea(10, 20);
public TestPane() {
setLayout(new BorderLayout());
add(new JScrollPane(textArea));
Queue q = new Queue();
q.peek();
textArea.append(Integer.toString(q.getCurrentSize()) + "n");
textArea.append(Integer.toString(lastFrom(q)) + "n");
enqueue(q, 14);
enqueue(q, 24);
enqueue(q, 46);
textArea.append(q.display() + "n");
enqueue(q, 16);            
textArea.append(Integer.toString(dequeue(q)) + "n");
textArea.append(q.display());
dequeue(q);
textArea.append(Integer.toString(q.peek()) + "n");
textArea.append(q.display() + "n");
}
public int lastFrom(Queue queue) {
try {
return queue.last();
} catch (Queue.QueueException ex) {
textArea.append("ERR: Failed get last value from queuen");
textArea.append("ERR: Queue is " + ex.getState() + "n");
}
return -1;
}
public void enqueue(Queue queue, int value) {
try {
queue.enqueue(value);
} catch (Queue.QueueException ex) {
textArea.append("ERR: Failed to enqueue " + value + "n");
textArea.append("ERR: Queue is " + ex.getState() + "n");
}
}
public int dequeue(Queue queue) {
try {
return queue.dequeue();
} catch (Queue.QueueException ex) {
textArea.append("ERR: Failed to dequeue value");
textArea.append("ERR: Queue is " + ex.getState() + "n");
}
return -1;
}
}
public class Queue {
public class QueueException extends Exception {
public enum State {
FULL, EMPTY;
}
private State state;
public QueueException(State state) {
this.state = state;
}
public State getState() {
return state;
}
}
private int num[];
private int front, rear, capacity;
public int hold;
public Queue() {
capacity = 5;
num = new int[capacity];
front = rear = 1;
}
public Queue(int capacity) {
this.capacity = capacity;
num = new int[capacity];
front = rear = -1;
}
public boolean isEmpty() {
return rear == -1;
}
public boolean isFull() {
return (rear == capacity - 1);
}
public void enqueue(int data) throws QueueException {
if (isFull()) {
throw new QueueException(QueueException.State.FULL);
}
num[++rear] = data;
front = 0;
}
public int dequeue() throws QueueException {
int val = 0;
if (isEmpty()) {
front = -1;
throw new QueueException(QueueException.State.EMPTY);
} else {
val = num[front];
for (int i = 0; i < rear; i++) {
num[i] = num[i + 1];
}
rear--;
}
return val;
}
public String display() {
String hold = "";
if (!isEmpty()) {
for (int i = front; i <= rear; i++) {
hold += num[i] + " ";
}
} else {
hold = "Queue is empty";
}
return hold;
}
public int peek() {
if (isEmpty()) {
System.err.println("Queue is empty");
return -1;
} else {
return num[front];
}
}
public int last() throws QueueException {
if (isEmpty()) {
throw new QueueException(QueueException.State.EMPTY);
} else {
return num[rear];
}
}
public int frontValue() {
return num[front];
}
public int rearValue() {
return num[rear];
}
public int getCurrentSize() {
return capacity - (capacity - (rear + 1));
}
public int getCapacity() {
return capacity;
}
}
}

供参考:在将来,您实际上需要提供一个尝试的演示。我差点就通过了这个问题

最新更新