Java Label hat komischen text

SchalliLP

Aktives Mitglied
Hallo,
Ich habe mal ein Pong programmiert und wollte nun Punkteanzeigen hinzufügen.
allerdings zeigt das label meinePunkte folgenden Text anstatt Punkte: 0 an:

Punkte: javax.swing.JLabel{,9,9,500x100,alignmentX=0.0,alig...

Hat jemand ne Ahnung woran das liegt?

[Java]
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import java.awt.event.KeyAdapter;
import java.awt.Font;

public class Pong extends JFrame {

private JPanel contentPane;
ImageIcon Ball = new ImageIcon("Ball.png");
ImageIcon Pannel = new ImageIcon("pannel.png");
JLabel ball = new JLabel(Ball);
JLabel gegnerPannel = new JLabel(Pannel);
JLabel meinPannel = new JLabel(Pannel);

int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
int HEIGTH = Toolkit.getDefaultToolkit().getScreenSize().height;

int xPosBall = WIDTH / 2;
int yPosBall = HEIGTH / 2;
int yPosGegner = HEIGTH / 2;
int yPosIch = HEIGTH / 2;
int ySpeed = 3;
int xSpeed = 5;
int punkteIch = 0;
int punkteGegner = 0;

boolean wPressed = false;
boolean sPressed = false;

Timer Timer;
private final JLabel meinePunkte = new JLabel("Punkte: "+punkteIch);
private final JLabel gegnerPunkte = new JLabel("Punkte: "+punkteGegner);

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {



try {
Pong frame = new Pong();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public Pong() {
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {

if (e.getKeyCode() == KeyEvent.VK_W) {

wPressed = true;

}
if (e.getKeyCode() == KeyEvent.VK_S) {

sPressed = true;

}

}
@Override
public void keyReleased(KeyEvent e) {


if (e.getKeyCode() == KeyEvent.VK_W) {

wPressed = false;

}
if (e.getKeyCode() == KeyEvent.VK_S) {

sPressed = false;

}
}
});

Timer = new Timer(20, new Bewegung());
Timer.start();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(0, 0, WIDTH, HEIGTH);
contentPane = new JPanel();
contentPane.setBackground(Color.BLACK);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
setUndecorated(true);

ball.setBounds(152, 113, 30, 30);
contentPane.add(ball);

gegnerPannel.setBounds(WIDTH - 50, yPosGegner / 2 - 170 / 2, 30, 170);
contentPane.add(gegnerPannel);
meinPannel.setBounds(50, yPosIch / 2 - 170 / 2, 30, 170);
contentPane.add(meinPannel);
meinePunkte.setFont(new Font("Tahoma", Font.PLAIN, 18));
meinePunkte.setForeground(Color.WHITE);
gegnerPunkte.setFont(new Font("Tahoma", Font.PLAIN, 18));
gegnerPunkte.setBounds(0, 0, 500, 100);

contentPane.add(meinePunkte);
meinePunkte.setBounds(WIDTH-300, 0, 500, 100);
gegnerPunkte.setForeground(Color.WHITE);

contentPane.add(gegnerPunkte);
}

public void Bewegung() {

if (yPosBall <= 0) {

ySpeed = 2;

}
if (yPosBall >= HEIGTH - 30) {

ySpeed = -2;

}

yPosBall = yPosBall + ySpeed;

}

public class Bewegung implements ActionListener {

@Override
public void actionPerformed(ActionEvent arg0) {

abprallen();
ballBewegen();
gegnerBewegen();
michBewegen();
punkt();
frame();

}

private void michBewegen() {

if (wPressed && sPressed) {
/*
* Da beide Tasten gepresst sind werden beide ignoriert
*/
} else if (wPressed) {

yPosIch = yPosIch - 3;

} else if (sPressed) {
yPosIch = yPosIch + 3;

} else {
/*
* Keine Tasten gedrückt
*/
}

if (yPosIch < 0) {

yPosIch = 0;

}
if (yPosIch > HEIGTH - 170 / 2) {

yPosIch = HEIGTH - 170 / 2;

}

meinPannel.setBounds(50, yPosIch, 30, 170);
}

private void frame() {
ball.setBounds(xPosBall, yPosBall, 30, 30);
meinePunkte.setText("Punkte: "+punkteIch);
gegnerPunkte.setText("Punkte: "+gegnerPunkte);

}

private void punkt() {
if (xPosBall < 20) {
punkteGegner++;
zurücksetzen();
} else if (xPosBall > WIDTH - 20) {
punkteIch++;
zurücksetzen();
}

}

private void zurücksetzen() {
xPosBall = WIDTH / 2;
yPosBall = HEIGTH / 2;
yPosGegner = HEIGTH / 2 - 170 / 2;
yPosIch = HEIGTH / 2 - 170 / 2;

gegnerPannel.setBounds(WIDTH - 50, yPosGegner - 170 / 2, 30, 170);
meinPannel.setBounds(50, yPosIch - 170 / 2, 30, 170);

ySpeed = (int) (10 * Math.random() - 5);
while (ySpeed < 2 && ySpeed > -2) {

ySpeed = (int) (6 * Math.random() - 3);

}

}

private void gegnerBewegen() {

if (yPosGegner + 100 < yPosBall) {

yPosGegner = yPosGegner + 3;

} else if (yPosGegner - 100 > yPosBall) {

yPosGegner = yPosGegner - 3;

}

if (yPosGegner < 0) {

yPosGegner = 0;

}
if (yPosGegner > HEIGTH - 170 / 2) {

yPosGegner = HEIGTH - 170 / 2;

}

gegnerPannel.setBounds(WIDTH - 50, yPosGegner - 170 / 2, 30, 170);

}

private void ballBewegen() {
yPosBall = yPosBall + ySpeed;
xPosBall = xPosBall + xSpeed;

}

private void abprallen() {
if (yPosBall < 0 || yPosBall > HEIGTH - 30) {

ySpeed = ySpeed * -1;

}

if (xPosBall < 80
&& (yPosBall+30 >= yPosIch && yPosBall <= yPosIch + 170)&&xPosBall>=75) {

xSpeed = xSpeed * -1;

}

if (xPosBall > WIDTH - 80
&& (yPosBall+30 >= yPosGegner && yPosBall <= yPosGegner-30 + 170)&&xPosBall>=WIDTH-75) {

xSpeed = xSpeed * -1;

}

}

}

}
[/code]
 
Das sieht so aus, als ob du toString vom Panel aufrufen würdest, was du allerdings nicht tust. 😀

Debug mal und guck nach, was in der Variable steht.
 
allerdings zeigt das label meinePunkte folgenden Text anstatt Punkte: 0 an:

Punkte: javax.swing.JLabel{,9,9,500x100,alignmentX=0.0,alig...

[Java]private void frame() {
ball.setBounds(xPosBall, yPosBall, 30, 30);
meinePunkte.setText("Punkte: "+punkteIch);
gegnerPunkte.setText("Punkte: "+gegnerPunkte);

}
[/code]

Das Problem liegt nicht beim Label "meinePunkte" sondern am Label "gegnerPunkte"!
Du fügst hier folgenden Text zusammen > "Punkte" + gegnerPunkte
gegnerPunkte ist in diesem Fall aber das JLabel selber und nicht die Integer Variable, durch die String Verkettung wird die "toString()" Methode des JLabels aufgerufen -> Ergebnis ist die obrige Ausgabe! 🙂
 
Zuletzt bearbeitet:

Zurück
Oben