簡體   English   中英

為什么我的卡片布局中沒有顯示第一個面板

[英]Why isn't the first panel showing in my cardlayout

抱歉代碼轉儲,但我不確定是哪里導致了問題。 當我單擊 Round Robin 按鈕時,它應該帶我到 RoundRobin 頁面(它確實如此),但隨后我應該能夠單擊開始和藍色的中心面板應該是黃色的黃色動畫面板。 當我運行這個時,我只看到黃色的動畫面板,當我點擊開始時,我得到一個“線程“AWT-EventQueue-0”中的異常java.lang.IllegalArgumentException:CardLayout的錯誤父級”錯誤。

/**
     * @description:scheduling algoritmn visualisation app
     * @version: 1.0
     * @date created: 22/11/2020
     */
    
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.ImageIO;
    import java.net.*; 
    
    public class SchedulingAlgorithms extends JFrame{
        CardLayout cl;
        JPanel contentPane;
        Container deckPanel;
        Image image;
    
        /**
         * Constructor for objects of class SchedulingAlgorithms
         */
        public SchedulingAlgorithms(){
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 658, 336);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);
                
            JPanel headerPanel = new JPanel();
            JLabel label = new JLabel();
            headerPanel.setBackground(Color.LIGHT_GRAY);
            headerPanel.setBounds(5, 5, 125, 106);
            contentPane.add(headerPanel);
            
            
            try{
                URL url = new URL("file:C:///Users//alyssa//Scheduling_Animations//icon.png");        
                image=ImageIO.read(url);
                headerPanel.add(new JLabel(new ImageIcon(image)));
            }catch (IOException e) {
                e.printStackTrace();
            } 
            
            
            JPanel btnPanel = new JPanel();
            btnPanel.setBackground(Color.LIGHT_GRAY);
            btnPanel.setBounds(5, 111, 125, 181);
            contentPane.add(btnPanel);
            btnPanel.setLayout(null);
            cl= new CardLayout();
        
            JPanel deckPanel = new JPanel();
            deckPanel.setBounds(140, 5, 492, 287);
            contentPane.add(deckPanel);
            deckPanel.setLayout(cl);
                
            JPanel home = new JPanel();
            deckPanel.add(home, "home");
            
            JPanel homePic = new JPanel(new GridLayout(2,1));
            home.add(homePic, "homePic");
            
            JLabel instructions1 = new JLabel();
            instructions1.setText("Select a scheduling algorithm to see it's visual representation");
            homePic.add(instructions1);
            
            try{
                URL url = new URL("file:C:///Users//alyssa//Scheduling_Animations//schedule.png");        
                image=ImageIO.read(url);
                homePic.add(new JLabel(new ImageIcon(image)));
            }catch (IOException e) {
                e.printStackTrace();
            }                
            JPanel card1 = new JPanel();
            deckPanel.add(card1, "rr");
            card1.setLayout(new BorderLayout());
            
            JLabel txt2= new JLabel();
            txt2.setText("Round Robin: Set the boundaries of the animation");
            card1.add(txt2);
                       
                
            JPanel card2 = new JPanel();
            deckPanel.add(card2, "fcfs");
                
            JTextArea txt3 = new JTextArea();
            txt3.setText("First-Come-First-Served");
            card2.add(txt3);
                
            JPanel card3 = new JPanel();
            deckPanel.add(card3, "sjf");
                
            JTextArea txt4 = new JTextArea();
            txt4.setText("Shortest Job First");
            card3.add(txt4);
                
            JPanel card4 = new JPanel();
            deckPanel.add(card4, "ps");
                
            JTextArea txt5 = new JTextArea();
            txt5.setText("Priority Scheduling");
            card4.add(txt5);
            
            JButton btn0 = new JButton("Home");
            btn0.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if(e.getActionCommand()=="Home")
                    cl.show(deckPanel, "home");
                }
            });
            btn0.setBounds(10, 11, 89, 23);
            btnPanel.add(btn0);
                
            JButton btn1 = new JButton("Round Robin");
            btn1.setBounds(10, 45, 89, 23);
            btnPanel.add(btn1);
            
            btn1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {                
                    if(e.getActionCommand()=="Round Robin")
                    cl.show(deckPanel, "rr");
                }
            });
            
                
            JButton btn2 = new JButton("First-Come-First-Served");
            btn2.setBounds(10, 79, 89, 23);
            btnPanel.add(btn2);
            
            btn2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {                
                    if(e.getActionCommand()=="First-Come-First-Served")
                    cl.show(deckPanel, "fcfs");
                }
            });
                
            JButton btn3 = new JButton("Shortest Job First");
            btn3.setBounds(10, 113, 89, 23);
            btnPanel.add(btn3);
        
            btn3.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {                
                    if(e.getActionCommand()=="Shortest Job First")
                    cl.show(deckPanel, "sjf");
                }
            });
                
            JButton btn4 = new JButton("Priority Scheduling");
            btn4.setBounds(10, 147, 89, 23);
            btnPanel.add(btn4);
            
            btn4.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {                
                    if(e.getActionCommand()=="Priority Scheduling")
                    cl.show(deckPanel, "ps");
                }
            });
            
           JPanel northPanel=new JPanel();
           card1.add(northPanel,BorderLayout.NORTH);
           
           JLabel heading=new JLabel();
           heading.setText("Round Robin.");
           northPanel.add(heading);
           JLabel instructions2= new JLabel();
           instructions2.setText("Set the boundaries of the animation");          
           northPanel.add(instructions2);
           
           JPanel centerPanel=new JPanel();
           centerPanel.setBackground(Color.blue);
           card1.add(centerPanel,BorderLayout.CENTER);
           centerPanel.setLayout(new CardLayout());
           
           JPanel animPanel=new JPanel();
           animPanel.setBackground(Color.yellow);
           centerPanel.add(animPanel,"anim");
           
           
           JPanel southPanel=new JPanel();
           card1.add(southPanel,BorderLayout.SOUTH);
           
           JButton start = new JButton("START");
           start.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                   if(e.getActionCommand()=="START")
                   cl.show(centerPanel, "anim");
               }
           });
           start.setBounds(311, 159, 65, 23);
           southPanel.add(start);           

        }

       public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
            try {
                SchedulingAlgorithms frame = new SchedulingAlgorithms();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
            }
        });
       }
}

您使用變量“cl”作為對布局管理器的引用來設置“deckPanel”的布局:

deckPanel.setLayout(cl);

但是隨后您嘗試更改“centerPanel”上的卡:

  if(e.getActionCommand()=="START")
     cl.show(centerPanel, "anim");
  1. 您不能共享 CardLayout 引用。 如果您希望“centerPanel”也使用 CardLayout,那么您需要為該布局管理器保留一個單獨的引用。 因此,對於各個布局管理器,您確實需要“deckLayout”和“centerLayout”變量。 這就是為什么變量名應該是描述性的,這樣你就知道如何使用變量。

  2. 不要使用“==”進行對象比較。 而是使用equals(...)方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM