【发布时间】:2020-11-15 16:07:51
【问题描述】:
我最近为我的第一个棋盘游戏创建了我的第一个棋盘,并在我的 JPanel 上以多种方式绘制棋盘。我最终决定使用 Label,但由于我需要将图片添加到该标签,我不得不将其更改为 JLabel。语法正确,但 Jlabels 不显示。 我尝试清理我的绘画方法,它开始显示,但标签的背景没有改变。
我尝试将代码放入我的绘制方法中,但没有成功。 我还尝试在我的 draw 方法之后将容器添加到框架中,但它也不起作用。
以下是我的代码的一些重要部分:
////////////////////////////////////
//The constructor, Creates the frame.
////////////////////////////////////
public SecondFrame() {
setTitle("Counter Strike");
setIconImage(Toolkit.getDefaultToolkit().getImage(SecondFrame.class.getResource("/cs/resources/icon2.png")));
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(150, 10, WIDTH, HEIGHT);
contentPane = new JPanel();
//contentPane.setBackground(Color.CYAN);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGap(0, 984, Short.MAX_VALUE)
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGap(0, 662, Short.MAX_VALUE)
);
contentPane.setLayout(gl_contentPane);
//Starting the game.
start();
//Initializing the label "boardParts" 2D array.
frame();
}
//////////////////
//Draws the board
//////////////////
public void frame() {
boardParts=new JLabel[rows][columns];
for(int i=0;i<rows;i++)
for(int j=0;j<columns;j++) {
boardParts[i][j]=new JLabel();
boardParts[i][j].setBounds(100*i+100, 100*j+100, tilesize, tilesize);
boardParts[i][j].setBorder(new LineBorder(new Color(0, 0, 0)));
if(randomBarrier())
boardParts[i][j].setIcon(new ImageIcon(FirstFrame.class.getResource("/cs/resources/boundIcon.png")));
else
boardParts[i][j].setBackground(Color.yellow);
contentPane.add(boardParts[i][j]);
}
}
我还在我的另一个类中创建了这个类的一个新对象,当我运行它时,它显示错误大约 1 秒,然后将它们擦除,所以我不知道这些错误是什么原因。
这是我的简化代码:
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.ActionEvent;
public class Frame1 {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame1 window = new Frame1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Frame1() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.setVisible(false);
SecondFrame frame2 = new SecondFrame();
frame2.setVisible(true);
}
});
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(42)
.addComponent(btnNewButton)
.addContainerGap(303, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
.addContainerGap(183, Short.MAX_VALUE)
.addComponent(btnNewButton)
.addGap(56))
);
frame.getContentPane().setLayout(groupLayout);
}
}
package hello;
import java.awt.Color;
import java.awt.Toolkit;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class SecondFrame extends JFrame implements Runnable{
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private static int WIDTH=1000,HEIGHT=700;
private static int boardWidth=500,boardHeight=500; //The width and height of the game board.
private Thread thread;
private boolean isRunning;
private BoardParts barriers;
private int rows=8,columns=5,tilesize=100;
private JLabel[][] boardParts;
private boolean[][] notBarrier;
////////////////////
//Creates the frame.
////////////////////
public SecondFrame() {
setTitle("Counter Strike");
setIconImage(Toolkit.getDefaultToolkit().getImage(SecondFrame.class.getResource("/cs/resources/icon2.png")));
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(150, 10, WIDTH, HEIGHT);
contentPane = new JPanel();
contentPane.setBackground(Color.CYAN);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGap(0, 984, Short.MAX_VALUE)
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGap(0, 662, Short.MAX_VALUE)
);
contentPane.setLayout(gl_contentPane);
//Starting the game.
start();
//Initializing the label "boardParts" 2D array.
frame();
}
//////////////////
//Draws the board
//////////////////
public void frame() {
boardParts=new JLabel[rows][columns];
notBarrier=new boolean[rows][columns];
for(int i=0;i<rows;i++)
for(int j=0;j<columns;j++) {
boardParts[i][j]=new JLabel();
boardParts[i][j].setBounds(100*i+100, 100*j+100, tilesize, tilesize);
boardParts[i][j].setBorder(new LineBorder(new Color(0, 0, 0)));
if(randomBarrier()) {
boardParts[i][j].setIcon(new ImageIcon(Frame1.class.getResource("/cs/resources/boundIcon.png")));
notBarrier[i][j]=false;
}
else {
boardParts[i][j].setBackground(Color.yellow);
notBarrier[i][j]=true;
}
contentPane.add(boardParts[i][j]);
}
}
///////////////////////////////////////////////////////////////////////////
//finds a random place for the barrier objects in the beginning of the game.
///////////////////////////////////////////////////////////////////////////
public static boolean randomBarrier() { //Should put all the parts to this method to see if the are barrier material or not.
int row = WIDTH/100;
int column = HEIGHT/100;
int min = 0;
int max = row*column;
double random = Math.random();
if(random<0.4)
return true;
else if(random>=0.4)
return false;
return true;
}
//////////////////
//Starts the game.
/////////////////
public void start() {
isRunning = true;
thread = new Thread(this);
thread.start();
}
////////////////
//Stops the game.
///////////////
public void stop() {
isRunning = false;
try {
thread.join();
} catch (InterruptedException e) {
System.out.println("An error occured...");
e.printStackTrace();
}
}
public void tick() {
}
@Override
public void run() {
while(isRunning) {
tick();
repaint();
}
}
}
package hello;
public class BoardParts {
}
【问题讨论】:
-
为了快速获得帮助,请发布一个简短的演示代码,它能够重现问题或发布您遇到的运行时错误。
-
我无法准确地看到我的运行时错误。它只显示一秒钟,然后控制台被清理。
-
我不能减少太多代码,但我会尽力而为。谢谢你提醒我。
-
我不能减少太多代码 - 当然可以。所有与线程和停止/启动游戏相关的代码都与所述问题无关。
randomBarrier()只需要一行代码。 标签的背景不会改变。 - 您应用程序的代码与更改标签的背景无关。对于 MRE,您只需要一个带有单个 JLabel 的 JFrame。您不需要创建两个框架或 100 个 JLabel 来测试它。重点是忘记你的应用程序,从头开始创建一个 MRE 来演示问题。
标签: java swing runtime-error jpanel jlabel