执行程序时不显示JLabel



我不知道发生了什么,我已经尝试了很多事情来解决这个问题。我知道这听起来是个愚蠢的错误,但这已经花了我一些时间。下面是代码。如果有人知道该怎么办,请告诉我。

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;
    }
}
  1. 不要使用空布局。Swing是为布局管理器设计的。
  2. 添加所有的组件到框架之前使框架可见。

最新更新