如何在我的JFrame中切换JPanels

时间:2013-02-25 19:53:31

标签: java swing user-interface jframe jpanel

我不知道为什么这不起作用。我已经实现了一个ItemListener来检查是否在SplashScreen上单击了continue按钮然后切换到MainMenu面板。当我运行代码时,我得到第一个带有背景图像和按钮的面板,然后当我点击按钮时没有任何反应。

这是我的Window类

package edu.ycp.cs.Main;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Window implements ItemListener{
private static final long serialVersionUID = 1L;
JPanel cards; // a panel that uses CardLayout
final static String SPLASHSCREEN = "SplashScreen";
final static String MAINMENU = "MainMenu";

public void addComponentToWindow(Container pane) {
    // Put the JComboBox in a JPanel to get a nicer look.
    JPanel gameWindow = new JPanel(); // use FlowLayout

    // Create the "cards".
    JPanel card1 = new JPanel();
    JButton continueButton = new JButton("Continue");
    continueButton.addItemListener(this);
    card1.add(new SplashScreen());
    card1.add(continueButton);

    JPanel card2 = new JPanel();
    card2.add(new MainMenuScreen());

    cards = new JPanel(new CardLayout());
    cards.add(card1, SPLASHSCREEN);
    cards.add(card2, MAINMENU);

    pane.add(gameWindow, BorderLayout.PAGE_START);
    pane.add(cards, BorderLayout.CENTER);
}

public void itemStateChanged(ItemEvent evt) {
    CardLayout cl = (CardLayout) (cards.getLayout());
    cl.show(cards, (String) evt.getItem());
}
}

这是我的启动画面代码 MainMenu代码与不同的图像文件完全相同

package edu.ycp.cs.Main;

import javax.imageio.ImageIO;
import javax.swing.*;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;

class SplashScreen extends JPanel {
private static final long serialVersionUID = 1L;

private BufferedImage background;

public SplashScreen() {
    try {
        background = ImageIO.read(new File("Logo.png"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public Dimension getPreferredSize() {
    return new Dimension(800, 600);
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (background != null) {
        int x = (getWidth() - background.getWidth());
        int y = (getHeight() - background.getHeight());
        g.drawImage(background, x, y, this);
    }

    Graphics2D g2d = (Graphics2D) g;
    g2d.setPaint(Color.white);
    g2d.drawString("Please wait...", getWidth() / 2, getHeight() * 3 / 4);
}
}

这是我的主要

package edu.ycp.cs.Main;

import javax.swing.JFrame;

public class Main {
    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                makeGUI();
            }
        });
    }

private static void makeGUI() {
    // Create and set up the window.
    JFrame frame = new JFrame("M&M Arcade");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create and set up the content pane.
    Window window = new Window();
    window.addComponentToWindow(frame.getContentPane());

    // Display the window.
    frame.pack();
    frame.setVisible(true);
}
}

1 个答案:

答案 0 :(得分:1)

点击“继续ItemListenerJButton无效”。改为使用ActionListener

continueButton.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        CardLayout cl = (CardLayout) (cards.getLayout());
        cl.show(cards, MAINMENU);
   }
});

还考虑使用CardLayout#next进行导航。

相关问题