添加action到JButtons的网格 - JAVA

时间:2014-05-04 18:32:50

标签: jbutton actionlistener

我创建了一个JButtons网格,现在想要在单击其中一个按钮时添加ButtonListener和actionPerformed。 这是生成jbuttons网格的代码:

//Create the center grid, matrix of the robot world
protected JComponent getGrid(){
    JPanel gridPanel = new JPanel();                                        //create new panel
    gridPanel.setLayout(new GridLayout(this.cellsInColumn,this.cellsInRow));//define grid layout inside the panel in size of cellsInColumn and cellsInRow
    for(int i=0; i<cellsInColumn; i++)
        for(int j=0; j<cellsInRow; j++){
            JButton newButton = new JButton();
            gridPanel.add(new JButton(""));                                 //add empty JButton
        }

    return gridPanel;                                                       //return the created panel with the grid inside
}

这是我为ButtonListener编写的代码和actionPerformed用于屏幕中的其他按钮, 我成功地使用它们因为我在if语句中识别它们,但是网格中的按钮没有像其他按钮那样的名称......

public void actionPerformed(ActionEvent e){
    Point p = new Point();

    if(e.getActionCommand().equals("Move")){
        p.x = Integer.parseInt(JOptionPane.showInputDialog("Enter x value of the robot"));
        p.y = Integer.parseInt(JOptionPane.showInputDialog("Enter y value of the robot"));
        world.moveRobot(p);
    }
    if(e.getActionCommand().equals("New Robot")){
        Robot newRobot = new Robot(Direction.UP);
        p.x = Integer.parseInt(JOptionPane.showInputDialog("Enter x value for the new robot"));
        p.y = Integer.parseInt(JOptionPane.showInputDialog("Enter y value for the new robot"));
        world.addRobot(newRobot, p);
    }
    if(e.getActionCommand().equals("Turn Right")){
        p.x = Integer.parseInt(JOptionPane.showInputDialog("Enter x value for the new robot"));
        p.y = Integer.parseInt(JOptionPane.showInputDialog("Enter y value for the new robot"));
    }
    if(e.getActionCommand().equals("")){
        System.out.println("on empty");
    }
}

1 个答案:

答案 0 :(得分:0)

您可以像在此示例中一样尝试,它会在4行中创建5个JButton。 ActionListener类在单击时获取JButton并打印ActionCommand,我在这个简单的例子中设置了JButton ij(例如JButton12)。

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 *
 * @author Patrick Ott <Patrick.Ott@professional-webworkx.de>
 * @version 1.0
 */
public class ButtonPanel extends JPanel {

    private JRadioButton radioButton;

    public ButtonPanel() {

        this.setPreferredSize(new Dimension(800, 600));
        this.setLayout(new GridLayout(5, 1));
        this.add(getButtonGrid());
    }

    private JComponent getButtonGrid() {
        JPanel gridPanel = new JPanel(new GridLayout(4, 5));
        for(int i = 0; i < 5; i++) {
            for(int j = 0; j < 4; j++) {
                JButton btn = new JButton("Button_" + i+j);
                btn.setActionCommand("JButton" + i+j);
                btn.addActionListener(new MyActionListener());
                gridPanel.add(btn, i);
            }
        }
        return gridPanel;
    }

    class MyActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            JButton sourceBtn = (JButton)e.getSource();
            System.out.println(sourceBtn.getActionCommand());
        }

    }
}

帕特里克