所以我对java很新,我正在为一个简单的扫雷游戏开发一个图形用户界面。最初我将所有代码集成到一个类中,并在同一个类中实例化我的Panel和Frame,但我的教授坚持要将我的Frame和Panel放在不同的类中。我现在仍然坚持如何在我的框架中实现我的面板。我尝试在main方法中创建类的实例,但是当我运行程序时它只显示一个空框架。在我设置单独的类之前,我的代码运行得很好,但现在我的面板没有添加到我的框架中。我在面板中遗漏了什么吗? 到目前为止,这是我的代码:
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("");
}
}
}
}
答案 0 :(得分:0)
您实际上从未调用任何会添加(或生成)void metersToFeetAndInches10(double meters, double *feet, double *inches) {
double unit = meters * meter_per_foot;
unit *= inch_per_foot * 10;
unit = round(unit);
*inches = modf(unit/(inch_per_foot * 10), feet) * (inch_per_foot * 10);
}
类内容的内容。
在Panel
构建器中,尝试拨打Panel
和getJPanel
,然后将这些面板添加到getJContentPane
课程中。
另外,这......
Panel
毫无意义,你正在创建两个实例Panel panel = new Panel();
Frame frame = new Frame();
frame.getContentPane().add(new Panel());
,但只使用一个。{1}}。你可以使用
Panel
代替。
此外,而不是使用...
public static void main(String[] args) {
Frame frame = new Frame();
frame.getContentPane().add(new Panel());
//...
}
可能会导致您的框架显示在其他OS元素(即任务栏)下面,您应该使用...
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = screenSize.width;
int height = screenSize.height;
setSize(width, height);
代替。
答案 1 :(得分:0)
如果将类拆分为单独的文件(比将其保存在单个文件中更好),则必须导入如下类:
假设您的课程都在同一个文件夹中
Minesweeper.java
import java.awt.event.InputEvent;
import java.awt.*;
import java.util.Random;
import javax.swing.*;
import MyPanel;
import MyFrame;
public class Minesweeper
{
public static void main(String[] args)
{
Frame frame = new MyFrame();
frame.getContentPane().add(new MyPanel());
frame.setVisible(true);
}
}
<强> Frame.java 强>
import javax.swing.*;
public class MyFrame extends JFrame
{
public MyFrame()
{
setExtendedState(JFrame.MAXIMIZED_BOTH);
setTitle("Minesweeper");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
<强> Panel.java 强>
import javax.swing.*;
public class MyPanel extends JPanel
{
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;
public MyPanel()
{
super();
}
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("");
}
}
}
}
注意:我只是复制导入,检查它们是否是您在每个课程中所需要的。