2013-09-27 73 views
0

我遇到的問題是當我嘗試用第二個替換主屏幕時,它刪除舊的,但堅持放置在新的。如果我最大化屏幕,我可以看到新的面板。我的另一個問題是如何在網格佈局中佔用空間,而不使用我必須做的所有填充jpanel。代碼如下:JFrame Screen替換問題

package home.personalprojects.jordan.ArrayGame; 

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class GuiMain extends JFrame 
{ 

private JButton Inventory, Store, Up, Down, Left, Right, Move, GameInfo, Back = new JButton("Back"); 
private JLabel pstats, roominfo; 
private JPanel Filler1,Filler2,Filler3,Filler4,Filler5,Filler6,Filler7,Filler8,Filler9,Filler10,Filler11; 
private JPanel Controls, Main = new JPanel(), BackPanel; 

PlayerTraits pt = new PlayerTraits(); 
Rooms rm = new Rooms(); 

public GuiMain(){ 
    super("Dungeon Crawler v 0.0.1 Created By: Jordan Savage"); 
    Main.setLayout(new GridLayout(4,3)); 

    //mainbuttons 
    Inventory = new JButton("Inventory"); 
    Inventory.setToolTipText("Gives you access to all of your items and lets  you manage them"); 

    Store = new JButton("Store"); 
    Store.setToolTipText("The marketplace where you can buy and sell items such as swords and armor"); 

    Move = new JButton("Move"); 
    Move.setToolTipText("Choose where you want to move next"); 

    GameInfo = new JButton("Game Information and Settings"); 
    GameInfo.setToolTipText("All the info for the game including instructions, version info, and settings"); 

    //main labels 
    pstats = new JLabel(pt.name + ": " + pt.gold + " Gold, " + pt.health + " Health, and Level is " + pt.lvl); 
    roominfo = new JLabel("You are at: (" + pt.x + "," + pt.y + ") In room: " + rm.name); 

    //fillers for grid layout 
    Filler1 = new JPanel();Filler2 = new JPanel();Filler3 = new JPanel();Filler4 = new JPanel();Filler5 = new JPanel();Filler6 = new JPanel();Filler7 = new JPanel();Filler7 = new JPanel();Filler8 = new JPanel();Filler9 = new JPanel();Filler10 = new JPanel();Filler11 = new JPanel(); 

    //action listeners 
    Move.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      ControlScheme(); 
      BackToMain(); 
      getContentPane().removeAll(); 
      getContentPane().add(Controls, BorderLayout.CENTER); 
      getContentPane().add(BackPanel, BorderLayout.SOUTH); 
      getContentPane().doLayout(); 
      update(getGraphics()); 
     } 
    }); 

    Back.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      getContentPane().removeAll(); 
      getContentPane().add(Main); 
      getContentPane().doLayout(); 
      update(getGraphics()); 
     } 
    }); 

    Main.add(Inventory); 
    Main.add(Filler1); 
    Main.add(Store); 
    Main.add(Filler2); 
    Main.add(pstats); 
    Main.add(Filler3); 
    Main.add(Filler4); 
    Main.add(roominfo); 
    Main.add(Filler5); 
    Main.add(Move); 
    Main.add(Filler6); 
    Main.add(GameInfo); 
    add(Main); 
} 

public void BackToMain(){ 
    BackPanel = new JPanel(); 
    BackPanel.setLayout(new FlowLayout()); 
    BackPanel.add(Back); 
} 


public void ControlScheme(){ 
    Up = new JButton("Up"); 
    Down = new JButton("Down"); 
    Left = new JButton("Left"); 
    Right = new JButton("Right"); 

    Controls = new JPanel(); 
    Controls.setLayout(new GridLayout(3,3)); 

    Controls.add(Filler7); 
    Controls.add(Up); 
    Controls.add(Filler8); 
    Controls.add(Left); 
    Controls.add(Filler9); 
    Controls.add(Right); 
    Controls.add(Filler10); 
    Controls.add(Down); 
    Controls.add(Filler11); 

} 

public static void main(String[] args) 
{ 

    GuiMain gm = new GuiMain(); 
    gm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    gm.setSize(800, 600); 
    gm.setVisible(true); 
    gm.setLocationRelativeTo(null); 

} 

} 

任何幫助表示讚賞:)

+0

請嘗試將您的編碼風格適應【JAVA命名約定(HTTP:// en.wikipedia.org/wiki/Naming_convention_%28programming%29#Java)否則你會混淆我們,很難知道什麼是類,什麼是變量名。 –

+0

感謝您的提示。只是將它們改爲合適的外殼。 – user2280906

回答

1

簡短的回答是叫revalidate在框架上。

更好的答案是使用CardLayout,它的目的是做什麼你試圖做...

關於你的第二個問題,它不能與GridLayout做,其實,即使使用GridBagLayout,也可能難以實現,從而可以控制虛擬網格中組件的位置。

你可以做的是使用填充網格的空面板,保持他們在某種矩陣查找(即getComponentAt(gridx, gridy)),並使用它們來放置你的組件。例如,

因此,例如。如果您想在網格2x3上放置面板,您只需在該網格位置查看面板並將組件放置在該面板上即可。

PS-雖然我想,你可能還需要repaintrevalidate如果revalidate沿着不工作...

+0

謝謝!現在我只是稱重新驗證,但我會明確地嘗試一個CardLayout。重新驗證獨自工作。 – user2280906