首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

CardLayout布局的有关问题,请教如何改?小菜求教

2013-03-27 
CardLayout布局的问题,请问怎么改?小菜求教首先,请看代码://import ... 略去public class VisualPanel_use

CardLayout布局的问题,请问怎么改?小菜求教
首先,请看代码:


//import ... 略去
public class VisualPanel_user {
JFramejf=null;
CardLayoutcard=null;
public VisualPanel_user() {
JPaneljpN=newJPanel();
JPaneljpW=newJPanel();
JPaneljpC=newJPanel();
card=new CardLayout();

JLabeluser_name=newJLabel("hello,"+Login_Cla.user_name);
    JLabel title=new JLabel(" 欢迎进入系统 ");
    
    user_name.setFont(new java.awt.Font("华文行楷", 0, 30));
    title.setFont(new java.awt.Font("华文行楷", 0, 30));
    jpN.add(user_name);
    jpN.add(Box.createHorizontalStrut(100),"CENTER");
    jpN.add(title,"CENTER");
    jpN.add(Box.createHorizontalStrut(100),"CENTER");
    jpN.setBorder(new Border(){
public Insets getBorderInsets(Component c) {
return new Insets(10,10,10,10);
}
public boolean isBorderOpaque() {
return false;
}
public void paintBorder(Component c, Graphics g, int x, int y,
int width, int height) {
}});
    JScrollPane scroll=new JScrollPane();
    finalJTreetree=newJTree();
    DefaultMutableTreeNode root=newDefaultMutableTreeNode();
    DefaultMutableTreeNode buy=newDefaultMutableTreeNode();
    DefaultMutableTreeNode deposit=newDefaultMutableTreeNode();
    DefaultMutableTreeNode money=newDefaultMutableTreeNode();
    DefaultMutableTreeNode transfer=newDefaultMutableTreeNode();
    DefaultMutableTreeNode withdraw=newDefaultMutableTreeNode();
    
    root.setUserObject("管理菜单");
    buy.setUserObject("购买商品");
    deposit.setUserObject("即时存款");
    money.setUserObject("资金查询");
    transfer.setUserObject("即时转账");
    withdraw.setUserObject("即时取款");
    
    root.add(buy);
    root.add(deposit);
    root.add(money);
    root.add(transfer);
    root.add(withdraw);
    
    DefaultTreeModeldtm=newDefaultTreeModel(root);
    
    tree.setModel(dtm);
    scroll.setViewportView(tree);
    jpW.add(scroll);

//    final JPanelp_1=new UserBuy_Cla();
//    final JPanelp_2=new UserDeposit_Cla();
//    final JPanelp_3=new UserMoney_Cla();
//    final JPanelp_4=new UserTransfer_Cla();
//    final JPanelp_5=new UserWithdraw_Cla();
    final JPanelp_1=new JPanel();
    final JPanelp_2=new JPanel();
    final JPanelp_3=new JPanel();
    final JPanelp_4=new JPanel();
    final JPanelp_5=new JPanel();
    p_1.setBackground(Color.black);


    p_2.setBackground(Color.blue);
    p_3.setBackground(Color.cyan);
    p_4.setBackground(Color.DARK_GRAY);
    p_5.setBackground(Color.gray);
    jpC.setLayout(card);
    jpC.add(p_1,"buy");
    jpC.add(p_2,"deposit");
    jpC.add(p_3,"money");
    jpC.add(p_4,"transfer");
    jpC.add(p_5,"withdraw");
    
jf=newJFrame("在线销售系统");
jf.add(jpN,BorderLayout.NORTH);
jf.add(jpW,BorderLayout.WEST);
jf.add(jpC,BorderLayout.CENTER);
jf.pack();
jf.setVisible(true);

tree.addTreeSelectionListener(new TreeSelectionListener(){
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode select = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if( select.toString().equals("购买商品") )
card.show(p_1,"buy");
else if( select.toString().equals("即时存款"))
card.show(p_2,"deposit");
else if( select.toString().equals("资金查询"))
card.show(p_3,"money");
else if( select.toString().equals("即时转账"))
card.show(p_4,"transfer");
else
card.show(p_5,"withdraw");
}});
}
public static void main(String[] args) {
new VisualPanel_user();
}
}



问题:
点击【子节点】,弹出Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: wrong parent for CardLayout

然后Eclipse显示出card.show(...)这里出错!
可是我的JPanel p_1到p_5装进jpC里面去了啊,jpC定义为CardLayout .

问题在,但我不知道错在哪,也不会改?求指教!
[解决办法]
引用:
首先,请看代码:
……
……


你的 CardLayout.show(),用错了。第一个参数应该是 card 的 container
jpC, 而不应该是 card 自身。87行的 tree.addTreeSelectionListener
可更改为:


        final JPanel cards = jpC;
        tree.addTreeSelectionListener(new TreeSelectionListener(){
            public void valueChanged(TreeSelectionEvent e) {
                DefaultMutableTreeNode select = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
                if( select.toString().equals("购买商品") )
                    card.show(cards ,"buy");
                else if( select.toString().equals("即时存款"))
                    card.show(cards ,"deposit");
                else if( select.toString().equals("资金查询"))
                    card.show(cards ,"money");
                else if( select.toString().equals("即时转账"))


                    card.show(cards ,"transfer");
                else
                    card.show(cards ,"withdraw");
        }});


[解决办法]
让类本身继承JFrame
取消使用frame就可以了
[解决办法]

大家互相学习!
[解决办法]
引用:
引用:让类本身继承JFrame
取消使用frame就可以了

也谢谢你,提供这个思路!

不过,想问你下,直接将类定义为extends JFrame和在类里面定义JFrame jf有区别么?

一个是调用frame对象,一个是把自身看成frame对象
[解决办法]
另外,两条小建议:

// 建议1,加入这一行。(否则,关掉窗口后,你的 App 也不能停止)
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JPanel cards = jpC;
tree.addTreeSelectionListener(new TreeSelectionListener(){
  ......
  // 省略
  ......
});

// 建议2,把这放到最后
jf.pack();
jf.setVisible(true);

热点排行
Bad Request.