Guten Tag.
Ich weiß, dass diese Frage schon oft gestellt wurde, aber die Antworten haben bei mir leider nicht funktioniert.
Das Programm soll ein Fenster öffnen, auf welchem ein Panel mit den Folgenden Objekten angezeigt wird. Zwei Buttons, ein Label und ein Bild. Bis dahin funktioniert auch alles. Aber wenn einer der Knöpfe gedrückt wird, soll das aktuelle Panel vom Frame gelöscht werden und ein neues Panel mit anderen Objekten bzw. Beschriftungen auf dem Frame erscheinen.
Das Problem ist, dass das aktuelle Panel zwar gelöscht wird, aber das neue einfach weiß ist, obwohl sich Objekte darauf befinden...
Das ist der Code:
Danke schon mal für die Antworten
Ich weiß, dass diese Frage schon oft gestellt wurde, aber die Antworten haben bei mir leider nicht funktioniert.
Das Programm soll ein Fenster öffnen, auf welchem ein Panel mit den Folgenden Objekten angezeigt wird. Zwei Buttons, ein Label und ein Bild. Bis dahin funktioniert auch alles. Aber wenn einer der Knöpfe gedrückt wird, soll das aktuelle Panel vom Frame gelöscht werden und ein neues Panel mit anderen Objekten bzw. Beschriftungen auf dem Frame erscheinen.
Das Problem ist, dass das aktuelle Panel zwar gelöscht wird, aber das neue einfach weiß ist, obwohl sich Objekte darauf befinden...
Das ist der Code:
Java:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.Color;
public class Dan {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Dan window = new Dan();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Dan() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
JPanel panel1 = new JPanel();
panel1.setLocation(0, 0);
panel1.setSize(1000, 650);
JPanel panel2 = new JPanel();
panel2.setLocation(0, 0);
panel2.setSize(1000, 650);
JPanel panel3 = new JPanel();
panel3.setLocation(0, 0);
panel3.setSize(1000, 650);
JPanel panel4 = new JPanel();
panel4.setLocation(0, 0);
panel4.setSize(1000, 650);
JPanel panel5 = new JPanel();
panel5.setLocation(0, 0);
panel5.setSize(1000, 650);
frame = new JFrame("Game of Life");
frame.setBounds(100, 100, 1000, 650);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.getContentPane().add(panel1);
panel1.setLayout(null);
//ERSTE FRAGE
JLabel feierBild = new JLabel(
new ImageIcon("C:\\Users\\Jonathan\\Desktop\\Spielprojekt\\encouragement night.jpg")
);
feierBild.setBackground(new Color(240, 240, 240));
feierBild.setBounds(199, 32, 596, 346);
panel1.add(feierBild);
JButton btnGut1 = new JButton("I talk to the brothers and make a plan to encourage her too.");
btnGut1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel1.removeAll();
frame.getContentPane().add(panel2);
panel2.setLayout(null);
frame.update(frame.getGraphics());
}
});
btnGut1.setBounds(62, 530, 390, 49);
panel1.add(btnGut1);
JButton btnSchlecht1 = new JButton("Nothing. It's not my responibility to take care of it.");
btnSchlecht1.setBounds(515, 530, 390, 49);
btnSchlecht1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel1.removeAll();
frame.getContentPane().add(panel3);
panel3.setLayout(null);
frame.update(frame.getGraphics());
}
});
panel1.add(btnSchlecht1);
JLabel text1 = new JLabel("<html>You are part of the australian campus minestry. The minestry is planning a sister <br> encouragement night. At that night every sister is getting a card from each brother.<br>You also heard of a sister from germany who is in Sydney since a couple of days. She will <br> come to the encouragement night but because she is just in australia for a couple of <br> days the brothers can't write cards for her. <br> What are you doing?");
text1.setBounds(259, 393, 517, 118);
panel1.add(text1);
//ZWEITE FRAGE (Panel2)
JLabel zeitZusammen = new JLabel(
new ImageIcon("C:\\Users\\Jonathan\\Desktop\\Spielprojekt\\encouragement night.jpg")
);
feierBild.setBackground(new Color(240, 240, 240));
feierBild.setBounds(199, 32, 596, 346);
panel2.add(zeitZusammen);
frame.update(frame.getGraphics());
JButton btnGut2 = new JButton("Ask her to go to breakfast on another day.");
btnGut1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel2.removeAll();
panel3.removeAll();
frame.getContentPane().add(panel4);
panel4.setLayout(null);
frame.update(frame.getGraphics());
}
});
btnGut2.setBounds(62, 530, 390, 49);
panel2.add(btnGut2);
JButton btnSchlecht2 = new JButton("Thank her for the time and go home.");
btnSchlecht2.setBounds(515, 530, 390, 49);
btnSchlecht2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel2.removeAll();
panel3.removeAll();
frame.getContentPane().add(panel5);
panel5.setLayout(null);
frame.update(frame.getGraphics());
}
});
panel2.add(btnSchlecht2);
//Text bei guter Antwort
JLabel text2Gut = new JLabel("<html>You are part of the australian campus minestry. The minestry is planning a sister <br> encouragement night. At that night every sister is getting a card from each brother.<br>You also heard of a sister from germany who is in Sydney since a couple of days. She will <br> come to the encouragement night but because she is just in australia for a couple of <br> days the brothers can't write cards for her. <br> What are you doing?");
text2Gut.setBounds(259, 393, 517, 118);
panel2.add(text2Gut);
//Text bei schlechter Antwort
JLabel text2Schlecht = new JLabel("<html>You are part of the australian campus minestry. The minestry is planning a sister <br> encouragement night. At that night every sister is getting a card from each brother.<br>You also heard of a sister from germany who is in Sydney since a couple of days. She will <br> come to the encouragement night but because she is just in australia for a couple of <br> days the brothers can't write cards for her. <br> What are you doing?");
text2Schlecht.setBounds(259, 393, 517, 118);
panel3.add(text2Schlecht);
}
}
Danke schon mal für die Antworten