1.将一位数组打乱放置二维数组当中
package Test;
import java.util.Random;
public class Test {
public static void main(String[] args) {
//打乱一维数组
int[] tempArr={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
Random r=new Random();
for(int i=0;i<tempArr.length;i++){
int index=r.nextInt(tempArr.length);
int temp=tempArr[i];
tempArr[i]=tempArr[index];
tempArr[index]=temp;
}
//输出打乱后的数组
for (int i = 0; i < tempArr.length; i++) {
System.out.print(tempArr[i]+" ");
}
//定义二维数组
int[][]data=new int[4][4];
//将数据添加至二维数组中
//解法①遍历一维数组
for (int i = 0; i < tempArr.length; i++) {
data[i/4][i%4]=tempArr[i];
}
//解法②
int index=0;
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
data[i][j]=tempArr[index]; //此时的tempArr[index]已经是打乱了的数组了
index++;
}
}
//遍历二维数组
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
System.out.print(data[i][j]+" ");
}
System.out.println();
}
}
}
对于解法①思想很巧妙,运用模和取余的方式进行。仅适用于矩阵形式的数组。
2.动作监听ActionListener()
只能监听空格与鼠标按下的时的状态。
//①、用匿名内部类去监听
public static void main(String[] args) { JFrame jFrame=new JFrame(); jFrame.setSize(603,680); jFrame.setTitle("事件演示"); jFrame.setAlwaysOnTop(true); jFrame.setLocationRelativeTo(null); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.setLayout(null); //创建一个按钮对象 JButton button=new JButton("按钮"); button.setBounds(0,0,100,50); button.addActionListener(new ActionListener() { //匿名内部类 @Override public void actionPerformed(ActionEvent e) { System.out.println("按钮被点击了"); } }); //动作监听与鼠标左键点击 //为按钮添加事件监听器 jFrame.getContentPane().add(button); jFrame.setVisible(true); }
//把重写的方法写在自己的类中,就不用再new了,直接用this便可以调用, ActionListener本身就是一个接口。
public class MyJFrame extends JFrame implements ActionListener { JButton button1=new JButton("按钮1"); JButton button2=new JButton("按钮2"); public MyJFrame(){ JFrame jFrame=new JFrame(); jFrame.setSize(603,680); jFrame.setTitle("事件演示"); jFrame.setAlwaysOnTop(true); jFrame.setLocationRelativeTo(null); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.setLayout(null); //创建按钮对象 button1.setBounds(0,0,200,50); button1.addActionListener(this); //创建按钮对象 button2.setBounds(0,0,100,50); button2.addActionListener(this); //按钮添加到页面中 this.getContentPane().add(button1); this.getContentPane().add(button2); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { Object source=e.getSource(); if(source==button1){ button1.setSize(100,100); } else if (source==button2) { Random r=new Random(); button2.setSize(r.nextInt(500),r.nextInt(500)); } System.out.println("按钮被点击了"); } }
3.MouseListener(鼠标监听)
package Test; import javax.swing.*;; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; //鼠标监听 public class MyJFrame1 extends JFrame implements MouseListener { JButton button1=new JButton("按钮1"); public MyJFrame1(){ JFrame jFrame=new JFrame(); jFrame.setSize(603,680); jFrame.setTitle("事件演示"); jFrame.setAlwaysOnTop(true); jFrame.setLocationRelativeTo(null); jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jFrame.setLayout(null); //创建按钮对象 button1.setBounds(0,0,200,50); button1.addMouseListener(this); //添加按钮到窗体中,显示窗体 this.getContentPane().add(button1); this.setVisible(true); } @Override public void mouseClicked(MouseEvent e) { System.out.println("按钮1被点击了"); } @Override public void mousePressed(MouseEvent e) { System.out.println("按下不松"); } @Override public void mouseReleased(MouseEvent e) { System.out.println("松开鼠标"); } @Override public void mouseEntered(MouseEvent e) { System.out.println("划入"); } @Override public void mouseExited(MouseEvent e) { System.out.println("划出"); } }
4.键盘监听
package Test;
import javax.swing.*;;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
//键盘监听
public class MyJFrame2 extends JFrame implements KeyListener {
public MyJFrame2() {
JFrame jFrame = new JFrame();
jFrame.setSize(603, 680);
jFrame.setTitle("事件演示");
jFrame.setAlwaysOnTop(true);
jFrame.setLocationRelativeTo(null);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setLayout(null);
//给整个窗体添加键盘监听
//调用者this:本类对象,当前的页面对象
//addKeyListener(this),表示我要给整个页面添加监听
this.addKeyListener(this);
//显示整个页面
this.setVisible(true);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println("按下不松");
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println("松开按钮");
//获取键值
int code = e.getKeyCode();
System.out.println(code);
}
}