所以我有以下代码,每当我运行程序时它都会给我一个错误。这对我来说似乎都是正确的,但事实并非如此。
它应该采用文本字段中的任何值并将方块移动到该位置,并且每当用户输入新值时,应该将方块更改为文本字段应该是的任何值。谁能解决这个问题,或者至少告诉我什么是错的?对代码感到抱歉,当我开始尝试调试它时,它变得更加搞砸了。
错误是
Exception in thread "main" java.lang.NullPointerException
at com.theDevCorner.Game$OptionPanel.<init>(Game.java:228)
at com.theDevCorner.Game$GridPane.<init>(Game.java:81)
at com.theDevCorner.Game.<init>(Game.java:35)
at com.theDevCorner.Game.main(Game.java:52)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Game extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
private GridPane gridPane;
private DragPanel drag;
public boolean isMouseClicked = false;
public static JMenuBar bar = new JMenuBar();
public int gridY = 1;
public int gridX = 1;
public Game() {
setLayout(new BorderLayout());
OptionPanel options = new OptionPanel();
options.addActionListener(this);
add(options, BorderLayout.NORTH);
gridPane = new GridPane();
gridPane.setBorder(BorderFactory.createLineBorder(Color.white));
add(gridPane);
drag = new DragPanel(options);
drag.setBorder(BorderFactory.createLineBorder(Color.white));
drag.setBackground(new Color(100, 100, 125));
add(drag, BorderLayout.WEST);
}
public static void main(String args[]) {
Game game = new Game();
JFrame frame = new JFrame();
frame.setTitle("Game");
frame.setVisible(true);
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(game);
frame.pack();
frame.setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e);
if (e.getActionCommand().equalsIgnoreCase("grid")) {
gridPane.setGridOn(!gridPane.isGridOn());
}
if (e.getActionCommand().equalsIgnoreCase("square")) {
gridPane.setSqaureOn(!gridPane.isSquareOn());
}
if (e.getActionCommand().equalsIgnoreCase("vgrid")) {
gridPane.setVertOn(!gridPane.isVertOn());
}
}
public class GridPane extends JPanel {
public OptionPanel op = new OptionPanel();
private static final long serialVersionUID = 1L;
private boolean gridOn = false;
private boolean squareOn = false;
private boolean vertOn = false;
public int x = 0,y = 0,w = 0,h = 0;
public GridPane() {
setBackground(Color.BLACK);
}
public boolean isGridOn() {
return gridOn;
}
public boolean isSquareOn() {
return squareOn;
}
public boolean isVertOn() {
return vertOn;
}
public void setGridOn(boolean value) {
if (value != gridOn) {
this.gridOn = value;
repaint();
}
}
public void setVertOn(boolean value) {
if (value != vertOn) {
this.vertOn = value;
repaint();
}
}
public void setSqaureOn(boolean value) {
if (value != squareOn) {
this.squareOn = value;
repaint();
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Toolkit tk = Toolkit.getDefaultToolkit();
if (gridOn) {
System.out.println("Grid works");
g.setColor(Color.white);
for (int i = 0; i < tk.getScreenSize().height; i += 64) {
gridY++;
g.drawLine(0, (64 * gridY), tk.getScreenSize().width, (64 * gridY));
}
}
gridY = -1;
gridX = -1;
if (vertOn) {
System.out.println("vert grid works");
g.setColor(Color.white);
for (int ig = 0; ig < tk.getScreenSize().width; ig += 64) {
gridX++;
g.drawLine((64 * gridX), 0, (64 * gridX), tk.getScreenSize().height);
}
}
if (squareOn) {
System.out.println("Square works");
g.setColor(Color.red);
x = Integer.parseInt(op.squareX.getText());
y = Integer.parseInt(op.squareY.getText());
w = Integer.parseInt(op.squareW.getText());
h = Integer.parseInt(op.squareH.getText());
g.fillRect(x,y,w,h);
}
x = 0;
y = 0;
w = 64;
h = 64;
}
}
public class DragPanel extends JPanel {
OptionPanel op;
public DragPanel(OptionPanel op) {
this.op = op;
this.add(this.op.squareButton);
this.op.squareButton.setActionCommand("square");
}
public void addActionListener(ActionListener listener) {
System.out.println(listener);
this.op.squareButton.addActionListener(listener);
}
}
private static class Square {
}
private class OptionPanel extends JPanel {
public JButton grid;
public JButton vgrid;
public JButton squareButton;
public JTextField squareX;
public JTextField squareY;
public JTextField squareW;
public JTextField squareH;
public int x,y,w,h;
public Square square = new Square();
public OptionPanel() {
//Sets the stuff for the panel
setBackground(new Color(155, 0, 255));
setLayout(new GridBagLayout());
//end
//The Show Grid Button Stuff
grid = new JButton("Show Horizontal Grid");
grid.setActionCommand("grid");
//end
//The vertical grid
vgrid = new JButton("Show Vertical Grid");
vgrid.setActionCommand("vgrid");
//end
//The Square tool button stuff
squareButton = new JButton("Sqaure Tool");
//end
squareX = new JTextField(gridPane.x); //<----- THIS IS WHERE THE PROBLEM IS!!!!!
squareY = new JTextField("1",3);
squareW = new JTextField("1",3);
squareH = new JTextField("1",3);
//The gridbagConstraints things
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTH;
//kind of like padding
gbc.weighty = 1;
//sets the positions
gbc.gridx = 0;
gbc.gridy = 0;
//add it
add(grid, gbc);
//changes position for the second button
gbc.gridx = -1;
gbc.gridy = 0;
// adds it
add(vgrid, gbc);
//end
add(squareX, gbc);
add(squareY, gbc);
add(squareW, gbc);
add(squareH, gbc);
}
public void addActionListener(ActionListener listener) {
//adds action listeners
grid.addActionListener(listener);
vgrid.addActionListener(listener);
squareButton.addActionListener(listener);
squareX.addActionListener(listener);
squareY.addActionListener(listener);
squareW.addActionListener(listener);
squareH.addActionListener(listener);
}
}
squareX = new JTextField("0" + gridPane.x,3); //<----- THIS IS WHERE THE PROBLEM IS!!!!!
squareY = new JTextField("0" + gridPane.y,3);
squareW = new JTextField("0" + gridPane.w,3);
squareH = new JTextField("0" + gridPane.h,3);
似乎主要的错误来自于我尝试做这样的事情......
答案 0 :(得分:3)
OptionPanel options = new OptionPanel();
options.addActionListener(this);
add(options, BorderLayout.NORTH);
gridPane = new GridPane();
在创建GridPane之前创建OptionPanel,因此当您的optionPanel尝试访问该变量时,gridPane变量为null。
答案 1 :(得分:1)
在创建gridPane
之前创建OptionPanel
你的代码应该是
gridPane = new GridPane();
OptionPanel options = new OptionPanel();
options.addActionListener(this);
add(options, BorderLayout.NORTH);
gridPane.setBorder(BorderFactory.createLineBorder(Color.white));
add(gridPane);
答案 2 :(得分:1)
您可以通过执行类似......
之类的操作来修复NullPointerException
public Game() {
setLayout(new BorderLayout());
gridPane = new GridPane();
gridPane.setBorder(BorderFactory.createLineBorder(Color.white));
OptionPanel options = new OptionPanel();
options.addActionListener(this);
add(options, BorderLayout.NORTH);
add(gridPane);
drag = new DragPanel(options);
drag.setBorder(BorderFactory.createLineBorder(Color.white));
drag.setBackground(new Color(100, 100, 125));
add(drag, BorderLayout.WEST);
}
你是第二个NullPointerException
更棘手......
你有一个循环依赖问题......
A类试图创建B类,当C类依赖于B的实例时,它试图创建C类......
澄清...... GridPane
的实例为OptionPane
,但OptionPane
取决于GridPane
的状态,即null
,因为在GridPane
初始化之前OptionPane
无法初始化......混淆了......
您可以使用延迟加载方法解决此问题。也就是说,在你需要它之前,不要试图侮辱OptionPane
......
public class GridPane extends JPanel {
public OptionPanel op;
// Other variables...
// Other methods...
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Other paint code...
if (squareOn) {
if (op == null) {
op = new OptionPanel();
}
System.out.println("Square works");
g.setColor(Color.red);
x = Integer.parseInt(op.squareX.getText());
y = Integer.parseInt(op.squareY.getText());
w = Integer.parseInt(op.squareW.getText());
h = Integer.parseInt(op.squareH.getText());
g.fillRect(x, y, w, h);
}
}
}
然而如何。从长远来看,我不认为这会实现你想要的,因为OptionPane
的状态在类之间会有所不同......(GridPane
和OptionPane
赢了不要共享同一个实例....)
更好的解决方案是删除这些直接依赖项,以便您可以在GridPane
和OptionPane
之间传递某种模型的相同引用,这将允许它充当粘合剂他们之间......