有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

所以我用一个链表做了这个队列,一个作业对象进入队列。我只使用了这两个类文件就让程序工作了,但现在我正试图让它与GUI一起工作

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

我的问题在最后

注:为了切中要害,省略了最初的主要方法

这是我的工作目标进入队列,一般的东西在这里。。。省略了getter/setter:

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;
}

这是一个包含构造函数和方法的队列类。。。这些都很好用

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;
}

这是我的GUI:

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");
    }
 }

当我知道如何处理这个问题时,我会制作更多的actionListeners

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

这是我第一次尝试使用GUI,因此任何洞察都会很棒。谢谢:)


共 (1) 个答案

  1. # 1 楼答案

    你宣布:

    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();
    

    Comment
    我不确定main()是否是实例化PrintQueue的正确位置——可能是这样,但也可能是构造函数更好