这是CardLayout使用的例子,可是图片怎么弄呢?需要添加路径的么
import java.awt.*;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class CardJButton extends JFrame{
private CardLayout cl = new CardLayout();
private Container container = getContentPane();
public CardJButton(){
JButton button ;
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
cl.next(container);
}
};
setLayout(cl);
for(int index = 0 ; index < 12 ;index ++){
button = new JButton(new ImageIcon("image/T"+index+".gif"));
add(button,Integer.toString(index));
button.addActionListener(listener);
}
}
public static void main(String[] args){
CardJButton frame = new CardJButton();
frame.setTitle("测试CardLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 150);
frame.setVisible(true);
}
}