2017-04-06 26 views
0

私はJavaを使い慣れていません。スイングでCardLayoutを使ってナビゲーションを行っています。基本的に私はJFrameに2つのボタンがあります。ボタン1つをクリックするとカード1に行きます。JPanelcard1に2つのボタンをつけています。別のボタンをクリックするとカード2に行きます。JTextFieldパネル上card2。しかし、それは起こっていません。CardLayout上のJPanel Swing Java

誰でも修正できますか?

私のコードは以下の通りです。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class CardLayoutTest extends JFrame implements ActionListener { 
    JFrame f; 
    JButton b; 
    JButton c; 
    JPanel panel; 
    JPanel cards; 
    JPanel card1; 
    JPanel card2; 
    CardLayout card; 
    Container pane;  
    final String card1Text = "One"; 
    final String card2Text = "Two"; 

    CardLayoutTest() { 

    } 

    public void passBtn() { 
     f=new JFrame("Card Layout Test"); 

     card1 = new JPanel(); 
     card1.add(new JButton("Button 1 - Card 1")); 
     card1.add(new JButton("Button 2 - Card 1")); 
     card1.setBackground(new Color(255,0,0)); 

     card2 = new JPanel(); 
     card2.add(new JTextField("TextField on Card 2", 20)); 
     card2.setBackground(new Color(0,255,0)); 

     //Create the panel that contains the "cards". 
     cards = new JPanel(new CardLayout()); 
     cards.add(card1, card1Text); 
     cards.add(card2, card2Text); 

     b = new JButton("Page 1"); 
     b.setBounds(50,50,70,30);   
     b.setBackground(Color.red); 

     c = new JButton("Page 2"); 
     c.setBounds(50,80,70,30);   
     c.setBackground(Color.blue); 

     pane = f.getContentPane(); 
     pane.add(cards, BorderLayout.CENTER); 

     b.addActionListener(new ActionListener(){ 
       public void actionPerformed(ActionEvent e) {          
        card.show(card1, card1Text);      
       } 
     }); 

     c.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e) {          
       card.show(card2, card2Text);     
      } 
    }); 
     f.add(b); f.add(c); f.add(panel); f.add(cards); 
    } 

    public static void main(String[] args) {  
     new CardLayoutTest();  
    }  
} 
+0

あなたがここを見てなかった:HTT ps://docs.oracle.com/javase/tutorial/uiswing/layout/card.html? – XtremeBaumer

+0

'card'を決して初期化しないので、あなたはNullPointerExceptionsを取得していると思います。どこでも 'card'を使うことはありません。 – VGR

答えて

0

試してみてください。

card = new CardLayout(); 
cards = new JPanel(card); 

代わりのcards = new JPanel(new CardLayout());

UPDATE

public class CardLayoutTest extends JFrame { 
JButton b; 
JButton c; 
JPanel cards; 
JPanel card1; 
JPanel card2; 
CardLayout card; 
final String card1Text = "One"; 
final String card2Text = "Two"; 

CardLayoutTest() { 
    super(); 
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    passBtn(); 
} 

public void passBtn() { 
    card1 = new JPanel(); 
    card1.setBackground(new Color(255,0,0)); 

    card2 = new JPanel(); 
    card2.add(new JTextField("TextField on Card 2", 20)); 
    card2.setBackground(new Color(0, 255, 0)); 

    //Create the panel that contains the "cards". 
    card = new CardLayout(); 
    cards = new JPanel(card); 
    cards.add(card1, card1Text); 
    cards.add(card2, card2Text); 

    b = new JButton("Page 1"); 
    b.setBounds(50, 50, 70, 30); 
    b.setBackground(Color.red); 
    card1.add(b); 
    c = new JButton("Page 2"); 
    c.setBounds(50, 80, 70, 30); 
    c.setBackground(Color.blue); 
    card1.add(c); 
    add(cards); 
    card.show(cards, card1Text); 

    b.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      try { 
       card.show(cards, card1Text); 
      } catch (Exception e1) { 
       e1.printStackTrace(); 
      } 
     } 
    }); 

    c.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      try { 
       card.show(cards, card2Text); 
      } catch (Exception e1) { 
       e1.printStackTrace(); 
      } 
     } 
    }); 
} 

public static void main(String[] args) { 
    CardLayoutTest cardLayoutTest = new CardLayoutTest(); 
    cardLayoutTest.setVisible(true); 
} 

}

+0

'cards = new JPanel(new CardLayout());の何が問題なのですか? – c0der

+0

あなたのフィールドには割り当てられません。リスナーはnull値を使用します。 – ZhenyaM

+0

コードに間違いがある場合。 'cards = new JPanel(new CardLayout());'はエラーではなく、あなたが示唆したことを実行しても問題は解決しません。 – c0der

関連する問題