2016-11-12 51 views
0

我不知道发生了什么,我已经尝试了很多事情来解决这个问题。我知道这听起来很愚蠢,但已经花了我一些时间。以下是代码。如果有人知道该怎么做,请告诉我。执行程序时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
  1. 不要使用空布局。 Swing旨在与布局经理一起使用。
  2. 在使框架可见之前,将所有组件添加到框架。