在Java中交换JButton元素

时间:2012-09-25 02:14:41

标签: java swing awt jbutton

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

新代码

import java.awt.*;
import java.awt.event.*;

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;
        int from;
        int to;
        String fromTo;  //??
        int i;

    public GUIswitchClass()                                                 //constructor
    {
        super("Proj 3");


        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) );
            rdBtGp.add(rdoBut[i]);
            leftPanel.add(rdoBut[i]);   
        }

        /*for(i=0;i<10;i++)
            if(rdoBut[i].isSelected() )
            {
                from = Integer.parseInt(rdoBut[i].getText() );
            }
        */
        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);
            teamBut[i].addActionListener(new checkHdlr() );
            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();
            for(i=0;i<10;i++)
                if(rdoBut[i].isSelected() )
                {
                    from = Integer.parseInt(rdoBut[i].getText() );
                }

            for(i=0;i<10;i++)
            {
                if(event.getSource() == teamBut[i])
                {
                    //teamBut[i].
                    teamBut[i].setText(teamName[i]);
                    //to = (int)(teamBut[i]);
                }
            }
            rightPanel.revalidate();                //refresh the panels
            moveTeam(teamName, from, to);
        }
    }   

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

        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
  }

}

旧代码

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

2 个答案:

答案 0 :(得分:4)

我不会交换JButton本身,而是提供JButton的AbstractActions并简单地交换这些动作,因为这是不可思议的更容易做到的。或者你可以交换JButton的文本和actionCommand(如果你要走这条路线,你应该做两件事),但这会涉及做并行交换,并且对我来说感觉不干净。

答案 1 :(得分:3)

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

我会为所有复选框创建一个动作侦听器,因此每次单击都会通过它。

在动作监听器中,我会使用java.util.List来跟踪当前选择的选项(你可以走数组,但我发现这更简单)。

当列表包含两个项目时,我会......

有点像......

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()更新布局;)