本文共 1546 字,大约阅读时间需要 5 分钟。
_(:з」∠)_在书本例子的基础上做了点改动
package t4;import java.awt.*;import javax.swing.*;import java.awt.event.*;//让包含事件源的容器对象来担任监听者public class App14_4 extends JFrame implements ActionListener { static App14_4 frm = new App14_4(); static JPanel cardPan = new JPanel(); //使用卡片布局的容器 static JPanel btPan = new JPanel(); //使用网格布局容器 static CardLayout crd = new CardLayout(5,10); //定义卡片布局对象crd static JButton bt1 = new JButton("第一页"); static JButton bt2 = new JButton("上一页"); static JButton bt3 = new JButton("下一页"); static JButton bt4 = new JButton("最后页"); public static void main(String[] args) { // TODO Auto-generated method stub bt1.addActionListener(frm); //向按钮们注册监听者frm bt2.addActionListener(frm); bt3.addActionListener(frm); bt4.addActionListener(frm); //设置窗口 frm.setLayout(null); frm.setTitle("卡片式布局策略CardLayout"); frm.setSize(350,300); frm.setResizable(false); //设置使用卡片布局的容器 cardPan.setLayout(crd); cardPan.setBounds(10, 10, 320, 200); for(int i=1;i<=4;i++) { cardPan.add(new JTextArea("第"+i+"页")); } //设置使用网格布局的容器 btPan.setLayout(new GridLayout(1,4)); btPan.setBounds(10, 220, 320, 25); btPan.add(bt1); btPan.add(bt2); btPan.add(bt3); btPan.add(bt4); frm.add(cardPan); frm.add(btPan); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setVisible(true); } public void actionPerformed(ActionEvent e) { JButton bt = (JButton)e.getSource(); if(bt == bt1) crd.first(cardPan); else if(bt == bt2) crd.previous(cardPan); else if(bt == bt3) crd.next(cardPan); else crd.last(cardPan); }}
转载地址:http://snnlf.baihongyu.com/