0

我正在使用 CardLayout 编写一个简单的程序。主屏幕应显示一个按钮,按下该按钮将转到下一个屏幕,该屏幕包含另一个屏幕的另一个按钮。我的问题是,当我运行我的程序时,屏幕是黑色的。我尝试按照在线教程编写自己的程序,但我似乎没有发现我的代码有问题。运行时我没有收到任何错误。这是我的代码

//using CardLayout to change screen when action is performed
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Popup;
import javax.swing.JOptionPane;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;

public class CL extends JFrame {

    JPanel cardPanel;
    JPanel cardPanelA;
    JPanel cardPanelB;//to set different screens
    CardLayout cl;
    private JButton button1;
    private JButton button2;
    private JButton change;
    private JLabel label;
    private JTextField textField1;
    private JTextField textField2;
    JButton button;

    public CL() {
        super("This is a sample");

        cardPanel = new JPanel();
        cardPanelA = new JPanel();
        cardPanelB = new JPanel();

        cl = new CardLayout();
        cardPanel.setLayout(cl);

        button1 = new JButton("button1");
        button2 = new JButton("button2");
        change = new JButton("change screen");
        label = new JLabel("this is a label");
        textField1 = new JTextField(10);
        textField2 = new JTextField("enter text", 6);

        cardPanelA.add(change);
        cardPanelA.add(label);
        cardPanelA.add(textField1);
        cardPanelA.add(textField2);
        cardPanelB.add(button1);
        cardPanelB.add(button2);

        cardPanel.add(cardPanelA);
        cardPanel.add(cardPanelB);

        JPanel panel1 = new JPanel();
        button = new JButton("initial button");
        panel1.add(button);

        theHandler handler = new theHandler();//make action listener
        change.addActionListener(handler);
        button1.addActionListener(handler);
        button2.addActionListener(handler);
        button.addActionListener(handler);
        /*
        getContentPane().add(panel1, BorderLayout.NORTH);
        getContentPane().add(cardPanelA, BorderLayout.NORTH);
        getContentPane().add(cardPanelB, BorderLayout.NORTH);
        */
    }

    private class theHandler implements ActionListener {
        public void actionPerformed(ActionEvent event) {

            if (event.getSource() == button) {
                cl.show(cardPanel, "Panel A");
            }
            if (event.getSource() == change) {
                cl.show(cardPanelB, "panelB");
            }
            if (event.getSource() == button2) {
                cl.show(cardPanel, "PanelA");
            }
            if (event.getSource() == button1) {
                JOptionPane.showMessageDialog(null, "this is the second screen");
            }

        }
    }

}

/* 使用 CardLayout 的方法:创建一个 CardLayout 管理器并创建一堆不同的 JPanel * 每个都是不同的屏幕。制作一个将 CardLayout 存储为布局的面板。* 将不同的元素添加到每个面板(按钮、文本字段),然后将面板添加到存储 * CardLayout */ 的 JPanel

import javax.swing.JFrame;

public class CardTest {
    public static void main(String[] args) {

        CL object = new CL();
        object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        object.setSize(400, 400);
        object.setVisible(true);

    }
}

这可能很简单,但我不确定它是什么。一些建议将不胜感激。

4

1 回答 1

0

确保将面板添加到框架中

    add(cardPanel);

没有它,将不会显示任何组件

于 2013-08-08T06:12:40.660 回答