这就是我的问题,我正在创建一个包含 20 个单选按钮和 jbutton 的 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;
}
}