2016-06-22 230 views
0

我有2个面板在我的框架中,1是按钮(我想使用radioButton,但现在它更容易使用按钮),另一个是卡布局面板。当我按下特定按钮时,我的计划是洗牌乍得。像移动按钮将显示移动面板卡。移动面板卡具有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 

     } 
    } 

在其他I类有:

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");}); 
    } 
    } 

我得到的结果很奇怪。它没有完全显示我的面板。但是当我尝试评论所有的线条面板时,它就可以工作。有人在这里修复吗?

注意:我很抱歉,我不知道如何编辑这里的文本,所以它有点混乱。

编辑1:如guleryuz说,我注释掉x0Labelx0TextField从线路板

回答

1

在Swing组件层次结构中的成分只能被添加到一个容器中,要添加x0Label和x0TextField两人双双面板。所以当你添加x0Labe两个第二个面板(linePanel)时,它将从movePanel中移除(对于x0TextField,也是这种情况),因此movePanel变为空。

更多详情here

+0

啊!好的!这是一个问题,但它仍然没有解决我的按钮不会改变面板。 –