java - 如何将输出队列放在 JTextArea 中?
问题描述
您好,我一直在尝试获取有关在 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
.
您需要考虑的其他一些注意事项是
- 文本区域
String
仅支持内容,因此您需要转换输出 - 将文本附加到文本区域不会自动插入新行,因此您需要这样做。
请参阅如何使用文本区域
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 queue\n");
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;
}
}
}
仅供参考:将来,您实际上需要提供尝试的演示。我差点走过这个问题
推荐阅读
- python - matplotlib 填充箱线图的边框颜色
- google-app-engine - 如何在 Google App Engine 中获取 26 字节的非 JPG 图像的宽度和高度?
- python - 尝试授权自己进入 sharepoint 站点时收到 keyerror
- reporting-services - Report builder 3.0 Using Reportitems!TexboxXX.Value 有时会创建多个框。为什么?
- python - 可重现的 Pytorch 结果和随机种子
- python-3.x - 如何在 python 中实现全局“oracle”?
- javascript - 在 redux-observable 中完成一些异步操作后,如何对 redux 操作进行排序?
- r - dataframe column containing lists extracted as columns of the same dataframe
- javascript - AngularJS 选择 | 从 JSON 中选择一个 id 后,显示该 id 的其余信息
- java - Openshift 部署 F8:无法访问集群以检测模式:sun.security.validator.ValidatorException