Итак, я довольно новичок в java, и я работаю над графическим интерфейсом для простой игры тральщика. Первоначально у меня был весь мой код, интегрированный в один класс, и я создал экземпляр Panel и Frame в том же классе, но мой профессор настоял, чтобы я поместил свою рамку и панель в отдельные классы. Я теперь зациклен на том, как реализовать свою панель в моем кадре. Я попытался создать экземпляр класса в моем основном методе, но когда я запустил свою программу, он просто отобразил пустой фрейм. Прежде чем я установил отдельные классы, мой код работал отлично, но теперь моя панель не добавляется в мой фрейм. Я что-то пропустил в своей панели? Вот мой код до сих пор:У меня возникли проблемы с вызовом кода из класса
import java.awt.event.InputEvent;
import java.awt.*;
import java.util.Random;
import javax.swing.*;
public class Minesweeper
{
public static void main(String[] args)
{
Frame frame = new Frame();
frame.getContentPane().add(new Panel());
frame.setVisible(true);
}
}
Это только каркас.
class Frame extends JFrame
{
public Frame()
{
setTitle("Minesweeper");
setExtendedState(JFrame.MAXIMIZED_BOTH);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
И это мой класс панели. У меня проблемы.
class Panel extends JPanel
{
public Panel()
{
super();
this.setLayout(new BorderLayout());
add(getJPanel());
add(getJContentPane());
}
private int columns = 8;
private int rows = 8;
boolean jBombs[][] = new boolean[rows][columns];
boolean jShown[][] = new boolean[rows][columns];
int jCells[][] = new int[rows][columns];
private int currX, currY = 0;
JToggleButton jButtons[] = new JToggleButton[columns*rows];
private JPanel jPanel = null;
private JToolBar jToolBar = null;
private JPanel jContentPane = null;
private JButton jBtnNewGame = null;
private JProgressBar jProgressBar = null;
private JPanel getJPanel()
{
if (jPanel == null)
{
jPanel = new JPanel();
jPanel.setLayout(new BorderLayout());
jPanel.add(getJContentPane(), BorderLayout.CENTER);
jPanel.add(getJProgressBar(), BorderLayout.SOUTH);
}
return jPanel;
}
private JPanel getJContentPane()
{
if (jContentPane == null)
{
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(rows);
gridLayout.setColumns(columns);
jContentPane = new JPanel();
jContentPane.setLayout(gridLayout);
BuildBoard();
}
return jContentPane;
}
private void BuildBoard()
{
if(jProgressBar != null)
{
jProgressBar.setValue(0);
}
jContentPane.removeAll();
int i = 0;
for(int x = 0; x < rows; x++)
{
for(int y = 0; y < columns; y++)
{
currX = x;
currY = y;
Random randBomb = new Random();
jBombs[x][y] = randBomb.nextBoolean() && randBomb.nextBoolean() && randBomb.nextBoolean();
jButtons[i] = new JToggleButton("?");
jButtons[i].addMouseListener(new java.awt.event.MouseAdapter(){
public void mouseReleased(java.awt.event.MouseEvent e) {
if(e.getModifiers() == InputEvent.BUTTON3_MASK)
{
markCell(e);
}
else if(e.getModifiers() == InputEvent.BUTTON1_MASK)
{
showCell(e);
}
}
});
jContentPane.add(jButtons[i]);
i++;
}
}
for(int x = 0; x < rows; x++)
{
for(int y = 0; y < columns; y++)
{
jCells[x][y] = bombCount(x, y);
jShown[x][y] = false;
}
}
jContentPane.setEnabled(true);
this.repaint();
this.validate();
}
private JProgressBar getJProgressBar()
{
if (jProgressBar == null)
{
jProgressBar = new JProgressBar();
jProgressBar.setMaximum(columns * rows);
}
return jProgressBar;
}
private void showAllBombs()
{
for(int x = 0; x < rows; x++)
{
for(int y = 0; y < columns; y++)
{
if(jBombs[x][y] == true)
{
JToggleButton jButton = findButton(x,y);
if(jButton.isEnabled())
{
jProgressBar.setValue(jProgressBar.getValue() + 1);
}
jButton.setText("X");
jButton.setSelected(true);
jButton.setEnabled(false);
}
}
}
}
private void clearCells(int x, int y)
{
if(inBounds(x, y))
{
if(!jShown[x][y] && jBombs[x][y] == false)
{
jShown[x][y] = true;
JToggleButton jButton = findButton(x,y);
if(jCells[x][y] > 0)
{
jButton.setText(Integer.toString(jCells[x][y]));
}
else
{
jButton.setText("");
}
if(jButton.isEnabled())
{
jProgressBar.setValue(jProgressBar.getValue() + 1);
}
jButton.setSelected(true);
jButton.setEnabled(false);
if(jCells[x][y] == 0)
{
for(int r = -1; r <= 1; r++)
{
for(int c = -1; c <= 1; c++)
{
clearCells(x + r, y + c);
}
}
}
}
}
}
private boolean inBounds(int x, int y)
{
return 0 <= x && x < jCells.length && 0 <= y && y < jCells[x].length;
}
private boolean isBomb(JToggleButton jButton)
{
int i = 0;
for(int x = 0; x < rows; x++)
{
for(int y = 0; y < columns; y++)
{
if(jButton == jButtons[i])
{
currX = x;
currY = y;
return jBombs[x][y];
}
i++;
}
}
return false;
}
private void disableBoard()
{
for(int x = 0; x < rows; x++)
{
for(int y = 0; y < columns; y++)
{
JToggleButton jButton = findButton(x,y);
jButton.setEnabled(false);
}
}
}
private JToggleButton findButton(int x, int y)
{
return jButtons[(x*rows+y)];
}
private void showCell(java.awt.event.MouseEvent e)
{
JToggleButton jButton = (JToggleButton)e.getSource();
if(jButton.isEnabled())
{
jProgressBar.setValue(jProgressBar.getValue() + 1);
jButton.setEnabled(false);
if(isBomb(jButton))
{
showAllBombs();
jButton.setEnabled(false);
JOptionPane.showMessageDialog(null,"You lost " + Math.round((jProgressBar.getPercentComplete() * 100)) + "% through.", "You Lost!", JOptionPane.INFORMATION_MESSAGE);
disableBoard();
System.exit(0);
}
else
{
if(jCells[currX][currY] > 0)
{
jButton.setText(Integer.toString(jCells[currX][currY]));
}
else if(jCells[currX][currY] == 0)
{
clearCells(currX, currY);
}
}
}
}
private int bombCount(int x, int y)
{
int bombCount = 0;
for(int r = -1; r <= 1; r++)
{
for(int c = -1; c <= 1; c++)
{
int newx = x + r;
int newy = y + c;
if(inBounds(newx, newy))
{
if(jBombs[newx][newy] == true)
{
bombCount++;
}
}
}
}
return bombCount;
}
private void markCell(java.awt.event.MouseEvent e)
{
JToggleButton jButton = (JToggleButton)e.getSource();
if(jButton.isEnabled())
{
if(jButton.getText() != "!")
{
jButton.setText("!");
}
else
{
jButton.setText("");
}
}
}
}
Пожалуйста, укажите ** минимальный пример **: http://stackoverflow.com/help/mcve –