Java “的多个实例”;不使用局部变量的值";
我正在学习Java,并试图下一盘棋。我想做的第一件事是将一个框架设置为一块电路板,然后使用8x8的JButton网格在电路板上制作正方形,但是我的一些代码似乎根本没有被使用 我的框架将被加载,但是没有添加任何按钮,我用来存储每个正方形位置的数组没有被使用,显然我要添加按钮的网格也没有被使用。我几乎修改了代码的每一部分,但还是弄不明白 我添加了Eclipse在引号中给出警告的地方 //“像这样” 这是我的棋盘课:Java “的多个实例”;不使用局部变量的值";,java,eclipse,swing,jbutton,chess,Java,Eclipse,Swing,Jbutton,Chess,我正在学习Java,并试图下一盘棋。我想做的第一件事是将一个框架设置为一块电路板,然后使用8x8的JButton网格在电路板上制作正方形,但是我的一些代码似乎根本没有被使用 我的框架将被加载,但是没有添加任何按钮,我用来存储每个正方形位置的数组没有被使用,显然我要添加按钮的网格也没有被使用。我几乎修改了代码的每一部分,但还是弄不明白 我添加了Eclipse在引号中给出警告的地方 //“像这样” 这是我的棋盘课: import java.awt.GridLayout; import java.io
import java.awt.GridLayout;
import java.io.*;
import javax.swing.ImageIcon; //"the import is never used"
import javax.swing.JFrame;
import javax.swing.JButton; //"the import is never used"
import Logic.ChessSquare;
public class ChessBoard {
//chess board constructor
public ChessBoard() throws IOException {
//create grid and grid dimensions
GridLayout grid = new GridLayout(8,8); //"the value of the local variable grid is never used"
//create frame and set specifications of frame
JFrame frame = new JFrame();
frame.setVisible(true);
frame.pack();
frame.setTitle("I should have started this sooner");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//initialise 3D array
int[][] square; //"the value of the local variable square is never used"
//create 64 instances of ChessSquare and assign each square as an element in the 3D array
for (int i = 0; i < 8; i++){
for(int l = 0; l < 8; l++){
ChessSquare chessSquare = new ChessSquare(i, l);
square = new int[i][l];
frame.add(chessSquare);
}
}
}
public static void main(String[] args) throws IOException{
new ChessBoard();
}
}
先谢谢你
当我更改帧时,请注意。添加(棋盘格);添加(棋盘格);(如下所示)警告消失,但随后出现错误“ChessSquare无法解析为变量”
棋盘:
import java.awt.GridLayout;
import java.io.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
import Logic.ChessSquare;
public class ChessBoard {
//chess board constructor
public ChessBoard() throws IOException {
//create grid and grid dimensions
GridLayout grid = new GridLayout(8,8);
//create frame and set specifications of frame
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setLayout(grid);
frame.pack();
frame.setTitle("I should have started this sooner");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//create 64 instances of ChessSquare and assign each square as an element in the 3D array
for (int i = 0; i < 8; i++){
for(int l = 0; l < 8; l++){
ChessSquare chessSquare = new ChessSquare(i, l);
int[][] square = new int[i][l];
frame.add(chessSquare);
}
}
}
public static void main(String[] args) throws IOException{
new ChessBoard();
}
}
导入java.awt.GridLayout;
导入java.io.*;
导入javax.swing.ImageIcon;
导入javax.swing.JFrame;
导入javax.swing.JButton;
导入Logic.ChessSquare;
公共类棋盘{
//棋盘构造器
公共棋盘()抛出IOException{
//创建轴网和轴网标注
GridLayout grid=新的GridLayout(8,8);
//创建框架并设置框架的规格
JFrame=新JFrame();
frame.setVisible(true);
框架布局(网格);
frame.pack();
setTitle(“我应该早点开始”);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//创建64个棋盘格实例,并将每个棋盘格指定为三维阵列中的一个元素
对于(int i=0;i<8;i++){
对于(int l=0;l<8;l++){
ChessSquare ChessSquare=新的ChessSquare(i,l);
整数[][]平方=新整数[i][l];
帧。添加(棋盘格);
}
}
}
公共静态void main(字符串[]args)引发IOException{
新棋盘();
}
}
好的,从顶部看,您的“导入从未使用过”,这意味着在该文件中,您不使用这些导入,因此可以安全地删除它们,除非您打算在该文件中使用它们,否则这通常发生在您使用eclipse导入项目时,但随后决定不使用它。(即删除)
其次,正如我在编辑之前所说的,gridlayout并没有添加到jframe中
第三,您的square表示“从未使用该值”,但这是eclipse的一个错误,您实际上正在使用它,它只是说,因为它是在for循环内部初始化的
在上一个框中,添加chesssquare
而不是类名称chesssquare
这可能是我目前能做的最好的了。玩得开心
编辑:
另外,您的JButton按钮也有同样的问题,您没有在其他任何地方使用它:)好的,从顶部看,您的“导入从未使用”,意味着在该文件中,您不使用这些导入,因此可以安全地删除它们,除非您打算在该文件中使用它们,这通常发生在使用eclipse导入项目时,但随后决定不使用它。(即删除)
其次,正如我在编辑之前所说的,gridlayout并没有添加到jframe中
第三,您的square表示“从未使用该值”,但这是eclipse的一个错误,您实际上正在使用它,它只是说,因为它是在for循环内部初始化的
在上一个框中,添加chesssquare
而不是类名称chesssquare
这可能是我目前能做的最好的了。玩得开心
编辑:
您的JButton按钮也有同样的问题,您没有在其他任何地方使用它:)自从我写Swing以来,已经有一段时间了,但我会尝试一下
GridLayout grid = new GridLayout(8,8); //"the value of the local variable grid is never used"
构建GridLayout
是不够的,还必须使用它。例如,frame.setLayout(网格)代码>
事实并非如此。您可能应该删除这一行以及初始化它64次的错误行
JButton button = new JButton(new ImageIcon(buttonIcon)); //"the value of the local variable button is never used"
ChessSquare
是一个JButton
。您不需要在此处创建另一个。相反,您应该调用类似于this.setIcon(newimageicon(buttonIcon))代码>
再说一遍,我的秋千有点生锈了,但我猜这些答案很接近。自从我写了《秋千》之后,已经有一段时间了,但我要试一试
GridLayout grid = new GridLayout(8,8); //"the value of the local variable grid is never used"
构建GridLayout
是不够的,还必须使用它。例如,frame.setLayout(网格)代码>
事实并非如此。您可能应该删除这一行以及初始化它64次的错误行
JButton button = new JButton(new ImageIcon(buttonIcon)); //"the value of the local variable button is never used"
ChessSquare
是一个JButton
。您不需要在此处创建另一个。相反,您应该调用类似于this.setIcon(newimageicon(buttonIcon))代码>
同样,我的Swing有点生锈,但我猜答案很接近。谢谢你澄清这些事情:)虽然我仍然不明白为什么当我运行框架时会出现,但是没有任何按钮,所以我只有一个空白框架。好的,这是因为你正在将jframe设置为可见。您需要将gridlayout添加到它:)。所以:执行frame.add(gridlayout);,但是您还需要首先将项目添加到gridlayout中。我希望这能有所帮助,我知道编程有时很难掌握窍门,但一旦你掌握了,那就太棒了!哈,我明白你的意思了,哈哈,乔希,他是对的,它没有被使用,但是eclipse会犯那个错误。但需要注意的是,“未使用”部分是一个提示,而不是在gridlayout中添加的errorCheers,只是没有得到如何使按钮显示的线索。当我在一个更简单的版本中添加JButtons时,JButtons看起来没有问题,但是现在我使用ChessSquare作为我的按钮,我认为
int[][] square; //"the value of the local variable square is never used"
JButton button = new JButton(new ImageIcon(buttonIcon)); //"the value of the local variable button is never used"