これが私の問題です。20個のラジオとjbuttonのGUIプリントアウトを作成し、要素を入れ替えようとしています。リンクリストノードをスワップすることが確実になるまで、サイドでメソッドを構築しましたが、今では、JButtonがノードから文字列値を取得して、ボタンの面にデータを入力することができません。(現在、20個のnullが表示されています)。何かアイデア、ポインタ、どこが間違っているのですか?
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;
}
}