2011-07-31 25 views
1

好吧,即時通訊工作在這個遊戲在Java稱爲8位嵌合體。我現在正在主菜單上工作,但是當我使用卡片佈局時,窗戶由於某種原因不會打開。這是一些代碼。Java CardLayout主菜單問題

import javax.swing.*; 
import java.awt.*; 

public class MainScreen extends JFrame{ 

String Title = "MainMenu"; 
MainMenuComp MMC = new MainMenuComp(); 
BreedingGround BGR = new BreedingGround(); 

public MainScreen() { 

    setTitle("8-bit Chimera "+Title); 
    setSize(800,600); 
    setResizable(false); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setVisible(true); 
    add(MMC); 
    add(BGR); 

} 

public static void main(String[] args){ 

    new MainScreen(); 
    } 
} 

這是主窗口

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class MainMenuComp extends JPanel implements ActionListener{ 

BreedingGround BGR = new BreedingGround(); 
ImageData ID = new ImageData(); 
Image TitleBg; 
Image Title; 
CardLayout CL; 
JButton Play; 

public MainMenuComp() { 

setLayout(new GridBagLayout()); 
GridBagConstraints GBC = new GridBagConstraints(); 
ImageIcon TitleData = new ImageIcon(ID.TitleSource); 
ImageIcon TitleBackGroundData = new ImageIcon(ID.TitleBackGroundSource); 
ImageIcon PlayData = new ImageIcon(ID.PlaySource); 
TitleBg = TitleBackGroundData.getImage(); 
Title = TitleData.getImage(); 
Play = new JButton(); 
Play.setIcon(PlayData); 
add(Play,GBC); 
add(BGR,"Breed"); 
} 

public void actionPerformed(ActionEvent AE){ 

    if(AE.getSource() == Play){ 

     CL.show(this, "Breed"); 
     } 
    } 

public void paintComponent(Graphics g){ 

    g.drawImage(TitleBg,0,0,800,600,this); 
    g.drawImage(Title,250,80,280,140,this); 
    } 
} 

這是卡片佈局

import javax.swing.*; 
import java.awt.*; 

public class BreedingGround extends JPanel{ 

ImageData ID = new ImageData(); 
Image Swamp; 
CardLayout CL; 

public BreedingGround(){ 

    setLayout(new GridBagLayout()); 
    GridBagConstraints c = new GridBagConstraints(); 
    ImageIcon SwampData = new ImageIcon(ID.SwampSource); 
    Swamp = SwampData.getImage(); 
} 

public void paintComponent(Graphics g){ 

    g.drawImage(Swamp,0,0,800,600,this); 
    } 

} 

,這也正是我想要的CardLayout打開。問題是,當我嘗試運行它時,窗口不會運行,並且這會一直顯示在編譯器中。

--------------------配置:8位奇美拉 - JDK版本1.6.0_26 - ------------- -------

Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint 
    at java.awt.GridBagLayout.addLayoutComponent(GridBagLayout.java:685) 
    at java.awt.Container.addImpl(Container.java:1074) 
    at java.awt.Container.add(Container.java:927) 
    at MainMenuComp.<init>(MainMenuComp.java:26) 
    at MainScreen.<init>(MainScreen.java:7) 
    at MainScreen.main(MainScreen.java:23) 

過程完成。

我真的很想知道這是什麼意思。

回答

0

您的問題與add(BGR,"Breed");MainMenuComp的佈局是GridBagLayout,所以約束必須是GridBagConstraint,而不是String(您有"Breed"作爲約束)。

+0

我固定的我改成了cardLayout。但按鈕填滿了整個屏幕。 –

4

我沒有看到你曾經將容器的佈局設置爲CardLayout,並且如果你沒有將佈局設置爲這樣,你不能神奇地使用它。如果你還沒有通過CardLayout tutorial,請考慮這樣做,因爲這裏都有解釋。

編輯1
從亞歷山大金的評論:

當我加入cardbagLayout它不會加載圖像和按鈕的大小填充整個屏幕。我也帶走了網格

您需要嵌套您的JPanel以嵌套佈局。使用單個JPanel作爲CardLayout容器,其單個功能是顯示其他JPanel(「卡」)。這些其他JPanel將使用必要的佈局來正確顯示它們所持有的組件,例如JButton或「網格」(不管它們是什麼)。甚至這些JPanel也可能包含其他使用其他佈局的JPanel。

