JFrame 屏幕替换问题



我遇到的问题是当我尝试用第二个屏幕替换主屏幕时,它会删除旧屏幕但卡在新屏幕中。如果我最大化屏幕,我可以看到新的面板。我的另一个问题是如何在不使用我必须做的所有填充面板的情况下占用网格布局中的空间。代码如下:

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);
}
}

任何帮助都值得赞赏:)

简短

的回答是在框架上调用revalidate

更好的答案是使用 CardLayout ,它旨在完全完成您要做的事情......

至于你的第二个问题,它不能用GridLayout来完成,事实上,你可能会发现即使使用GridBagLayout也很难实现,这使您可以控制虚拟网格中组件的放置。

您可能能够做的是使用空面板填充网格,将它们保留在某种矩阵查找(即getComponentAt(gridx, gridy))中,然后使用这些面板来放置组件。

所以,例如。 如果要在网格2x3放置面板,只需查看该网格位置的面板并将组件放在其上即可。

ps- 当我考虑时,您可能还需要revalidaterepaint如果revalidate不起作用......

最新更新