我是一名编写 Frog 游戏的学生,当 Frog 与物体碰撞或到达终点区域时,分数会增加或生命会减少,然后 Frog 会返回到起始位置。此部分有效,并且在将它们输出到控制台时减量和增量工作,我尝试将变量传递给另一个 jpanel 并在那里显示它,但它不会更新并在文本字段中显示变量。
游戏面板
public GamePanel() {
super();
setFocusable(true);
addKeyListener(new KeyList());
System.out.println("GAME PANE FOCUS:" + this.isFocusOwner());
scores.setVisible(true);
lives = p.STARTLIVES;
scores.setCurrentLives(lives);
txtTest.setText("Hello");
txtTest.setVisible(true);
add(scores,new AbsoluteConstraints(0,550,50,800));
Boolean displayable = scores.isDisplayable();
System.out.println("Displayable" + displayable);
scores.setEnabled(false);
scores.revalidate();
scores.repaint();
scores.setVisible(true);
System.out.println("Displayable" + displayable);
car1.start();
car2.start();
car3.start();
car4.start();
Log1.start();
Log2.start();
Log3.start();
Log4.start();
Log5.start();
Log6.start();
Log7.start();
Log8.start();
//check for collisions
}
final public void winZone(Rectangle object){
if(myFrog.frogArea().intersects(object)){
currentScore = currentScore + 100;
System.out.println("current Score " + currentScore);
p.setScore(currentScore);
scores.myScore().setText("hello");
myFrog.lostLife();
}
分数面板
public class jplScores extends JPanel {
Properties p = new Properties();
int currentLives;
int i;
/** Creates new form jplScores */
public jplScores() {
initComponents();
}
public void setCurrentLives(int Lives){
currentLives = Lives;
}
public String getCurrentLives(){
String L = Integer.toString(currentLives);
return L;
}
public JTextField myScore(){
return txtScore;
}
目前,它将显示它们所在框架中的 jpanel,但我已尝试使其成为面板中的面板,但我无法让面板从游戏面板中显示。
任何帮助都会非常感谢
public FroggerGame() {
initComponents();
setFocusable(true);
//repaint();
// p.setHeight(550);
// p.setWidth(800);
// p.setLives(3);
// p.setScore(0);
PANELHEIGHT = p.getHeight();
PANELWIDTH = p.getWidth();
welcomePanel();
/*
Toolkit tool = Toolkit.getDefaultToolkit();
imgBackground = tool.getImage(imageBackground);
background = new ImageIcon(imgBackground);
//setDefaultCloseOperation(EXIT_ON_CLOSE);
*/
jps.myScore().addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
// txtScorePropertyChange(evt);
jps.myScore().setText(Integer.toString(gp.currentScore()));
System.out.println(Integer.toString(gp.currentScore()));
jps.getScore(gp.currentScore());
System.out.println(" main score " + gp.currentScore());
}
});
}
...
private void btnEnterActionPerformed(ActionEvent evt) {
welcomePanel.setVisible(false);
getContentPane().setLayout(new AbsoluteLayout());
getContentPane().add(gp,new AbsoluteConstraints(0,0,800,550));
getContentPane().add(jps,new AbsoluteConstraints(0,550,800,100));
//gp.setSize(800, 550);
// gp.setBounds(0, 0, 800, 550);
gp.setVisible(true);
gp.requestFocusInWindow();
jps.setVisible(true);
gp是游戏面板,jps是得分面板。
最佳答案
这实际上与“面板”或 Swing GUI 编码无关,而与将信息从一个类传递到另一个类的基本 OOPS 问题有关。解决此问题的一种方法是为您的分数面板提供一个公共(public)方法,例如
public void changeScore(int value) {
// in here add the new value to the currently
// displayed value and display the new value.
}
然后主类,即带有分数面板引用的主类,您可以调用此方法,如果分数要增加则传入 1,如果要减少则传入 -1。
我认为这是“推送”解决方案,其中一个类将信息推送到另一个类中。解决此问题的另一种方法是通过监听器,其中 Scores 类监听其他类的关键属性,然后在发生适当事件时更改自己的分数,这通常涉及使用 PropertyChangeListeners 或其他更特定于 Swing 的监听器类。有时这是一个更好的解决方案,但我认为它是一个稍微更先进的概念。
关于java - 尝试显示在一个 jpanel 中创建的变量并在另一个 jpanel 中显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10268757/