/
* @(#)CardLayoutDemo.java
*
*
* @author
* @version java100个基础小程序 1.00 2008/8/10
*/
import java.awt.*;
import java.awt.event.*;
public class CardLayoutDemo extends Frame{
/
*
*/
private static final long serialVersionUID = 1L;
/
* Creates a new instance of <code>CardLayoutDemo</code>.
*/
Panel pnlCommondArea=new Panel();
Panel pnlDisplayArea=new Panel();
CardLayout cardlayout1=new CardLayout();
Button btnFirst=new Button("第一个");
Button btnPrevious=new Button("前一个");
Button btnNext=new Button("后一个");
Button btnLast=new Button("最后一个");
public CardLayoutDemo() {
this.setLayout(new BorderLayout());
this.add(pnlCommondArea,BorderLayout.NORTH);
this.add(pnlDisplayArea,BorderLayout.CENTER);
pnlDisplayArea.setLayout(cardlayout1);
Panel pnlFirst=new Panel();
pnlFirst.setBackground(Color.blue);
pnlFirst.setForeground(Color.white); //设置字的颜色
pnlDisplayArea.add("first",pnlFirst);
pnlFirst.add(new Label("This is the first Panel!"));
Panel pnlSecond=new Panel();
pnlSecond.setBackground(Color.red);
pnlSecond.setForeground(Color.blue);
pnlDisplayArea.add("second",pnlSecond);
pnlSecond.add(new Label("This is the second Panel!"));
Panel pnlThird=new Panel();
pnlThird.setBackground(Color.black);
pnlThird.setForeground(Color.white);
pnlDisplayArea.add("third",pnlThird);
pnlThird.add(new Label("This is the third Panel!"));
Panel pnlFourth=new Panel();
pnlFourth.setBackground(Color.green);
pnlFourth.setForeground(Color.black);
pnlDisplayArea.add("fourth",pnlFourth);
pnlFourth.add(new Label("This is the fourth Panel!"));
Panel pnlFifth=new Panel();
pnlFifth.setBackground(Color.cyan);
pnlFifth.setForeground(Color.GRAY);
pnlDisplayArea.add("fifth",pnlFifth);
pnlFifth.add(new Label("This is the fifth Panel!"));
btnFirst.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{

processAction(e);
}
});
btnPrevious.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processAction(e);
}
});
btnNext.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processAction(e);
}
});
btnLast.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processAction(e);
}
});
pnlCommondArea.add(btnFirst);
pnlCommondArea.add(btnPrevious);
pnlCommondArea.add(btnNext);
pnlCommondArea.add(btnLast);
}
/
* @param args the command line arguments
*/
@SuppressWarnings("deprecation")
public static void main(String[] args) {
// TODO code application logic here
CardLayoutDemo frmCardLayout=new CardLayoutDemo();
frmCardLayout.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frmCardLayout.pack();
frmCardLayout.show();
}
public Dimension getPreferredSize()
{
return new Dimension(300,300);
}
private void processAction(ActionEvent e)
{
Button btnEvent=(Button)e.getSource();
if(btnEvent.equals(btnFirst))
cardlayout1.first(pnlDisplayArea);
else if(btnEvent.equals(btnLast))
cardlayout1.last(pnlDisplayArea);
else if(btnEvent.equals(btnPrevious))
cardlayout1.previous(pnlDisplayArea);
else if(btnEvent.equals(btnNext))
cardlayout1.next(pnlDisplayArea);
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/2188.html