📄 matrixarr.java
字号:
package datastructure;
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.*;
public class MatrixArr
extends JPanel
implements java.awt.event.ActionListener {
private int vertexnum;
private Button button[][] = new Button[12][12];
private Label promptrow[] = new Label[12];
private Label promptcol[] = new Label[12];
int array[][] = new int[12][12];
int matrixcol = 6;
int matrixrow = 6;
ThreeElem elemdata[] = new ThreeElem[20];
int count = 0;
java.util.Vector vector = new java.util.Vector();
public MatrixArr() {
this.setLayout(null);
createArray(6, 6);
}
public void createArray(int row, int col) {
vector.removeAllElements();
count = 0;
matrixcol = col;
matrixrow = row;
for (int i = 0; i < col; i++)
for (int j = 0; j < row; j++) {
if (i == 0) {
promptrow[j] = new Label("" + (1 + j), Label.CENTER);
add(promptrow[j]);
promptrow[j].setSize(30, 20);
promptrow[j].setLocation(0, 30 + 25 * j);
}
if (j == 0) {
promptcol[i] = new Label("" + (1 + i), Label.CENTER);
add(promptcol[i]);
promptcol[i].setSize(30, 20);
promptcol[i].setLocation(30 + 35 * i, 10);
}
button[i][j] = new Button("0");
button[i][j].setName("" + (i * matrixcol + j));
button[i][j].addActionListener(this);
add(button[i][j]);
button[i][j].setSize(30, 20);
button[i][j].setLocation(30 + 35 * i, 30 + 25 * j);
}
}
public void clear() {
for (int i = 0; i < matrixcol; i++)
for (int j = 0; j < matrixrow; j++) {
if (i == 0) {
this.remove(promptrow[j]);
}
if (j == 0) {
this.remove(promptcol[i]);
}
this.remove(button[i][j]);
}
}
public void actionPerformed(java.awt.event.ActionEvent e) {
int i, j, position;
Button sourceButton = (Button) e.getSource();
position = Integer.parseInt(sourceButton.getName());
j = position / matrixcol;
i = position - j * matrixcol;
String strdata = new String("0");
int data = 0;
try {
strdata = JOptionPane.showInputDialog(this, "输入数据");
data = Integer.parseInt(strdata);
}
catch (NumberFormatException exp) {
JOptionPane.showMessageDialog(this, "输入非法字符");
strdata = "0";
data = 0;
}
if (!vector.contains(String.valueOf(position))) {
vector.add(String.valueOf(position));
count++;
}
button[j][i].setLabel(strdata);
array[i][j] = data;
elemdata[count] = new ThreeElem(i + 1, j + 1, data);
}
public void clearArray() {
for (int i = 0; i < 12; i++)
for (int j = 0; j < 12; j++)
array[i][j] = 0;
}
public TSMatrix getTsmatrix() {
return new TSMatrix(matrixrow, matrixcol, count, elemdata);
}
public int[][] getArray() {
return array;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -