尝试从链接列表值设置JButton字符串字段

时间:2012-10-10 17:33:34

标签: java swing linked-list

这就是我的问题,我正在创建一个包含20个无线电和jbuttons的GUI打印输出,并尝试交换元素。我建立了我的方法,直到我确定我的链接列表节点交换,他们这样做,但现在我不能让JButton从我的节点抓取字符串值来填充按钮面。 (它现在显示20个空值)。任何想法,指针,我哪里出错了?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class ProbGuiDrv
{

    public static void main(String[] args) throws IOException
    {
        ProbdrvGuiWindow myWindow = new ProbdrvGuiWindow();
        myWindow.setSize(550,550);
        myWindow.setLocation(300,200);
        myWindow.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        myWindow.setVisible(true);
    }

}
//===================== GUI Window Creation Classes ============================
@SuppressWarnings("serial")
class ProbdrvGuiWindow extends JFrame
{
        ProbNode        head;
        ProbNode        teamList;
        JRadioButton    teamRBtn[]  =   new JRadioButton[20];
        JButton         teamBtn[]   =   new JButton[20];
        static String   teamNames[] = new String[20];

      public ProbdrvGuiWindow() throws IOException
      {
          super("Switching Elements");
            int i;
            MoveHandler moveHdlr = new MoveHandler();
            ButtonGroup teamGrp  = new ButtonGroup();
            JPanel      pnlWest  = new JPanel();
            JPanel      pnlEast  = new JPanel();
            pnlWest.setLayout(new GridLayout(20,1));
            pnlEast.setLayout(new GridLayout(20,1));

            for(i=0;i<20;i++)
              {
                teamRBtn[i]  = new JRadioButton(""+(i+1));
                teamRBtn[i].setFont(new Font("Courier", Font.PLAIN, 15));
                pnlWest.add(teamRBtn[i]);
                teamGrp.add(teamRBtn[i]);

                teamBtn[i] = new JButton(); //can't figure out how to set the text from my Node.name value//
                teamBtn[i].setFont(new Font("Courier", Font.PLAIN, 15));
                teamBtn[i].setHorizontalAlignment(SwingConstants.LEFT);
                teamBtn[i].addActionListener(moveHdlr);
                pnlEast.add(teamBtn[i]);
              }

            setLayout(new BorderLayout());
            add(pnlWest,BorderLayout.WEST);
            add(pnlEast,BorderLayout.CENTER);
      }
      //--------------------------------------------------------------------------------------
      private class MoveHandler implements ActionListener
      {
        public void actionPerformed(ActionEvent event)
        {
          int    i;
          int    from = 0;
          int    to;
          String nameAtToSlot;

          nameAtToSlot = event.getActionCommand();  // to button

          i = 0;
          while(!nameAtToSlot.equals(teamBtn[i].getText()))
            i++;

          to = i;

          for(i=0;i<20;i++)
            if(teamRBtn[i].isSelected())  //from radiobutton
              from = i;

          moveNode(from, to);

          teamRBtn[to].setSelected(true);
        }
      }
      //----------------------------------------------------------------
      public void addNode(String n)
        {
            ProbNode newNode = new ProbNode(n, head);
            head = newNode;
        }
      //----------------------------------------------------------------
      public ProbNode getNodeAt(int pos)
        {
            ProbNode node = head;
            for (int i = 0; i < pos; i++)
            {
                if (node.next == null)
                    break;
                node = node.next;
            }
            return node;
        }
    //----------------------------------------------------------------
        public ProbNode removeNodeAt(int pos)
        {
            ProbNode node = head;
            ProbNode prev = head;
            for (int i = 0; i < pos; i++)
            {
                if (node.next == null)
                    break;
                prev = node;
                node = node.next;
            }
            if (head == node)
            {
                head = null;
            }
            else
            {
                if (node != null)
                {
                    prev.next = node.next;
                    node.next = null;
                }
            }
            return node;
        }
        //--move a node from one position to another -------------
        public void moveNode(int from, int to)
        {
            ProbNode node = removeNodeAt(from-1); //
            insert(to-1, node);
        }
        //--------------------------------------------------------
        public void insert(int pos, ProbNode node)
        {
            if (pos == 0)
            {
                node.next = head;
                head = node;
            }
            else
            {
                ProbNode prev = getNodeAt(pos - 1);
                node.next = prev.next;
                prev.next = node;
            }
        }
      //-----------------------------------------------------------
      public static ProbMgr readList() throws IOException 
        {
              ProbMgr teamList    = new ProbMgr();
            FileReader inFile   = new FileReader("teamNames.txt");
            BufferedReader data = new BufferedReader(inFile);
            int i;

            String name;
            i = 1;
            while( (name = data.readLine()) != null )
            {
                teamList.addNode(name);
                //for(i=0;i<20;i++)
                //  teamNames[i] = teamList.getName();
                i++;
            }
            return teamList;
        }
}//end outer class

ProbMgr构造函数:

 public class ProbMgr
{
    public ProbNode head;

    public ProbMgr()
    {
        head = null;
    }
}

我的ProbNode:

public class ProbNode
{
     public String          name;
     public ProbNode        next;

        public ProbNode(String n, ProbNode ptr)//constructor
        {
            name = n;
            next = ptr;
        }
        public String getName()
        {
            return name;
        }
}

0 个答案:

没有答案