编程语言
首页 > 编程语言> > Java CardLayout主菜单问题

Java CardLayout主菜单问题

作者:互联网

好的,所以即时通讯工作在java中称为8位嵌合体.我现在在主菜单上工作但是当我使用卡片布局时,窗口由于某种原因不会打开.这是一些代码.

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)

流程已完成.

所有我真正想知道的是这是什么意思.

解决方法:

我没有看到你将容器的布局设置为CardLayout的位置,如果你没有将布局设置为这个,你就不能神奇地使用它.如果您尚未完成CardLayout tutorial,请考虑这样做,因为它在那里都有解释.

编辑1
Alexander Kim的评论:

when i added the cardbagLayout it wont load the image and the button size filled the whole screen. I also took away the grids

您需要嵌套JPanel以嵌套布局.使用单个JPanel作为CardLayout容器,其单个功能是显示其他JPanel(“卡”).这些其他JPanel将使用正确显示它们所持有的组件所需的任何布局,例如JButton或“网格”(无论它们是什么).甚至这些JPanels也可能拥有其他使用其他布局的JPanel.

再次,请阅读布局教程,因为它在那里描述得很好.你不会后悔这样做.

编辑2
这是一个使用CardLayout的非常简单的示例. CardLayout使用JPanel(称为cardContainer)显示的组件将根据在组合框中选择的项目而更改.

这是CardLayout和使用它的JPanel:

私人CardLayout cardLayout = new CardLayout();

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

以下是我使用JPanel将组件添加到cardlayout的方法:

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

我还将String添加到JComboBox中,以便稍后使用此组合框告诉CardLayout显示此JPanel(redPanel),如果用户在同一个JComboBox中选择“Red”项:

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

这是JComboBox中的ActionListener,它允许我使用JPanel更改cardlayout中显示的项目:

  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);
     }
  });

这是整个shebang:

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();
         }
      });
   }
}

标签:cardlayout,java,layout,swing,layout-manager
来源: https://codeday.me/bug/20190928/1825959.html