java - JFrame 屏幕更换问题

标签 java swing user-interface jframe refresh

我遇到的问题是,当我尝试用第二个屏幕替换主屏幕时,它会删除旧屏幕,但卡在新屏幕中。如果我最大化屏幕,我可以看到新的面板。我遇到的另一个问题是如何在网格布局中占用空间而不使用我必须做的所有填充 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);

}

}

感谢任何帮助:)

最佳答案

简短的答案是调用 revalidate在框架上。

更好的答案是使用 CardLayout ,它的设计目的正是为了完成您想要做的事情...

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

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

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

ps- 当我思考时,您可能还需要 repaint之后revalidate如果revalidate沿着不起作用...

关于java - JFrame 屏幕更换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19061710/

相关文章:

java - Java中try catch中的圆括号/括号()是什么

java - 可以在 java 中获取 mysql enum filed 的索引值吗?

java - 在此示例中如何从目标列表中获取所有值

javascript - Chrome 扩展用户界面

java - java中显示复数根的二次方法

java - 比较方法违反了排序方法中的一般契约

java - 滚动 JPanel

java - 如何使用 JFileChooser 将 .txt 文件保存在所需位置

user-interface - Hue 安装在 vanilla hadoop 上

user-interface - 将我的 Office 加载项配置页放在哪里?