我在我的简单程序中遇到CardLayout更换卡时遇到问题

时间:2014-11-11 01:38:26

标签: java swing user-interface layout-manager cardlayout

此构造函数用于创建一个内部有四个面板的框架。这些面板中的每一个都应该能够使用卡布局在空面板和面板之间循环,其中包含内容。 (第一个面板从显示的内容开始,但是需要卡片布局以备将来使用)我只完成了为第一个和第三个面板添加元素但是卡片已经给我提出了问题。我尝试了很多东西但似乎没有任何帮助我的情况。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

public class Menu implements ActionListener{

    //Create an array of names for the panels
    String[] panelNames = {"part1", "part2", "part3", "part4"};

    //Create an array of panels
    JPanel[] panels = new JPanel[panelNames.length];

    //creates an array of the cards to be added to the panels created above
    JPanel[] cards = new JPanel[panelNames.length];

    //Create an array of buttons
    JButton[] buttons1 = new JButton[4];

    //Create an array of buttons
    JButton[] buttons2 = new JButton[4];

    //create a default panel for the initially empty panels
    JPanel empty = new JPanel();

    //Create a layout
    CardLayout c = new CardLayout();

    Menu(){

        //Create a JFrame to hold the panels
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setTitle("Select your application"); 
        frame.setSize(700,700);
        frame.setLocationRelativeTo(null);
        frame.setLayout(new BorderLayout());

        //Panel that contains the four panels needed for this assignment
        JPanel parts = new JPanel(new GridLayout(2,2));

        //a loop that creates each panels and fills it with the cards
        for(int i = 0 ; i < panelNames.length ; i++){

            //define the panels and adds them to the frame
            panels[i] = new JPanel(c);
            panels[i].setPreferredSize(new Dimension(300,300));
            cards[i] = new JPanel(new FlowLayout());
            parts.add(panels[i]);
            panels[i].add(empty, "empty");
            panels[i].add(cards[i], "card");
            c.show(panels[i], "empty");
        }
        c.show(panels[0], "card");
        frame.add(parts, BorderLayout.NORTH);

        //adds 4 buttons to the first panel and names them
        Border line = new LineBorder(Color.GRAY,1);
        cards[0].setLayout(new GridLayout(4,1));
        for(int i = 0 ; i < buttons1.length ; i++){

            buttons1[i] = new JButton("TBA");
            buttons1[i].setBackground(Color.white);
            buttons1[i].setBorder(line);
            buttons1[i].addActionListener(this);
            cards[0].add(buttons1[i]);
        }
        buttons1[0].setText("Sudoku");

        //adds 4 buttons to the 3rd panel and names them
        cards[2].setLayout(new GridLayout(4,1));
        for(int i = 0 ; i < buttons2.length ; i++){

            buttons2[i] = new JButton();
            buttons2[i].setBackground(Color.LIGHT_GRAY);
            buttons2[i].addActionListener(this);
            cards[2].add(buttons2[i]);
            buttons2[i].setEnabled(false);
        }
        buttons2[0].setText("Sample");
        buttons2[1].setText("Solution");
        buttons2[2].setText("Verify");
        buttons2[3].setText("Return to main");

        //adds the exit button and the exit panels to the frame
        JPanel exit = new JPanel();
        frame.add(exit, BorderLayout.SOUTH);
        JButton exitb = new JButton("exit");
        exitb.setBackground(Color.red);
        exitb.setForeground(Color.white);
        exitb.setPreferredSize(new Dimension(245,30));
        exit.add(exitb);
        exitb.addActionListener(new ActionListener(){ 

            @Override
            public void actionPerformed(ActionEvent e) { 

                System.exit(0);
            } 
        });

        //show the frame
        frame.setVisible(true);
    }

    //method that determined which app to launch and launches it
    @Override
    public void actionPerformed(ActionEvent ae) {

        //finds the source of the event
        Object src = ae.getSource();

        //these statements do the appropriate actions when a button is pressed
        if(src == buttons1[0]){

            c.show(panels[2], "card");
            buttons2[0].setEnabled(true);
            buttons2[3].setEnabled(true);

        }

        else if(src == buttons2[0]){

            buttons2[1].setEnabled(true);
            buttons2[0].setEnabled(false);
        }
        else if(src == buttons2[1]){

            buttons2[2].setEnabled(true);
            buttons2[1].setEnabled(false);
        }
        else if(src == buttons2[2]){

            buttons2[2].setEnabled(false);    
        }
        else if(src == buttons2[3]){

            c.show(panels[2], "empty");
            for(int i = 0 ; i < buttons1.length ; i++){buttons2[i].setEnabled(false);
                                                       buttons1[i].setEnabled(true);}
        }

        else{System.out.println("option not yet implemented");}
    }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

  • 组件只能驻留在单个容器中。每次将组件添加到另一个容器时,它首先(有效地)从其当前父级中删除。您无法按照自己的方式重复使用empty
  • CardLayout无法管理多个容器,它负责管理单个容器。

我的“直觉”感觉是创建一个从JPanel扩展的单独自定义类,它具有show方法,允许您显示命名组件。该面板将使用它自己的CardLayout并管理其内部状态。这将允许您根据需要添加额外内容的跳跃点......