/
* @(#)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://www.bianchenghao6.com/h6javajc/25963.html