为什么公共类名称错了?(卡片布局)

时间:2015-10-12 21:21:04

标签: java cardlayout

  

在我修正销售错误后,该程序仍无效。它在控制台中说。 “错误:在类help.MyCardLayout中找不到主要方法,请将主方法定义为:      public static void main(String [] args)   或JavaFX应用程序类必须扩展javafx.application.Application“我不知道这意味着什么。

package help;

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

public class MyCardLayout implements ItemListener {
    JPanel cards;
    final static String BUTTONPANEL1 = "Card1";
    final static String BUTTONPANEL2 = "Card2";

    public void addComponentToPane(Container pane){
        JPanel comboBoxPane = new JPanel();
        String comboBoxItems[] = { BUTTONPANEL1, BUTTONPANEL2 };
        JComboBox cb  = new JComboBox(comboBoxItems);
        cb.setEditable(false);
        cb.addItemListener(this);
        comboBoxPane.add(cb);

        JPanel card1 = new JPanel();
        card1.add(new JButton("Button 1"));

        JPanel card2 = new JPanel();
        card2.add(new JButton("Button 2"));

       cards = new JPanel(new CardLayout());
       cards.add(card1, BUTTONPANEL1);
       cards.add(card2, BUTTONPANEL2);

       pane.add(comboBoxPane, BorderLayout.PAGE_START);
       pane.add(cards, BorderLayout.CENTER);

    }

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

    private static void createAndShowGUI(){
        JFrame frame = new JFrame("CardLayoutDemo");
        frame.setSize(300,200);
        frame.setTitle("CardLayout");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        CardLayoutDemo demo = new CardLayoutDemo();
        demo.addComponentToPane(frame.getContentPane());

        frame.pack();
        frame.setVisible(true);



        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

您写道:

 public void itemStateChange(ItemEvent evt) {
                           ^ d is missing

名称为itemStateChanged,而非itemStateChange

答案 1 :(得分:0)

你错误拼写itemStateChanged()itemStateChange()(最后遗失了d):

public void itemStateChange(ItemEvent evt){
                           ↑ here