📄 panllayout.java
字号:
package layout;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class ButtonsPanel extends Panel {
//利用该类将5个按钮依BroderLayout组成一组,以备放入各卡片
ButtonsPanel(String id) {
setLayout(new BorderLayout());
add("Center",new Button(id));
add("North",new Button(id));
add("South",new Button(id));
add("East",new Button(id));
add("West",new Button(id));
}
}
public class PanlLayout extends Applet {
Button first = new Button("First card"), second = new Button("Second card"),
third = new Button("Third card"); //定义三个按钮
Panel cards = new Panel(); //定义一个面板cards
CardLayout cl=new CardLayout();
public void init() {
setLayout(new BorderLayout()); //设置applet的布局
Panel p = new Panel(); //定义一个面板p
p.setLayout(new FlowLayout()); //面板p的布局
p.add(first);
p.add(second);
p.add(third); //面板p中放入三个按钮
add("North", p); //将面板p放在applet显示区域的北方
cards.setLayout(cl); //设置面板cards的布局
cards.add("card1",new ButtonsPanel("in the first card"));
cards.add("card2", new ButtonsPanel("in the second card"));
cards.add("card3",new ButtonsPanel("in the third card"));
add("Center", cards);
//cards面板中加入三个ButtonsPanel,既三张卡片。放在applet显示区域中间
first.addActionListener(new ActionListenerOfButton ());
second.addActionListener(new ActionListenerOfButton ());
third.addActionListener(new ActionListenerOfButton ());
//设置面板p中按钮的监听者为ActionListenerOfButton类对象
}
class ActionListenerOfButton implements ActionListener {
//ActionListenerOfButton类确定显示哪张卡片
public void actionPerformed(ActionEvent e){
Object source=e.getSource();
//用getSource()方法取得事件源,见5.2.2节
if ( source==first ) cl.first(cards);
//若按下first card按钮,显示第一张cards
else if ( source==second ) {cl.first(cards); cl.next(cards);}
//若按下second card按钮,显示第一张cards的下一张。
else if ( source==third ) cl.last(cards);
//若按下third card按钮,显示最后一张cards
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -