2017-12-14 19 views
0

私は、カードレイアウトレイアウトのメインパネルを含むフレームを持つプログラムを持っていますが、別のカード/パネルを表示したいのです。JAVA cardLayout。別のクラスからカードを呼び出す方法

私の場合、私は本当にボタンアクションリスナーから新しいカードを呼び出すのに苦労しています。

ボタンをクリックした後に新しいカードを表示したいが、アクションリスナーに入れたコードのどれもが私が望むカードを表示しなかった。

私はprintlnを内部で行ったので、私のactionListenerの仕事を知っています。

ここに私のコードです。私は不必要なものを取り除きましたので、読むのが簡単です。助けてくれてありがとう!

私は、コードの構造化に関するすべてのアドバイスを取るよ

メインフレーム:

public class MainFrame extends JFrame{ 

final static String CONNEXION_VIEW = "connexionView"; 
final static String CONNEXION_FAILED_VIEW = "connexionRefusee"; 

public MainFrame() 
{ 
    super(); 
    initialize(); 
} 


private void initialize() 
{ 
    getMainPanel(); 

    add(getMainPanel()); 

} 

CardLayout cardLayout; 
public CardLayout getCardLayout() 
{ 
    if (cardLayout == null) 
    { 
     cardLayout = new CardLayout(); 
    } 
    return cardLayout; 
} 


JPanel mainPanel; 
public JPanel getMainPanel() 
{ 
    if (mainPanel == null) 
    { 
     mainPanel = new JPanel(); 

     mainPanel.setLayout(getCardLayout()); 

     mainPanel.add(CONNEXION_VIEW, getConnexionView()); 
     mainPanel.add(CONNEXION_FAILED_VIEW, getConnexionFailedView()); 

    } 
    return mainPanel; 
} 

ConnexionView connexionView; 
protected ConnexionView getConnexionView() 
{ 
    if (connexionView == null) 
    { 
     connexionView = new ConnexionView(); 
    } 
    return connexionView; 
} 

ConnexionFailedView connexionFailedView; 
protected ConnexionFailedView getConnexionFailedView() 
{ 
    if (connexionFailedView == null) 
    { 
     connexionFailedView = new ConnexionFailedView(); 
    } 
    return connexionFailedView; 
} 

コネクションビューは、ボタンで1は私が私のコードを入れたいアクションリスナーをクリックする

public class ConnexionView extends JPanel{ 

GridBagLayout gbl = new GridBagLayout(); 

private JButton btnConnexion; 

Dimension dimensionBouton = new Dimension(170, 30); 

public ConnexionView() 
{ 
    super(); 
    initialise(); 
} 

private void initialise() 
{ 
    setLayout(gbl); 

    GridBagConstraints gbcbtnConnexion = new GridBagConstraints(); 
    gbcbtnConnexion.gridwidth = GridBagConstraints.REMAINDER; 
    gbcbtnConnexion.gridheight = GridBagConstraints.REMAINDER; 
    gbcbtnConnexion.gridx = 1; 
    gbcbtnConnexion.gridy = 2; 
    add(getBtnConnexion(), gbcbtnConnexion); 
} 

private JButton getBtnConnexion() 
{ 
    if (btnConnexion == null) 
    { 
     btnConnexion = new JButton("Connexion"); 
     btnConnexion.setPreferredSize(dimensionBouton); 
     btnConnexion.setMinimumSize(dimensionBouton); 

     btnConnexion.addMouseListener(new MouseAdapter() 
     { 
      @Override 
      public void mouseClicked(MouseEvent e) 
      { 
       /////code to display the connexion_Failed_View 

       System.out.println("test"); 
      } 
     }); 
    } 
    return btnConnexion; 
} 

}

とコネクションビューに失敗し、私はボタンの後に表示したいものは、あなたがそれにアクセスできるように、何とか自分のボタンでコンポーネントを維持する必要があります事前

答えて

0

public class ConnexionFailedView extends JPanel{ 

public ConnexionFailedView() 
{ 
    super(); 
    initialise(); 
} 

private void initialise() 
{ 
    setBackground(Color.YELLOW); 
} 

感謝をクリックします。

class ConnexionView { 
    private JComponent mainPanel; 
    public ConnexionView(JComponent mp) { mainPanel = mp; } 
} 

明らかに、これはMainFrameがそれを渡す必要があることを意味します。

さて、リスナーは、それが働いた

// it would be cleaner if you passed the layout in the constructor as well 
CardLayout cl = (CardLayout) mainPanel.getLayoutManager(); 
cl.show(mainPanel, MainFrame.CONNEXION_FAILED_VIEW); 
+0

を行うことができます!ありがとうございます – Clement

+0

@Clement素晴らしい。回答の横にあるチェックマークを自由に選択して、Answeredとタグ付けしてください:) – daniu

関連する問題