【问题标题】:Swapping JButton elements in Java在 Java 中交换 JButton 元素
【发布时间】:2012-09-16 12:33:25
【问题描述】:

更新了我的代码我意识到我应该有但 JButton 上的 actionListener(因为他们是真正做所有工作的人)。我的逻辑现在在排序部分是关闭的。我能够让 jButtons 交换值,但我的输出看起来正确,但我试图找出我在哪里搞砸了我的排序机制。这个程序“应该”通过使用单选按钮选择一个起点,并为 JButton 选择一个不同的位置来交换正确地工作。 (例如,radio 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]);
  }
}

【问题讨论】:

  • 另外,代码还能编译吗?正如发布的那样,您的 moveTeam 方法不会。
  • 你必须使用单选按钮吗??
  • 不,我的代码无法编译。嗯,确实如此,但 moveTeam 方法不起作用。 :( 和单选按钮是奖励工作的一个组成部分。

标签: java swing awt jbutton


【解决方案1】:

我不会交换 JButton 本身,而是提供 JButton 的 AbstractAction 并简单地交换这些 Action,因为这非常容易做到。或者你可以交换 JButton 的文本和 actionCommand(如果你要走这条路,你应该两者都做),但这会涉及到并行交换,我觉得不那么干净。

【讨论】:

  • 这就是我试图做的,是交换按钮中的文本。我有一种在打印出来时有效的排序方法,并且用户输入了 from,to,然后交换了名称。我正在尝试对 GUI 按钮做同样的事情,并让自己陷入困境。感谢所有的帮助!
【解决方案2】:

我会将JRadioButtons 替换为JCheckBoxs,主要是因为您可以同时选择两个,并且允许以这种方式选择复选框比允许单选按钮更有意义.

我会为所有复选框创建一个动作监听器,这样每次点击都会通过它。

在动作监听器中,我会使用 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() 更新布局;)

【讨论】:

  • 好的,我意识到我在单选按钮上有 eventHandler,它们应该在 JButtons 上以使它们移动。我仍然无法让 radio[i] 的值指向相应的 JButton 面。例如,如果我点击单选按钮 3 和按钮 5,那么第三个按钮应该移动到 5 点(其他文本向上移动)。
猜你喜欢
  • 1970-01-01
  • 2021-06-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-25
  • 1970-01-01
  • 2018-02-24
  • 2017-01-30
相关资源
最近更新 更多