📄 grapharr.java
字号:
package datastructure;
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JButton;
public class GraphArr
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];
public GraphArr(int vertexnum) {
this.setLayout(null);
createArray(vertexnum);
}
public void createArray(int vertexnum) {
this.vertexnum = vertexnum;
for (int i = 0; i < vertexnum; i++)
for (int j = 0; j < vertexnum; j++) {
if (i == 0) {
promptrow[j] = new Label("" + (char) ('a' + j), Label.CENTER);
add(promptrow[j]);
promptrow[j].setSize(30, 20);
promptrow[j].setLocation(30 + 35 * j, 10);
}
if (j == 0) {
promptcol[i] = new Label("" + (char) ('a' + i), Label.CENTER);
add(promptcol[i]);
promptcol[i].setSize(30, 20);
promptcol[i].setLocation(0, 30 + 25 * i);
}
button[i][j] = new Button("0");
button[i][j].setName("" + (i * vertexnum + 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 < vertexnum; i++)
for (int j = 0; j < vertexnum; 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());
i = position / vertexnum;
j = position % vertexnum;
if (sourceButton.getLabel().equals("0")) {
button[i][j].setLabel("1");
button[j][i].setLabel("1");
array[i][j] = 1;
array[j][i] = 1;
}
else {
button[i][j].setLabel("0");
button[j][i].setLabel("0");
array[i][j] = 0;
array[j][i] = 0;
}
}
public void clearArray() {
for (int i = 0; i < 12; i++)
for (int j = 0; j < 12; j++)
array[i][j] = 0;
}
public int[][] getArray() {
return array;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -