在Java中交换JButton元素

在Java中交换JButton元素,java,swing,awt,jbutton,Java,Swing,Awt,Jbutton,更新了我的代码我意识到我应该在JButton上安装actionListener(因为他们实际上是做所有工作的人)。我的逻辑现在在排序部分不正确了。我能够让jButtons交换值,但是我的输出看起来是正确的,但是我试图找出我把排序机制搞砸的地方。通过使用单选按钮选择一个起始点,并为JButton选择一个不同的交换位置,该程序“应该”正常工作。(例如,收音机3然后jbutton 8会将第三个元素移动到第八个位置)我知道我已经很接近了,但我已经陷入了一种常规,看不见了。有什么建议吗?(免责声明这是一项

更新了我的代码我意识到我应该在JButton上安装actionListener(因为他们实际上是做所有工作的人)。我的逻辑现在在排序部分不正确了。我能够让jButtons交换值,但是我的输出看起来是正确的,但是我试图找出我把排序机制搞砸的地方。通过使用单选按钮选择一个起始点,并为JButton选择一个不同的交换位置,该程序“应该”正常工作。(例如,收音机3然后jbutton 8会将第三个元素移动到第八个位置)我知道我已经很接近了,但我已经陷入了一种常规,看不见了。有什么建议吗?(免责声明这是一项奖励积分编程2作业)

新代码

import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类GUI交换机
{
公共静态void main(字符串[]args)
{
GUIswitchClass myWindow=新的GUIswitchClass();
myWindow.setSize(300350);
设置位置(100100);
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWindow.setVisible(true);
}
}
//====================================================================================================================
类GUIswitchClass扩展JFrame//字段
{
JPanel leftPanel=新的JPanel();
JPanel rightPanel=新的JPanel();
JPanel allPanel=新的JPanel();
JRadioButton rdoBut[]=新的JRadioButton[10];
JButton teamBut[]=新JButton[10];
ButtonGroup rdBtGp=新建ButtonGroup();
字符串rdoButNum[]={“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”};
字符串teamName[]={“A”、“BB”、“CCC”、“DDDD”、“EEEEE”、“FFFFFF”,
“GGGGGGG”、“hhhhhhhhhhhhhhhhhhhiiiiiii”、“JJJJJJJJJJ”};
//串从;
//串到;
int from;
int到;
字符串fromTo;/??
int i;
public GUIswitchClass()//构造函数
{
超级(“项目3”);
fromTo=收进();
//to=团队,但[团队名称[i]];
}
私有字符串setButs()
{
int i;
setLayout(新的GridLayout(10,1));
设置布局(新的网格布局(10,1));

对于(i=0;i我不会交换JButton本身,而是给出JButton的抽象操作,并简单地交换这些操作,因为这样做非常容易。或者您可以交换JButton的文本和actionCommand(如果您要这样做,您应该同时做),但这将涉及并行交换,对我来说没有那么干净。

我将用
JCheckBox
s替换
JRadioButton
s,主要是因为您可以同时选择两个复选框,允许以这种方式选择复选框比允许单选按钮更有意义

我会为所有复选框创建一个单一的操作侦听器,这样每次单击都会通过它

在action listener中,我将使用
java.util.List
来跟踪当前选择的选项(您可以遍历数组,但我发现这更简单)

当列表包含两项时,我会

  • 取消选中这两个复选框并清空列表
  • 使用获取每个复选框的当前索引(或Z顺序)
  • 从左面板中查找相应的按钮(这是通过使用上一步中检索到的索引值实现的),使用
  • 交换组件。这是通过使用
比如说

int index0 = leftPanel.getComponentZOrder(cb0);
int index1 = leftPanel.getComponentZOrder(cb1);

Component comp0 = rightPanel.getComponent(index0);
Component comp1 = rightPanel.getComponent(index1);

rightPanel.setComponentZOrder(comp0, index1);
rightPanel.setComponentZOrder(comp1, index0);

您还需要在框架上
revalidate()
来更新布局;)

此外,代码是否可以编译?如前所述,您的moveTeam方法不会。您必须使用单选按钮吗?不,我的代码不可以编译。是的,可以,但是moveTeam方法不起作用(单选按钮是奖金工作的一个组成部分。这就是我试图做的,就是交换按钮中的文本。我有一个排序方法,打印出来时可以工作,用户键入from,to,然后交换名称。我正试图对GUI按钮做同样的事,让自己陷入其中。我很感激他所做的一切。)lp!!好的,我意识到我在单选按钮上有eventHandler,它们应该在JButtons上以使它们移动。我仍然无法使单选[I]的值指向相应的JButton面。例如,如果我单击单选3和按钮5,那么第三个按钮应该移动到5点(另一个文本向上移动)。
import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;

import javax.swing.*;

public class GUIswitch 
{
    public static void main(String[] args) 
    {


        GUIswitchClass myWindow = new GUIswitchClass();
        myWindow.setSize(300,350);
        myWindow.setLocation(100,100);
        myWindow.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        myWindow.setVisible( true );



    }
}
//====================================================================================================================
class GUIswitchClass extends JFrame                                         //fields
{
        JPanel          leftPanel       =   new JPanel();
        JPanel          rightPanel      =   new JPanel();                       
        JPanel          allPanel        =   new JPanel();
        JRadioButton    rdoBut[]        =   new JRadioButton[10];
        JButton         teamBut[]       =   new JButton[10];
        ButtonGroup     rdBtGp          =   new ButtonGroup();
        String          rdoButNum[]     =   {"1","2","3","4","5","6","7","8","9","10"};
        String          teamName[]      =   {"A","BB","CCC","DDDD","EEEEE","FFFFFF",
                                            "GGGGGGG","HHHHHHHH","IIIIIIIII","JJJJJJJJJJ"};
        String          from;
        String          to;
        String fromTo;
        int i;

    public GUIswitchClass()                                                 //constructor
    {
        super("Swap Elements");


        fromTo  = setButs();
        //to        = teamBut[teamName[i]];

    }
    private String setButs()
    {
        int i;
        leftPanel.setLayout(new GridLayout(10,1));
        rightPanel.setLayout(new GridLayout(10,1));
        for(i=0;i<10;i++)
        {
            rdoBut[i] = new JRadioButton(rdoButNum[i]);
            rdoBut[i].setFont(new Font("Courier", Font.PLAIN, 30) );
            rdoBut[i].addActionListener(new checkHdlr() );
            rdBtGp.add(rdoBut[i]);
            leftPanel.add(rdoBut[i]);   
        }

        for(i=0;i<10;i++)
        {
            teamBut [i] =   new JButton(teamName[i]);
            teamBut[i].setFont(new Font("Courier", Font.PLAIN, 30) );
            teamBut[i].setHorizontalAlignment(SwingConstants.LEFT);
            rightPanel.add(teamBut[i]);
        }

        allPanel.setLayout(new BorderLayout());
            allPanel.add(leftPanel, BorderLayout.WEST);
            allPanel.add(rightPanel, BorderLayout.CENTER);
        add(allPanel);

        return fromTo;



    }
    @SuppressWarnings("unused")
    private class checkHdlr implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            String userNum;
            userNum = event.getActionCommand();

        }
    }   

//--------------------------------------------------------------------------
     public static void moveTeam(String teamName[],String fromTo)
      {
        int             from;
        int             to;

        if(from>to)                           //determine direction to move
          moveUp(teamName,from,to);
        else
          moveDown(teamName,from,to);
      }

 //---------------------------------------------------------------------move up
  public static void moveUp(String teamList[], int from, int to)
  {
    String clipboard;
    int    row;

    clipboard = teamList[from];           //copy

    for(row=from;row>to;row--)            //shift others down
      teamList[row] = teamList[row-1];

    teamList[row] = clipboard;            //paste
  }
  //-------------------------------------------------------------------move down
  public static void moveDown(String teamList[], int from, int to)
  {
    String clipboard;
    int    row;

    clipboard = teamList[from];           //copy

    for(row=from;row<to;row++)            //shift others up
      teamList[row] = teamList[row+1];

    teamList[row] = clipboard;            //paste
  }
  //---------------------------------------------------------------------display
  public static void disp(String teamList[])
  {
    int r;
    for(r=0;r<10;r++)
      System.out.printf("%2d %s\n",r+1,teamList[r]);
  }
}
int index0 = leftPanel.getComponentZOrder(cb0);
int index1 = leftPanel.getComponentZOrder(cb1);

Component comp0 = rightPanel.getComponent(index0);
Component comp1 = rightPanel.getComponent(index1);

rightPanel.setComponentZOrder(comp0, index1);
rightPanel.setComponentZOrder(comp1, index0);