Max Max - 27 days ago 6
Java Question

JLabel doesnt appear when executing the program

I do not know what's going on, I already tried many things to fix this. I know it sounds to be a silly mistake, but it already took me some time. Below is the code. if anyone knows what to do please tell me.

public class Window extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;

private static Toolkit toolkit = Toolkit.getDefaultToolkit();

public static final int WIDTH = (int) toolkit.getScreenSize().getWidth();
public static final int HEIGHT = (int) toolkit.getScreenSize().getHeight();

public Dimension windowDimension = new Dimension(WIDTH, HEIGHT);

JButton buttonStartGame = new JButton();
JButton buttonTechTreeOverview = new JButton();
JButton buttonOptions = new JButton();
JButton buttonExit = new JButton();

ImageIcon buttonStartGameIcon = new ImageIcon(getClass().getResource("buttonStartGameIcon.png"));
ImageIcon buttonTechTreeOverviewIcon = new ImageIcon(getClass().getResource("buttonTechTreeOverviewIcon.png"));
ImageIcon buttonOptionsIcon = new ImageIcon(getClass().getResource("buttonOptionsIcon.png"));
ImageIcon buttonExitIcon = new ImageIcon(getClass().getResource("buttonExitIcon.png"));

JLabel backLabel = new JLabel();

public Window(String name) {

setLayout(null);
setSize(windowDimension);
setUndecorated(true);
setTitle(name);
setVisible(true);
setResizable(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);

modifyCompontes();

startMenu();

System.out.println("Thread is ok");

}

public void startMenu() {

backLabel.add(buttonExit);
backLabel.add(buttonStartGame);
backLabel.add(buttonTechTreeOverview);
backLabel.add(buttonOptions);

add(backLabel);
}

public void StartGame() {

}

public void TechTreeOverviewStart() {

}

public void gameOptions() {

}

private void modifyCompontes() {

try {
backLabel.setIcon(new ImageIcon(
imageResizer(ImageIO.read(getClass().getResourceAsStream("fundo.png")), WIDTH, HEIGHT)));
} catch (IOException e) {
e.printStackTrace();
}
backLabel.setBounds(0, 0, WIDTH, HEIGHT);

buttonStartGame.setIcon(buttonStartGameIcon);
buttonStartGame.setBounds(100, 100, 200, 100);
buttonStartGame.addActionListener(this);

buttonTechTreeOverview.setIcon(buttonTechTreeOverviewIcon);
buttonTechTreeOverview.setBounds(100, 250, 200, 100);
buttonTechTreeOverview.addActionListener(this);

buttonOptions.setIcon(buttonOptionsIcon);
buttonOptions.setBounds(100, 400, 200, 100);
buttonOptions.addActionListener(this);

buttonExit.setIcon(buttonExitIcon);
buttonExit.setBounds(100, 550, 200, 100);
buttonExit.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {

if (e.getSource() == buttonStartGame) {
StartGame();
}

if (e.getSource() == buttonTechTreeOverview) {
TechTreeOverviewStart();
}

if (e.getSource() == buttonOptions) {
gameOptions();
}

if (e.getSource() == buttonExit) {
System.exit(0);
}
}

public Image imageResizer(Image image, int width, int height) {
BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = resizedImage.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(image, 0, 0, width, height, null);
g.dispose();
return resizedImage;
}

}

Answer
  1. Don't use a null layout. Swing was designed to be used with layout managers.
  2. Add all the component to the frame BEFORE making the frame visible.