我正在构建一个数独游戏,我正在尝试为每个按钮添加不同的图形。我遇到的问题是让图形看起来正确。我对此进行了编辑以显示我的工作。我现在遇到的最大问题是我的绘图覆盖了我的按钮,所以我无法在按钮顶部获取图像......我要么得到按钮,要么得到绘图。
import java.awt.Graphics;
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
public class SudokuViewWorks extends JPanel {
private JPanel centerBoard;
private int cellSize = 48;
private int Rows;
private int Cols;
private int boardSize;
private MyPanel[][] region;
public SudokuViewWorks(SudokuBase sb){
super(new GridLayout(3,3,3,3));
setVisible(true);
SudokuBase board = sb;
Rows = board.rows;
Cols = board.columns;
boardSize = Rows * Cols;
//setBorder(BorderFactory.createLineBorder(Color.black));
region= new MyPanel[3][3];
for (int i =0; i < 3; i++){
for(int j =0; j<3; j++) {
region[i][j] = new MyPanel(i);
add(region[i][j]);
//repaint();
setPreferredSize(new Dimension(boardSize*cellSize + (boardSize*2 + 11), boardSize*cellSize +(boardSize*2 + 11)));
}
//public void paintComponent(Graphics g){
// a panel for the sudoku board
//centerBoard = new JPanel(new GridLayout(boardSize, boardSize));
//Cell button = new Cell(boardSize,boardSize);
//JButton button = new JButton();
//ImageIcon icon = new ImageIcon(drawSymbol(g,i));
//button.setIcon(icon);
//button.setLayout
//centerBoard.add(new MyPanel(i));
}
//add Jpanel
//add(region);
}
class MyPanel extends JPanel {
public int num;
JLabel[][] label = new JLabel[3][3];
public MyPanel(int numb) {
super(new GridLayout(3,3,1,1));
for(int i=0; i<3; i++) {
for (int j = 0; j<3; j++) {
label[i][j] = new JLabel("i");
//label[i][j] = new Cell(boardSize,boardSize);
add(label[i][j]);
num = numb;
setBorder(BorderFactory.createLineBorder(Color.black));
setVisible(true);
}
}
repaint();
}
public Dimension getPreferredSize() {
return new Dimension(50,50);
}
public void paintComponent(Graphics g) {
//super.paintComponent(g);
switch(num) {
case 1: g.setColor(Color.BLUE);
g.drawRect(15, 15, 25, 25);
case 2: g.drawOval(67, 67, 25, 25);
g.drawString("one", 10,20);
case 3: g.drawLine(119, 119, 144, 144);
g.drawString("2", 60,70);
g.drawLine(144, 119, 119, 144);
case 4: g.setColor(Color.YELLOW);
//g.fillRect(10, 20, 80, 70);
//g.setColor(Color.RED);
case 5: g.setColor(Color.GREEN);
g.fillOval(50, 25, 25, 25);
case 6: g.fillOval(50, 25, 25, 25);
case 7: g.fillOval(50, 25, 25, 25);
case 8: g.fillOval(50, 25, 25, 25);
case 9: g.fillOval(50, 25, 25, 25);
}
//repaint();
}
}
/**
* here is the method we are testing against
*/
public static SudokuBase makeBoard() {
SudokuBase board = new SudokuBoard(2, 3);
board.setValue(0, 3, 6);
board.setValue(0, 5, 1);
board.setValue(1, 2, 4);
board.setValue(1, 4, 5);
board.setValue(1, 5, 3);
board.setValue(2, 3, 3);
board.setValue(3, 2, 6);
board.setValue(4, 0, 2);
board.setValue(4, 1, 3);
board.setValue(4, 3, 1);
board.setValue(5, 0, 6);
board.setValue(5, 2, 1);
board.fixGivens();
board.setValue(1, 0, 6);
board.setValue(3, 1, 5);
return board;
}
/**public void setSelected(int row, int col){
}
public int getSelectedRow(){
}
public int getSelectedColumn(){
}*/
public static void main(String[] args) {
JFrame win = new JFrame("Test board");
win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
SudokuViewWorks view = new SudokuViewWorks(makeBoard());
//win.add(ggo);
win.add(view);
win.setVisible(true);
win.pack();
}
}