我正在上三堂课。第一个(JFrame)称为Splash类(JPanel)。一段时间后,我希望将面板移除,并用Menu类中的另一个面板替换。

我的主类当前看起来像这样(init方法由同一类中的main方法调用)...

public void init() throws InterruptedException{
        setLayout(new BorderLayout());

        Menu menu = new Menu(this);
        Splash splash = new Splash(this);

        this.getContentPane().add(splash, BorderLayout.CENTER);
        validate();

        setVisible(true);

        Thread.sleep(1000);

        this.removeAll();

        invalidate();
        this.getContentPane().add(menu, BorderLayout.CENTER);
        revalidate();

        setVisible(true);

    }


Splash类看起来像这样...

public Splash(Survive survive) throws InterruptedException{
        Color c1 = new Color(255, 255, 255);
        this.setBackground(c1);

        JLabel jl1 = new JLabel();
        jl1.setIcon(new ImageIcon("res/splash.png"));
        this.add(jl1);

    }


菜单类如下所示...

public Menu(Survive survive){
        ImageIcon i1 = new ImageIcon("res/title.png");

        this.setLayout(new FlowLayout(FlowLayout.CENTER));
        this.setLayout(new GridBagLayout());

        Color c1 = new Color(96, 96, 96);
        this.setBackground(c1);

        JButton b0 = new JButton(i1);
        b0.setPreferredSize(new Dimension(800, 250));
        b0.setRolloverIcon(i1);
        b0.setOpaque(false);
        b0.setBorderPainted(false);
        gbc.gridx = 0;
        gbc.gridy = -2;
        this.add(b0, gbc);

        JButton b1 = new JButton("Singleplayer");
        b1.setPreferredSize(new Dimension(buttonWidth, buttonHeight));
        gbc.gridx = 0;
        gbc.gridy = 1;
        this.add(b1, gbc);

        JButton b2 = new JButton("Options");
        b2.setPreferredSize(new Dimension(buttonWidth, buttonHeight));
        gbc.gridx = 0;
        gbc.gridy = 2;
        this.add(b2, gbc);

        JButton b3 = new JButton("Credits");
        b3.setPreferredSize(new Dimension(buttonWidth, buttonHeight));
        gbc.gridx = 0;
        gbc.gridy = 3;
        this.add(b3, gbc);

        JButton b4 = new JButton("Options");
        b4.setPreferredSize(new Dimension(buttonWidth, buttonHeight));
        gbc.gridx = 0;
        gbc.gridy = 4;
        this.add(b4, gbc);

    }


所有这些的目的是使用摆动组件制作启动画面。我意识到有更简单的方法可以做到这一点,但是我发现这种方法可以满足我的需求。

任何建议将不胜感激!

最佳答案

使用一拍一拍javax.swing.Timer触发事件,并使用CardLayout在组件之间保持/交换。

10-07 15:46