再次請閱讀佈局教程,因爲它在那裏都有很好的描述。你不會後悔這樣做。

編輯2
下面是一個非常簡單的使用CardLayout的示例。 CardLayout使用JPanel顯示的組件(稱爲cardContainer)會根據組合框中選擇的項目進行更改。

這裏的CardLayout和使用它的的JPanel:

私人CardLayout cardLayout =新CardLayout();

// *** JPanel to hold the "cards" and to use the CardLayout: 
    private JPanel cardContainer = new JPanel(cardLayout); 

這裏就是我如何將組件添加到使用cardlayout-的JPanel:

JPanel redPanel = new JPanel(); 
    //... 
    String red = "Red Panel"; 
    cardContainer.add(redPanel, red); // add the JPanel to the container with the String 

我也是字符串添加到一個JComboBox,所以我以後可以用這個組合框告訴CardLayout顯示此JPanel(redPanel)如果用戶在這同一JComboBox中選擇項目「紅」:

cardCombo.addItem(red); // also add the String to the JComboBox 

這裏是在JComboBox中的ActionListener的,讓我更改cardlayout顯示的項目使用的JPanel:

cardCombo.addActionListener(new ActionListener() { 

    public void actionPerformed(ActionEvent e) { 
     String item = cardCombo.getSelectedItem().toString(); 

     // *** if combo box changes it tells the CardLayout to 
     // *** swap views based on the item selected in the combo box: 
     cardLayout.show(cardContainer, item); 
    } 
    }); 

而這裏的整個家當:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class SimpleCardLayoutDemo { 
    private CardLayout cardLayout = new CardLayout(); 

    // *** JPanel to hold the "cards" and to use the CardLayout: 
    private JPanel cardContainer = new JPanel(cardLayout); 
    private JComboBox cardCombo = new JComboBox(); 
    private JPanel comboPanel = new JPanel();; 

    public SimpleCardLayoutDemo() { 
     JPanel greenPanel = new JPanel(new BorderLayout()); 
     greenPanel.setBackground(Color.green); 
     greenPanel.add(new JScrollPane(new JTextArea(10, 25)), BorderLayout.CENTER); 
     greenPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15)); 
     greenPanel.add(new JButton("Bottom Button"), BorderLayout.PAGE_END); 
     String green = "Green Panel"; 
     cardContainer.add(greenPanel, green); 
     cardCombo.addItem(green); 

     JPanel redPanel = new JPanel(); 
     redPanel.setBackground(Color.red); 
     redPanel.add(new JButton("Foo")); 
     redPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     String red = "Red Panel"; 
     cardContainer.add(redPanel, red); 
     cardCombo.addItem(red); 

     JPanel bluePanel = new JPanel(); 
     bluePanel.setBackground(Color.blue); 
     JLabel label = new JLabel("Blue Panel", SwingConstants.CENTER); 
     label.setForeground(Color.white); 
     label.setFont(label.getFont().deriveFont(Font.BOLD, 32f)); 
     bluePanel.add(label); 
     String blue = "Blue Panel"; 
     cardContainer.add(bluePanel, blue); 
     cardCombo.addItem(blue); 

     comboPanel.add(cardCombo); 
     cardCombo.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent e) { 
      String item = cardCombo.getSelectedItem().toString(); 

      // *** if combo box changes it tells the CardLayout to 
      // *** swap views based on the item selected in the combo box: 
      cardLayout.show(cardContainer, item); 
     } 
     }); 
    } 

    public JPanel getCardContainerPanel() { 
     return cardContainer; 
    } 


    public Component getComboPanel() { 
     return comboPanel ; 
    } 

    private static void createAndShowUI() { 
     SimpleCardLayoutDemo simplecardDemo = new SimpleCardLayoutDemo(); 

     JFrame frame = new JFrame("Simple CardLayout Demo"); 
     frame.getContentPane().add(simplecardDemo.getCardContainerPanel(), BorderLayout.CENTER); 
     frame.getContentPane().add(simplecardDemo.getComboPanel(), BorderLayout.PAGE_END); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    // to run Swing in a thread-safe way 
    public static void main(String[] args) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowUI(); 
     } 
     }); 
    } 
} 
+0

當我添加cardbagLayout它不會加載圖像和按鈕大小填充整個屏幕。我也拿走了網格 –

+0

@亞歷山大:請在上面「編輯1」。 –

+0

@Alexander:請參閱上面「編輯2」中發佈的示例。 –