Your constructor is in reality a method with a constructor name.
You should switch from this:
public void StartScreen() {
// Code
}
to this:
public StartScreen() {
// Code
}
Frage
I'm trying to open one of my classes that involve a JFrame using a simple launcher:
public class Launcher {
public static void main(String[] args) {
new StartScreen();
}
}
This launcher is meant to launch the class:
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.image.BufferedImage;
public class StartScreen extends JFrame {
private static JFrame frame;
GameKeyboard GK;
boolean gamePlay = false;
boolean gameQuit = false;
boolean gameTwoPlayer = false;
String option;
//set dimension of window and buttons
public final int screenWidth = 800; // Width of window
public final int screenHeight = screenWidth / 12 * 9; // Height of window
private static Graphics gr;
//store images
private static Image background;
private static Image play;
private static Image twoPlayer;
private static Image quit;
private static Image playSelected;
private static Image twoPlayerSelected;
private static Image quitSelected;
public void StartScreen() {
frame = new JFrame();
setSize(screenWidth, screenHeight);
frame.add(this, BorderLayout.CENTER);
// frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
frame.setTitle("Space Wars Menu");
frame.setLocationRelativeTo(null);
getContentPane().setLayout(null);
BufferedImage canvas=new BufferedImage(920,720,BufferedImage.TYPE_INT_ARGB);
gr=canvas.getGraphics();
JLabel label=new JLabel(new ImageIcon(canvas));
frame.add(label);
MenuKeyboard.initialise();
//load images
background = GameImage.loadImage("Images//background.jpg");
play = GameImage.loadImage("Images//play.png");
playSelected = GameImage.loadImage("Images//playSelected.png");
twoPlayer = GameImage.loadImage("Images//twoPlayer.png");
twoPlayerSelected = GameImage.loadImage("Images//twoPlayerSelected.png");
quit = GameImage.loadImage("Images//quit.png");
quitSelected = GameImage.loadImage("Images//quit.png");
//draw images
gr.drawImage(background, 0, 0, null);
gr.drawImage(playSelected, 160, -50, null);
gr.drawImage(twoPlayer, 160, 150, null);
gr.drawImage(quit, 160, 250, null);
int specialKey = MenuKeyboard.getSpecialKey();
while(gamePlay == false)
{
if (MenuKeyboard.getSpecialKey() == 40) //if down pressed
{
gr.drawImage(twoPlayerSelected, 160, 150, null);
gr.drawImage(play, 160, -50, null);
}
}
}
}
The class is not finished yet obviously but I was just testing if it would open up a window and load all the images, but no window even gets opened.. despite the classes actually being linked. Any help?
Lösung
Your constructor is in reality a method with a constructor name.
You should switch from this:
public void StartScreen() {
// Code
}
to this:
public StartScreen() {
// Code
}
Andere Tipps
Your while loop runs continuously which is stepping on the Swing event thread effectively freezing it and preventing Swing from drawing anything. Solution: get rid of that while loop.
Instead use Key Bindings to trap specific key presses. If you need a game loop, use a Swing Timer.
Tutorials:
Edit
Your StartScreen class has no constructor. Get rid of the void return type in your pseudo constructor.
Other issues: