高手,我弄了一个Swing的Test类,代码如下:
package c_port_packageBJTeacher;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
/**
* Java中CardLayout布局管理器使用的小例子
*/
public class Test extends JFrame implements ActionListener, Runnable {
/**
*
*/
private static final long serialVersionUID = 1L;
public ArrayList<JTextArea> al = new ArrayList<JTextArea>();
private JPanel pane = null; // 主要的JPanel,该JPanel的布局管理将被设置成CardLayout
private JPanel p = null; // 放按钮的JPanel
private CardLayout card = null; // CardLayout布局管理器
private JButton button_1 = null; // 上一步
private JButton button_2 = null; // 下一步
public LinkedList<JTextArea> ld = new LinkedList<JTextArea>();
String[] title = new String[]{"测试功能1","测试功能2","测试功能3","测试功能4"};
private JLabel lb = null;
private int currenPage = 1;
int o = 0;
private int myLocalPagenum = 0;
Font fnt = new Font("华文中宋", Font.BOLD, 14);
String[] MyJButtonDIYContent = new String[301];
LinkedList<String> ld0 = new LinkedList<String>();
public Test(String str, int pagenum) {
super("测试功能1");
ld0.add("测试功能1");
ld0.add("测试功能2");
ld0.add("测试功能3");
ld0.add("测试功能4");
for(int i =0;i<301;i++){
MyJButtonDIYContent[i]="CJCO";
}
// ////System.out.println(pagenum);
myLocalPagenum = pagenum;
card = new CardLayout(5, 5);
pane = new JPanel(card);
this.setResizable(false);
try {
// 将LookAndFeel设置成Windows样式
// UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception ex) {
ex.printStackTrace();
}
/** 创建一个具有指定的水平和垂直间隙的新卡片布局 */
p = new JPanel(); // 构造放按钮的JPanel
button_1 = new JButton("< 上一页");
button_2 = new JButton("下一页 >");
lb = new JLabel("第1页");
p.add(button_1);
p.add(button_2);
p.add(lb);
button_1.setEnabled(false);
if (myLocalPagenum == 1)
button_2.setEnabled(false);
/** 下面是翻转到卡片布局的某个组件,可参考API中的文档 */
button_1.addActionListener(new ActionListener() { // 上一步的按钮动作
public void actionPerformed(ActionEvent e) {
currenPage--;
o++;
if (currenPage == 1)
button_1.setEnabled(false);
if (currenPage < myLocalPagenum)
button_2.setEnabled(true);
lb.setText("第" + currenPage + "页");
card.previous(pane);
Test.this.setTitle(title[o]);
}
});
button_2.addActionListener(new ActionListener() { // 下一步的按钮动作
public void actionPerformed(ActionEvent e) {
currenPage++;
o--;
if (currenPage == myLocalPagenum)
button_2.setEnabled(false);
if (currenPage > 1)
button_1.setEnabled(true);
lb.setText("第" + currenPage + "页");
card.next(pane);
Test.this.setTitle(title[o]);
}
});
this.getContentPane().add(pane, BorderLayout.NORTH);
this.getContentPane().add(p, BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setSize(332, 722);
JPanel container = new JPanel(new GridLayout(5,10,5,5));
for(int i = 1; i < 51; ++i){
int j =0;
j = i+100;
JButton jb = new JButton("<html>"+MyJButtonDIYContent[i+100]+j+"</html>");
jb.setFont(fnt);
container.add(jb);
}
JPanel container0 = new JPanel(new GridLayout(5,10,5,5));
for(int i = 1; i < 51; ++i){
int j =0;
j = j+150;
JButton jb = new JButton("<html>"+MyJButtonDIYContent[i+150]+j+"</html>");
jb.setFont(fnt);
container0.add(jb);
}
JPanel container1 = new JPanel(new GridLayout(5,10,5,5));
for(int i = 1; i < 51; ++i){
int j =0;
j = i +200;
JButton jb = new JButton("<html>"+MyJButtonDIYContent[i+200]+j+"</html>");