我遇到的问题是,当我尝试用第二个主屏幕替换主屏幕时,它会删除旧屏幕,但卡在新屏幕上。如果我最大化屏幕,我可以看到新的面板。我遇到的另一个问题是如何在不使用我必须做的所有填充 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);
}
}
感谢任何帮助:)