java - 在另一个类中使用内部类actionListener

标签 java object user-interface queue actionlistener

所以我使用链表创建了这个队列,一个 Job 对象进入队列。我仅使用这两个类文件就可以使程序运行,但现在我正在尝试使其与 GUI 一起运行。

我希望能够使用队列和作业文件从 GUI 主方法运行我的程序。我创建的 GUI 有 8 个按钮和一个用于输出的 JTextArea。我希望能够通过单击按钮来执行每个功能(入队、显示队列长度等)。

我的查询在最后

PS。为了简洁起见,原来的 main 方法被省略了。

这是我要进入队列的 Job 对象,这里是一般内容...省略了 getters/setters:

public class Job 
{

String ID; //(A unique number to identify each print job)
String userID; //(The login of the user that sent the document to print)
String documentName; //(The name of the document being printed)
int fileSize; //(Size of the document being printed in Kb’s)

public Job(String p, String u, String e, int f) 
{ //local variables n,t,e only used in this method
    printID = p;
    userID = u;
    documentName = e;
            fileSize = f;
}

这是带有构造函数和方法的 Queue 类...据我所知,这些都可以工作。

public class Queue<Item> implements Iterable<Item> 
{
private int N;         // number of elements on queue
private Node first;    // beginning of queue
private Node last;     // end of queue



// helper linked list class
private class Node 
{
    private Item item;
    private Node next;
}

/**
 * Initialises an empty queue.
 */
public Queue() 
{
    first = null;
    last  = null;
    N = 0;
  //  assert check();
}

//is the list empty?
public boolean isEmpty() 
{
    return first == null;
}

//gives number of elements in the queue
public int length() 
{
    return N;     
}


 //looks at first item, if it is empty then return error. If it has something then return
 the   content of the element.
 public Item peek() 
 {
    if (isEmpty()) throw new NoSuchElementException("Nothing in queue");
    return first.item; 
 }

//add an item
 public void enqueue(Item item) 
{
    Node oldlast = last;  //move the current last to a placeholder
    last = new Node();  //create a new node inside of last
    last.item = item; //place the item i passed to the method call to the new last's item area
    last.next = null; //set the end of the queue
    if (isEmpty())   //checks the rest of the queue
        first = last;  //if empty creates a queue of 1 element length
    else           
        oldlast.next = last; //if not empty then previous last will point to this new one
    N++; //increment the number of elements being tracked.

}

 //remove an item
 public Item dequeue() 
 {
    if (isEmpty()) throw new NoSuchElementException("Nothing in queue");
    Item item = first.item;
    first = first.next;
    N--;
    if (isEmpty()) last = null;   // to avoid loitering

    return item;
}

这是我的图形用户界面:

public class GUI {   

  public static void main(String[] args) 
  {        
    Queue s = new Queue();

    JFrame frame = new JFrame("test");
    frame.setVisible(true);
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    JPanel panel = new JPanel(new GridBagLayout());
    frame.getContentPane().add(panel, BorderLayout.NORTH);

    GridBagConstraints c = new GridBagConstraints();

    JButton button1 = new JButton("Enqueue a print job");
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(10,10,10,10);
    panel.add(button1, c);
    button1.addActionListener(new Enqueue(){});

    JButton button2 = new JButton("Dequeue a print job");
    c.gridx = 0;
    c.gridy = 1;
    panel.add(button2, c);
    button2.addActionListener(new Dequeue(){});

    JButton button3 = new JButton("is the queue empty?");
    c.gridx = 1;
    c.gridy = 0;
    panel.add(button3, c);
    button3.addActionListener(new empty(){});

    JButton button4 = new JButton("Print first in queue");
    c.gridx = 1;
    c.gridy = 1;
    panel.add(button4, c);
    button4.addActionListener(new first(){});

    JButton button5 = new JButton("Length of queue");
    c.gridx = 0;
    c.gridy = 2;
    panel.add(button5, c);
    button5.addActionListener(new length(){});

    JButton button6 = new JButton("Print entire queue");
    c.gridx = 1;
    c.gridy = 2;
    panel.add(button6, c); 
    button6.addActionListener(new printAll() {});

    JTextArea jTextArea = new JTextArea();


    c.gridx = 4;
    c.gridy = 4;
    frame.add( jTextArea, BorderLayout.SOUTH);

这是有问题的内部类(在 GUI 文件中):我在这里做错了什么?我尝试过引用 's' 队列,但对于这个单独的类,队列不存在于主队列之外;这意味着我无法对其执行功能。

 static class length implements ActionListener
 {
 public void actionPerformed (ActionEvent e) 
    { 
         System.out.println("The length of the queue is: " + s.length() +"\n");
    }
 }

当我知道如何处理这个 Action 监听器时,我会制作更多的 Action 监听器。

诗。如果您知道如何从我在 GUI 文件的 main 方法中创建的 actionListener 写入 JTextArea,而不是使用 Sys out,那也会对我有帮助!

这是我第一次尝试使用 GUI,所以任何见解都会很棒。谢谢:)

最佳答案

您声明:

PrintQueue s = new PrintQueue();

main() 方法内 - 这意味着它只能在 main() 内部(方法作用域)使用。 如果您想在方法之外使用它,则必须将其声明为类成员并通过“get()”方法公开它。

例如:

public class GUI { 

  static PrintQueue s = null;  

  public static void main(String[] args) {        
    s = new PrintQueue();
    ...
  }

  public PrintQueue getS() {return s;}

然后您就可以使用它了:

public void actionPerformed (ActionEvent e) { 
     System.out.println("The length of the queue is: " + s.length() +"\n"); //from the same class
}

或者从类外使用:

GUI gui = new Gui();
PrintQueue s = gui.getS();

评论:
我不确定 main() 是否是实例化 PrintQueue 的正确位置 - 可能是所以,但构造函数也可能是一个更好的主意。

关于java - 在另一个类中使用内部类actionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27093249/

相关文章:

android - 不正确的用户界面 - android

java - Hadoop中的“could only be replicated to 0 nodes, instead of 1”

java - 忽略 xmlunit 中的空标签

java - 在java中处理 boolean 结果集

javascript - jQuery - 如何将 DOM 元素和 Javascript 对象链接在一起?

java - 在 Netbeans 中重新生成所有 Java Swing 页面的快速方法?

java - AKKA 在一段时间后停止记录堆栈跟踪

javascript - 如何在 JavaScript 中获取单词或索引后的子字符串?

java - 为什么使用 ImageIcon 而不是 Image?

javascript - 是否有一种规范的方式来为缓慢加载的网页呈现 "loading"弹出窗口?