Код: Выделить всё
import javax.swing.*;
import java.awt.*;
public class DemoPanel extends JPanel {
// Screen settings
final int MAX_COL = 15;
final int MAX_ROW = 10;
final int NODE_SIZE = 70;
final int SCREEN_WIDTH = NODE_SIZE * MAX_COL;
final int SCREEN_HEIGHT = NODE_SIZE * MAX_ROW;
// Node
Node[][] node = new Node[MAX_COL][MAX_ROW];
Node startNode, finishNode, currentNode;
public DemoPanel() {
this.setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT));
this.setOpaque(true);
this.setBackground(Color.BLACK);
this.setLayout(new GridLayout(MAX_ROW, MAX_COL));
// Place Node
int col = 0;
int row = 0;
while ( col < MAX_COL && row < MAX_ROW ) {
node[col][row] = new Node(col, row);
this.add(node[col][row]);
col++;
if ( col == MAX_COL ) {
col = 0;
row++;
}
}
// set start and finish node
setStartNode(3, 6);
setFinishNode(11, 3);
}
private void setStartNode(int col, int row) {
node[col][row].setAsStart();
startNode = node[col][row];
currentNode = startNode;
}
private void setFinishNode(int col, int row) {
node[col][row].setAsFinish();
finishNode = node[col][row];
}
}
Код: Выделить всё
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Node extends JButton implements ActionListener {
Node parent;
int col;
int row;
int gCost;
int hCost;
int fCost;
boolean start;
boolean finish;
boolean solid;
boolean open;
boolean checked;
public Node(int col, int row) {
this.col = col;
this.row = row;
setOpaque(true);
setBorderPainted(false);
setFocusPainted(true);
setBackground(Color.BLACK);
setForeground(Color.BLACK);
this.addActionListener(this);
}
public void setAsStart() {
setBackground(Color.BLUE);
setForeground(Color.BLACK);
setText("Start");
start = true;
}
public void setAsFinish() {
setBackground(Color.YELLOW);
setForeground(Color.BLACK);
setText("Finish");
finish = true;
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Node (" + col + ", " + row + ") clicked!");
this.repaint();
this.setBackground(Color.ORANGE);
}
}
Но я хочу изменить цвет белой части, а не цвет позади это. Могу ли я это сделать?
Я попробовал
Код: Выделить всё
//setBorderPainted(false);Я тоже попробовал
Код: Выделить всё
this.setBorder(BorderFactory.createEmptyBorder());Подробнее здесь: https://stackoverflow.com/questions/783 ... on-element
Мобильная версия