0

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

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

}

}

感谢任何帮助:)

4

1 回答 1

1

简短的回答是调用revalidate框架。

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

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

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

所以,例如。如果您想在 grid 放置一个面板2x3,您只需查看该网格位置的面板并将您的组件放置在其上。

ps-虽然我在想,但如果不起作用,您可能还需要repaintafter ...revalidaterevalidate

于 2013-09-28T00:03:12.173 回答