2016-06-22 3 views
0

私のフレームには2つのパネルがあり、1つはボタン用です(私はradioButtonを使いますが、今のところボタンを使う方が簡単です)。もう1つはカードレイアウトパネル用です。特定のボタンを押すと、私の計画はチャドをシャッフルすることです。移動ボタンのように、移動パネルカードが表示されます。移動パネルカードにはx0ラベルとテキストフィールドがあり、ラインパネルカードにはラベルとテキストフィールドの両方にx0とx1があります。カードレイアウトでカードを交換するボタン

2クラスがありますが、1は、他方はカードのためであるbuttonpanelのために=ボタン ある= PanelMiddle はここに私のコードです:

public class PanelMiddle{ 
    JPanel controlPanel = new JPanel(); 
    CardLayout cl = new CardLayout(); 

    JPanel movePanel = new JPanel(); 
    JPanel linePanel = new JPanel(); 

    JLabel x0Label = new JLabel("x0"); 
    JTextField x0TextField = new JTextField(3); 
    JLabel x1Label = new JLabel("x1"); 
    JTextField x1TextField = new JTextField(3); 

    public PanelMiddle(){ 
     controlPanel.setLayout(cl); 

     //move panel 
     movePanel.setLayout(new GridLayout (1,2)); 
     movePanel.add(x0Label); 
     movePanel.add(x0TextField); 
     controlPanel.add(movePanel,"Move"); //add the keyword Move to show the move card 

     //line panel 
     linePanel.setLayout(new GridLayout (2,2)); 
     //linePanel.add(x0Label); 
     linePanel.add(x1Label); 
     //linePanel.add(x0TextField); 
     linePanel.add(x1TextField); 
     controlPanel.add(linePanel,"Line"); // add the keyword Line to show the line card 

     } 
    } 

他のクラスで私が持っている:

public class Buttons extends PanelMiddle{ 
    JPanel buttonPanel = new JPanel(); 

    JButton moveB = new JButton ("Move"); 
    JButton lineB = new JButton ("Line"); 

    public Buttons(){ 
    buttonPanel.setLayout(new GridLayout (2,1)); 
    buttonPanel.add(moveB); 
    buttonPanel.add(lineB); 

    action(); 
    } 

    public void action(){ 
    moveB.addActionListener((e) -> { 
    cl.show(controlPanel,"Move"); 
    }); 

    lineB.addActionListener((e) -> { cl.show(controlPanel,"Line");}); 
    } 
    } 

私が得た結果は奇妙です。私のパネルは完全には表示されません。しかし、私がすべてのラインパネルにコメントしてみると、うまくいきます。誰かがここで修正をしていますか?

NB:私はこのテキストを編集する方法が分かりませんので、ちょっと混乱します。

編集1:guleryuzが言うように、私は、コンポーネントのみ、あなたがx0Labelとx0TextField 2の両方のパネル追加された1つのコンテナに追加することができ、スイングのコンポーネント階層内のラインパネル

答えて

1

からx0Labelx0TextFieldコメントアウト。したがって、x0Labeを2秒のパネル(linePanel)に追加すると、movePanelから削除されます(x0TextFieldの場合と同じ)ので、movePanelは空になります。

詳細here

+0

Ah!はい!それは1つの問題だけど、それでも私のボタンはパネルを変えないと解決していない。 –