2017-03-17 48 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