Hallo, ich habe im folgenden Code, 3 Componente, die geadded werden sollen, aller dings taucht am Ende nur 1 auf, wieso?
das Frame und alles läuft wie geplant, doch am Ende erscheint nur der Button "Start"
das Frame und alles läuft wie geplant, doch am Ende erscheint nur der Button "Start"
Java:
package Start;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class MainMenu extends JFrame {
BufferedImage img;
Toolkit tk;
@SuppressWarnings("static-access")
Dimension d = tk.getDefaultToolkit().getScreenSize();
public MainMenu() {
Runnable gui = new Runnable() {
@Override
public void run() {
setSize(d);
setUndecorated(true);
JButton b1 = new JButton("Start");
add(b1);
JButton b2 = new JButton("Spiel Laden");
add(b2);
JButton b3 = new JButton("Exit");
add(b3);
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});
img = loadImage("F:/Project-Earth/Project-Earth.jpg");
setLayout(new FlowLayout());
setVisible(true);
}
};
// GUI must start on EventDispatchThread:
SwingUtilities.invokeLater(gui);
}
public static BufferedImage loadImage(String ref) {
BufferedImage bimg = null;
try {
bimg = ImageIO.read(new File(ref));
} catch (Exception e) {
e.printStackTrace();
}
return bimg;
}
public static void main(String[] args) {
new MainMenu();
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillRect(0, 0, d.width, d.height);
warte(2000);
g.setFont(new Font("SF Movie Poster", 1, 20));
g.setColor(Color.cyan);
g.drawString("Project_Earth", d.width / 2 - 20, d.height / 2);
warte(2000);
g.drawImage(img, 0, 0, d.width, d.height, this);
warte(2000);
}
public static void warte(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
}
}
}