2013-02-25 120 views
1

我不知道爲什麼這不起作用。我實現了一個ItemListener來檢查SplashScreen上的繼續按鈕是否被點擊,然後切換到MainMenu面板。當我運行代碼時,我得到第一個面板上的背景圖片和按鈕,然後當我點擊按鈕時什麼也沒有發生。如何在我的JFrame中的JPanel之間切換

這裏是我的窗口類

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

當你cli時,ItemListener將不起作用ck繼續JButton。使用ActionListener代替:

continueButton.addActionListener(new ActionListener() { 

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

還可以考慮使用CardLayout#next進行導航。

+0

非常感謝你@Reimeus完美的工作。我仍然習慣於Swing圖形。 – mstep91 2013-02-26 05:37:58

+0

關於如何讓我的按鈕位於我的圖片@Reimeus上的任何建議?到目前爲止,他們只是推到了圖片的右側。 – mstep91 2013-02-26 05:49:55