2017-03-17 35 views
0

谢谢你为什么框架不显示的建议。我曾内容用下面的代码现在显示(在什么也没有main方法的子类进行扩展,虽然这已经是扩展JPanel的主要方法的类),但不正确:空的Java JFrame

case start: 
    JPanel mapFrame = new JPanel(); 
    mapFrame.setPreferredSize(new Dimension(950, 950)); 
    mapFrame.setBackground(Color.CYAN); 
    mapFrame.setVisible(true); 
    mapFrame.add(new Main()); 

    JPanel statBar = new JPanel(); 
    statBar.setBackground(Color.BLACK); 
    statBar.setPreferredSize(new Dimension(400, 950)); 

    JFrame fullBox = new JFrame(); 
    fullBox.getContentPane().setLayout(new BorderLayout()); 
    fullBox.setPreferredSize(new Dimension(1350, 950)); 

    fullBox.getContentPane().add(statBar, BorderLayout.EAST); 
    fullBox.getContentPane().add(mapFrame, BorderLayout.WEST); 
    fullBox.setResizable(true); 
    fullBox.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    fullBox.setVisible(true); 
    fullBox.pack(); 

以前,我有

case start: 
    JFrame mapFrame = new JFrame(); 
    mapFrame.pack(); 
    mapFrame.setSize(1350, 1000); 
    mapFrame.setResizable(false); 
    mapFrame.setLocationRelativeTo(null); 
    mapFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    mapFrame.add(new Main()); 
    mapFrame.setVisible(true); 

我的涂料方法:下面的代码,而不是上面我包括(在主方法的子类),将其在显示的背景图像和一个播放器,其位置可以正常使用与键输入更新对于后台和播放器如下(在扩展JPanel类中):

public void paint(Graphics g){ 
    super.paint(g); 
    Graphics2D g2d = (Graphics2D) g; 
    g2d.drawImage(getBackgroundImage(), 0, 0, this); 
    ((Penguin)player).draw(g2d); 

public BufferedImage getBackgroundImage(){ 
    ImageIcon i = new ImageIcon(getClass().getResource(background)); 
    Image image = i.getImage(); 
    BufferedImage scaledImage = new BufferedImage(950, 950, BufferedImage.TYPE_INT_ARGB); 
    Graphics2D g2 = scaledImage.createGraphics(); 
    g2.drawImage(image, 0, 0, 950, 950, null); 
    g2.dispose(); 
    return scaledImage; 
} 

fullBox Frame需要连续重新绘制,因为mapFrame包含玩家角色和要收集的点,statBar稍后将包含也将更新时间和点的文本。我尝试删除mapFrame的背景颜色,它只是JPanel的默认颜色。当我包括颜色时,有一个小的白色盒子被固定在青色以上的位置 - 这是一种奇怪的东西。

screenshot of execution of current code

+1

开始通过调用调用setVisible最后,添加完所有的组件框架 – MadProgrammer

+0

后还,因为你不使用无需扩展'JFrame'是'JFrame'任何地方(至少在所示的代码中)。另请参阅[使用多个JFrames,好/坏惯例?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice)这不好。为了更好地提供帮助,请尽快发布有效的[mcve] – Frakcool

+0

在JFrames上,当您向其添加可见组件时,应该通过内容窗格(例如, 'fullBox.getContentPane()。add(mapFrame,BorderLayout.WEST);' –

回答

0
class MenuActionListener implements ActionListener { 
    @Override 
    public void actionPerformed(ActionEvent e) { 

     switch (e.getActionCommand()) { 
      case startGame: 
       JFrame fullBox = new JFrame(); 
       fullBox.getContentPane().setLayout(new BorderLayout()); 
       JPanel mapFrame = new JPanel(); 
       mapFrame.setSize(950, 950); 
       mapFrame.setVisible(true); 
       mapFrame.add(new Main()); 
       fullBox.getContentPane().add(mapFrame, BorderLayout.WEST); 
       JPanel statBar = new JPanel(); 
       statBar.setSize(400, 950); 

       fullBox.getContentPane().add(statBar, BorderLayout.EAST); 

       fullBox.setResizable(false); 
       fullBox.setLocationRelativeTo(null); 
       fullBox.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       fullBox.pack(); 
       fullBox.setVisible(true); 

       break; 
      case loadLog: 
       System.out.println("load game"); 
       break; 
      case aboutGame: 
       System.out.println("about game"); 
       break; 
      case exitGame: 
       System.out.println("exit game"); 
       System.exit(0); 
+0

谢谢!它现在显示两个面板,但它们没有被正确绘制。 – gkdub