2017-03-07 68 views
0

我想用CardLayout創建一個帶參數的JPanel。我有以下代碼:Java在JFrame中創建cardLayout,構造函數參數爲

public class MainFrame extends JFrame{ 

private JPanel contentPane; 
private CardLayout cardLayout; 
private String card; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       MainFrame frame = new MainFrame(""); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public MainFrame(String card) { 
    initComponents(); 
    cardLayout = new CardLayout(); 

    if(card.equals("ADMIN")){ 
     contentPane.revalidate(); 
     cardLayout.show(this.contentPane, "ADMIN"); 
    }else if(card.equals("USER")){ 
     contentPane.revalidate(); 
     cardLayout.show(this.contentPane, "USER"); 
    } 
} 

private void initComponents() { 
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
    this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2); 

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(1280, 800); 
    this.contentPane = new JPanel(); 
    this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    this.contentPane.setLayout(cardLayout); 
    this.contentPane.add(new PanelOne(), "ADMIN"); 
    this.contentPane.add(new PanelTwo(), "USER"); 
    setContentPane(this.contentPane); 
} 

但是,如果我運行此代碼我得到一個異常:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: 
    wrong parent for CardLayout 

我有什麼做的,解決這個問題?

我已經嘗試在主要方法中更改參數,但沒有任何解決方案。

回答

4

你的問題是你正在使用的CardLayout對象。這CardLayout對象,您使用的是show的組件:

public MainFrame(String card) { 
    initComponents(); 
    cardLayout = new CardLayout(); 

是不一樣的一個是目前的contentPane而這裏的佈局:

this.contentPane.setLayout(cardLayout); 

事實上,它似乎這個後面的cardLayout變量在使用時實際上是空的。

解決辦法,改成這樣:

公共級的大型機擴展的JFrame {

private JPanel contentPane; 
private CardLayout cardLayout = new CardLayout(); 

,不叫new CardLayout();別處。


另一個問題是在調用構造函數時傳入一個空字符串。

例如,

import java.awt.CardLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Toolkit; 

import javax.swing.*; 
import javax.swing.border.EmptyBorder; 

public class MainFrame extends JFrame { 

    private JPanel contentPane; 
    private CardLayout cardLayout = new CardLayout(); 
    private String card; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        MainFrame frame = new MainFrame(PanelOne.NAME); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 
    public MainFrame(String card) { 
     initComponents(); 
     // cardLayout = new CardLayout();   
     cardLayout.show(getContentPane(), card); 
    } 

    private void initComponents() { 
     Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
     this.setLocation(dim.width/2 - this.getSize().width/2, 
       dim.height/2 - this.getSize().height/2); 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(1280, 800); 
     this.contentPane = new JPanel(); 
     this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     this.contentPane.setLayout(cardLayout); 
     this.contentPane.add(new PanelOne(), PanelOne.NAME); 
     this.contentPane.add(new PanelTwo(), PanelTwo.NAME); 
     setContentPane(this.contentPane); 
    } 
} 

public class PanelOne extends JPanel { 
    public static final String NAME = "Administrator"; 

    public PanelOne() { 
     setBorder(BorderFactory.createTitledBorder(NAME)); 
    } 
} 

public class PanelTwo extends JPanel { 
    public static final String NAME = "User"; 

    public PanelTwo() { 
     setBorder(BorderFactory.createTitledBorder(NAME)); 
    } 

} 
+0

謝謝你,你救了我的一天 – leonkuehn

相關問題