单独识别GridLayout中的JButton

时间:2014-01-05 02:05:03

标签: java swing jbutton jlabel

我一直在创建一个系统,通过它我可以在GUI中列出列车上的各种座位。我决定使用一个简单的网格布局,其中编号的JButtons代表座位,seatNo和空的JLabel代表空的空间(我意识到这可能是微不足道的但很简单)。我使用了Gridlayout,并创建了按钮和标签,如下所示。

我有一个List(compArray2)在一个单独的类中,由0和1组成,方向表示应该有座位的位置以及不应该是座位的位置。 .getNum获取空值为0或座位为1,而.getDir获取座位将面向的方向。

public JPanel rowPanelCreation(int type, int rowNo, int width){

    theNoOfSeats=0;
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(0,width));

    List<seatLayout> layout = new ArrayList<>();
    layout = ModelConstants.compArray2;


    for (seatLayout isit : x2){

        buttonEna = new JButton();
            buttonEna.setFont(new Font("Sans Serif", Font.BOLD , 14));
            buttonEna.setBorderPainted(false);
        buttonDis = new JLabel();

        if (isit.getNum()==0){
            if (isit.getDir()=='x'){
            panel.add(buttonDis);
            }
        }

        else if (isit.getNum()==1){

            theNoOfSeats++;

            if (isit.getDir()=='N'){
                image = new ImageIcon("/Users/Tomousee/NetBeansProjects/SortedList/src/Resources/chairDefaultN.png"); 
                buttonEna.setIcon(image);
                buttonEna.setText(String.valueOf(ModelConstants.compSeatNo.get(theNoOfSeats)));
                buttonEna.setHorizontalTextPosition(JButton.CENTER);
                buttonEna.setVerticalTextPosition(JButton.CENTER);
                panel.add(buttonEna);
                }
            else if (isit.getDir()=='E'){
                image = new ImageIcon("/Users/Tomousee/NetBeansProjects/SortedList/src/Resources/chairDefaultE.png");
                buttonEna.setIcon(image);
                buttonEna.setText(String.valueOf(ModelConstants.compSeatNo.get(theNoOfSeats)));
                buttonEna.setHorizontalTextPosition(JButton.CENTER);
                buttonEna.setVerticalTextPosition(JButton.CENTER);
                buttonEna.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){JOptionPane.showMessageDialog(null,"You have chosen : ");}});
                panel.add(buttonEna);
                }
            else if (isit.getDir()=='S'){
                image = new ImageIcon("/Users/Tomousee/NetBeansProjects/SortedList/src/Resources/chairDefaultS.png");
                buttonEna.setIcon(image);
                buttonEna.setText(String.valueOf(ModelConstants.compSeatNo.get(theNoOfSeats)));
                buttonEna.setHorizontalTextPosition(JButton.CENTER);
                buttonEna.setVerticalTextPosition(JButton.CENTER);
                panel.add(buttonEna);
                }
            else if (isit.getDir()=='W'){
                image = new ImageIcon("/Users/Tomousee/NetBeansProjects/SortedList/src/Resources/chairDefaultW.png");
                buttonEna.setIcon(image);
                buttonEna.setText(String.valueOf(ModelConstants.compSeatNo.get(theNoOfSeats)));
                buttonEna.setHorizontalTextPosition(JButton.CENTER);
                buttonEna.setVerticalTextPosition(JButton.CENTER);
                panel.add(buttonEna);
                }
        }
    }
    return panel;
}

我遇到的主要问题是,当我按下其中一个座位按钮时,我想要一条消息,告诉我我所选座位的座位号是。但是,当我这样做时,它只显示最后一个座位。有没有更好的方法可以做到这一点?我假设所有按钮基本相同,没有办法区分它们?

1 个答案:

答案 0 :(得分:3)

  

“我想要一条消息,告诉我我所选座位的座位号是。但是当我这样做时,它只显示最后一个座位。”

这是你的问题。这是一个参考问题。最后,所有按钮都具有相同的JButtonJLabel引用。因此,所有按钮和标签都将获得 last 按钮并创建标签参考,因此“显示最后一个seatNo。”

buttonEna = new JButton();
buttonEna.setFont(new Font("Sans Serif", Font.BOLD , 14));
buttonEna.setBorderPainted(false);
buttonDis = new JLabel();

您需要在每次迭代时创建一个新的JButton和新的JLabel引用

JBUtton buttonEna = new JButton();
buttonEna.setFont(new Font("Sans Serif", Font.BOLD , 14));
buttonEna.setBorderPainted(false);
JLabel buttonDis = new JLabel();

另一种方法是使用JButton数组循环来设置按钮,并将它们添加到Panel。假设你有GridLayout(2, 2)。然后,您需要一个4 JButton s

的数组
JButton[] buttons = new JButton[9];
...
for (int i = 0; i < 4; i++){
    buttons[i] = new JButton();
    buttons[i].setFont(new Font("Sans Serif", Font.BOLD , 14));
    buttonEna.setBorderPainted(false);
    // set other properties for button
    panel.add(buttons[i]);
}
相关问题