java怎么给按钮添加事件

Python013

java怎么给按钮添加事件,第1张

类名后面添加一个actionListener 接口

解决方法

(1)public void actionPerformed(ActionEvent e) {

}

这么一个方法,然后再在构造方法里面添加注册事件,button.addActionListener(this);

(2) 在res/layout中的activity_main.xml添加一个按钮,命名为button1。

(3)在MainActivity.Java中添加按钮事件绑定:

(4) 重写ButtonClickListener函数:

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import javax.swing.*

public class Queens{

int[] a = new int[8]

int[] b = new int[15]

int[] c = new int[15]

int[][] Queen = new int[8][8]

void next(int i) {

for (int j = 0j <8j++) {

if (a[j] == 0 &&b[i + j] == 0 &&c[i - j + 7] == 0) {

a[j] = b[i + j] = c[i - j + 7] = 1

Queen[i][j] = 1

if (i <7)

next(i + 1)

else {

String output = new String()

for (int m = 0m <8m++) {

for (int n = 0n <8n++)

output += " " + Queen[m][n] + " "

output += "\n"

}

JTextArea outputArea = new JTextArea()

outputArea.setText(output)

JOptionPane.showMessageDialog(null, outputArea,

"One possible distribution ",

JOptionPane.INFORMATION_MESSAGE)

System.exit(0)

}

a[j] = b[i + j] = c[i - j + 7] = Queen[i][j] = 0

}

}

}

public static void main(String args[]) {

Queens one = new Queens()

one.next(0)

System.exit(0)

}

}