在Jpanel

时间:2016-03-14 05:58:33

标签: java swing jpanel jbutton

我是这个动作命令和动作侦听器(鼠标按下)事件的新手。我在我的代码中使用了这个:

// This is the single event handler for all the buttons
public void actionPerformed(ActionEvent e) {
JButton selectedBtn = (JButton) e.getSource();

for (int row = 0; row < buttons.length; row++) {
  for (int col = 0; col < buttons[row].length; col++) {
    if (buttons[row][col] == selectedBtn) {
       moves++; //incrementing the moves
       System.out.printf("moves: %d%n", moves);
       System.out.printf("Selected row: %d%n", row+1);
       System.out.printf("Selected column: %d%n", col+1);

     }
   }
 }

这段代码显示正确按下按钮的行和列。但我想做的是我试图在我的Jlabels面前显示这些信息。

即如果按下按钮并且它位于第2行和第4列,它应该显示如下:

enter image description here

而是在输出控制台中显示为:

moves: 1
Selected row: 3
Selected column: 4

关于如何修改这些Jlabel以便在其上显示此信息的任何想法?

这些是我的Jlabels

//labels
JLabel label1 = new JLabel("COL: ");
label1.setLocation(10,260);
label1.setSize(100,100);
add(label1);

JLabel label2 = new JLabel("ROW: ");
label2.setLocation(10,280);
label2.setSize(100,100);
add(label2); 

任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:0)

尝试在JLabel实例上使用setText()