首页 > 代码库 > Java中CardLayout的使用
Java中CardLayout的使用
【例1】代码:
1 import java.awt.CardLayout; 2 import java.awt.GridLayout; 3 import java.awt.event.ActionEvent; 4 import java.awt.event.ActionListener; 5 import java.awt.event.WindowAdapter; 6 import java.awt.event.WindowEvent; 7 8 import javax.swing.JButton; 9 import javax.swing.JFrame; 10 import javax.swing.JLabel; 11 import javax.swing.JPanel; 12 13 public class TestCardLayout extends JFrame implements ActionListener { 14 CardLayout cl; 15 JPanel nav = new JPanel(); 16 JPanel main = new JPanel(); 17 JPanel jp1 = new JPanel(); 18 JPanel jp2 = new JPanel(); 19 JPanel jp3 = new JPanel(); 20 JPanel jp4 = new JPanel(); 21 JPanel jpFirst = new JPanel(); 22 JPanel jpNext = new JPanel(); 23 JPanel jpPrevious = new JPanel(); 24 JPanel jpLast = new JPanel(); 25 JLabel jl1 = new JLabel("第一张"); 26 JLabel jl2 = new JLabel("第二张"); 27 JLabel jl3 = new JLabel("第三张"); 28 JLabel jl4 = new JLabel("第四张"); 29 JButton first = new JButton("第一张"); 30 JButton next = new JButton("下一张"); 31 JButton previous = new JButton("上一张"); 32 JButton last = new JButton("最后一张"); 33 34 public static void main(String[] args) { 35 new TestCardLayout(); 36 } 37 38 TestCardLayout() { 39 this.setTitle("TestCardLayout"); 40 this.setSize(400, 300); 41 this.setLayout(new GridLayout(1, 2)); 42 main.setLayout(new CardLayout()); 43 this.setResizable(false); 44 45 nav.setLayout(new GridLayout(4,1)); 46 jp1.add(first); 47 jp2.add(next); 48 jp3.add(previous); 49 jp4.add(last); 50 nav.add(jp1); 51 nav.add(jp2); 52 nav.add(jp3); 53 nav.add(jp4); 54 jpFirst.add(jl1); 55 jpNext.add(jl2); 56 jpPrevious.add(jl3); 57 jpLast.add(jl4); 58 main.add(jpFirst); 59 main.add(jpNext); 60 main.add(jpPrevious); 61 main.add(jpLast); 62 63 this.add(nav); 64 this.add(main); 65 first.addActionListener(this); 66 next.addActionListener(this); 67 previous.addActionListener(this); 68 last.addActionListener(this); 69 this.addWindowListener(new WindowAdapter() { 70 public void windowClosing(WindowEvent arg0) { 71 System.exit(0); 72 } 73 }); 74 setVisible(true); 75 } 76 77 public void actionPerformed(ActionEvent e) { 78 cl = (CardLayout)main.getLayout(); 79 cl.setHgap(0); cl.setVgap(0); 80 81 if(e.getSource() == first){ 82 cl.first(main); 83 }else if(e.getSource() == next){ 84 cl.next(main); 85 }else if(e.getSource() == previous){ 86 cl.previous(main); 87 }else if(e.getSource() == last){ 88 cl.last(main); 89 } 90 91 92 } 93 94 }
效果图:
【例2】代码:
1 import java.awt.BorderLayout; 2 import java.awt.CardLayout; 3 import java.awt.Color; 4 import java.awt.Dimension; 5 import java.awt.Font; 6 import java.awt.Graphics; 7 import java.awt.GridLayout; 8 import java.awt.Image; 9 import java.awt.Label; 10 import java.awt.event.ActionEvent; 11 import java.awt.event.ActionListener; 12 import java.awt.event.WindowAdapter; 13 import java.awt.event.WindowEvent; 14 15 import javax.swing.BorderFactory; 16 import javax.swing.ImageIcon; 17 import javax.swing.JFrame; 18 import javax.swing.JLabel; 19 import javax.swing.JMenu; 20 import javax.swing.JMenuBar; 21 import javax.swing.JMenuItem; 22 import javax.swing.JPanel; 23 24 public class UserMain extends JFrame implements ActionListener { 25 private String name; 26 public void setName(String name) { 27 this.name = name; 28 } 29 30 public String getName() { 31 return name; 32 } 33 34 private CardLayout cl; 35 36 public CardLayout getCl() { 37 return cl; 38 } 39 40 //所用面板 41 private JPanel main = new JPanel(); 42 public JPanel getMain() { 43 return main; 44 } 45 JPanel barPanel = new JPanel(); 46 JPanel userPanel = new JPanel(); 47 JPanel jpDefault = new JPanel(); 48 JPanel eastPanel = new JPanel(); 49 JPanel southPanel = new JPanel(); 50 JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7; 51 52 //所用菜单组件 53 JMenuBar mBar = new JMenuBar(); 54 JMenu myOrder = new JMenu("我的订单"); 55 JMenuItem oQuery = new JMenuItem("订单查询"); 56 JMenuItem oAdd = new JMenuItem("添加订单"); 57 JMenuItem oDel = new JMenuItem("取消订单"); 58 JMenu uInfo = new JMenu("我的信息"); 59 JMenuItem uQuery = new JMenuItem("查询"); 60 JMenuItem uModify = new JMenuItem("修改"); 61 JMenu hInfo = new JMenu("酒店信息"); 62 JMenuItem hQuery = new JMenuItem("查询"); 63 JMenu option = new JMenu("选择"); 64 JMenuItem help = new JMenuItem("帮助"); 65 JMenuItem back = new JMenuItem("注销"); 66 67 public static void main(String[] args) { 68 new UserMain("KK"); 69 } 70 71 UserMain(String name) { 72 this.name = name; 73 74 this.setTitle(name +"用户,欢迎您!"); 75 this.setLayout(new BorderLayout()); 76 this.setResizable(false); 77 78 //放置背景图片 79 ImageIcon background = new ImageIcon(LoginFrame.class.getClassLoader().getResource("images/Umain.jpg")); 80 JLabel bkLabel = new JLabel(background); 81 bkLabel.setBounds(0, 0,background.getIconWidth(), background.getIconHeight()); 82 this.setSize(background.getIconWidth(), background.getIconHeight()); 83 this.getLayeredPane().add(bkLabel,new Integer(Integer.MIN_VALUE)); 84 JPanel ctPanel = (JPanel)this.getContentPane(); 85 ctPanel.setOpaque(false); 86 87 //设置面板透明度,布局,和菜单组合 88 main.setOpaque(false); 89 jpDefault.setOpaque(false); 90 barPanel.setOpaque(false); 91 userPanel.setOpaque(false); 92 eastPanel.setOpaque(false); 93 southPanel.setOpaque(false); 94 main.setLayout(new CardLayout()); 95 barPanel.setLayout(new BorderLayout()); 96 //main.setBackground(new Color(255, 250 ,250)); 97 uInfo.add(uQuery); 98 uInfo.add(uModify); 99 hInfo.add(hQuery); 100 myOrder.add(oQuery); 101 myOrder.add(oAdd); 102 myOrder.add(oDel); 103 option.add(help); 104 option.add(back); 105 mBar.add(uInfo); 106 mBar.add(hInfo); 107 mBar.add(myOrder); 108 mBar.add(option); 109 mBar.setOpaque(false); 110 barPanel.add(mBar,BorderLayout.EAST); 111 112 //设置标签样式,面板大小等 113 uInfo.setForeground(Color.WHITE); 114 hInfo.setForeground(Color.WHITE); 115 myOrder.setForeground(Color.WHITE); 116 option.setForeground(Color.WHITE); 117 myOrder.setFont(new java.awt.Font("微软楷体", Font.BOLD, 18)); 118 uInfo.setFont(new java.awt.Font("微软楷体", Font.BOLD, 18)); 119 hInfo.setFont(new java.awt.Font("微软楷体", Font.BOLD, 18)); 120 option.setFont(new java.awt.Font("微软楷体", Font.BOLD, 18)); 121 myOrder.setPreferredSize(new Dimension(120,20)); 122 uInfo.setPreferredSize(new Dimension(150,20)); 123 hInfo.setPreferredSize(new Dimension(150,20)); 124 option.setPreferredSize(new Dimension(80,20)); 125 barPanel.setPreferredSize(new Dimension(940,158)); 126 userPanel.setPreferredSize(new Dimension(380,400)); 127 eastPanel.setPreferredSize(new Dimension(60,400)); 128 southPanel.setPreferredSize(new Dimension(940,50)); 129 mBar.setBorder(BorderFactory.createEmptyBorder()); 130 131 132 main.add(jpDefault); 133 this.add(barPanel,BorderLayout.NORTH); 134 this.add(userPanel,BorderLayout.WEST); 135 this.add(main,BorderLayout.CENTER); 136 this.add(eastPanel,BorderLayout.EAST); 137 this.add(southPanel,BorderLayout.SOUTH); 138 this.pack(); 139 140 oQuery.addActionListener(this); 141 oAdd.addActionListener(this); 142 oDel.addActionListener(this); 143 uQuery.addActionListener(this); 144 uModify.addActionListener(this); 145 hQuery.addActionListener(this); 146 help.addActionListener(this); 147 back.addActionListener(this); 148 this.addWindowListener(new WindowAdapter() { 149 public void windowClosing(WindowEvent arg0) { 150 System.exit(0); 151 } 152 }); 153 setVisible(true); 154 } 155 156 public void actionPerformed(ActionEvent e) { 157 cl = (CardLayout)main.getLayout(); 158 cl.setHgap(0); cl.setVgap(0); 159 160 if(e.getSource() == uQuery){ 161 jp1 = (JPanel)new UserCard("用户查询",this); 162 main.add("用户查询",jp1); 163 cl.show(main,"用户查询"); 164 }else if(e.getSource() == uModify){ 165 jp2 = (JPanel)new UserCard("用户修改",this); 166 main.add("用户修改",jp2); 167 cl.show(main,"用户修改"); 168 }else if(e.getSource() == hQuery) { 169 jp3 = (JPanel)new UserCard("酒店查询",this); 170 main.add("酒店查询",jp3); 171 cl.show(main,"酒店查询"); 172 }else if(e.getSource() == oQuery) { 173 jp4 = (JPanel)new UserCard("订单查询",this); 174 main.add("订单查询",jp4); 175 cl.show(main, "订单查询"); 176 }else if(e.getSource() == oAdd) { 177 jp5 = (JPanel)new UserCard("订单添加",this); 178 main.add("订单添加",jp5); 179 cl.show(main, "订单添加"); 180 }else if(e.getSource() == oDel) { 181 jp6 = (JPanel)new UserCard("订单删除",this); 182 main.add("订单删除",jp6); 183 cl.show(main, "订单删除"); 184 }else if (e.getSource() == help) { 185 System.out.println("hhhh"); 186 jp7 = (JPanel)new UserCard("帮助",this); 187 main.add("帮助",jp7); 188 cl.show(main,"帮助"); 189 }else if (e.getSource() == back) { 190 this.setVisible(false); 191 new LoginFrame(); 192 } 193 194 } 195 196 }
通过show()方法显示指定“卡片”,不同”卡片”指向继承JPanel的不同类。
效果图:
因为是Java初学者,窗体的很多布局不是很了解,为了让窗体看上去好看,只有硬塞一张处理过的图片了,嘿嘿--
Java中CardLayout的使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。