java卡片布局show的使用。下面代码中LayoutDemo中“日记本”按钮显示TestFlowLayoutone 代码
packagem;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;classTestFlowLay...
package m;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TestFlowLayoutone extends JPanel {//使用浮动布局
JButton b1, b2, b3, b4, b5, b6;
TestFlowLayoutone() {
setLayout(new FlowLayout());//从左到右,从上到下,流式布局
b1 = new JButton("one");
b2 = new JButton("two");
b3 = new JButton("three");
b4 = new JButton("four");
b5 = new JButton("five");
b6 = new JButton("six");
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
}
}
class TestCardLayout extends JPanel {//使用卡片布局
CardLayout cd = new CardLayout();
TestCardLayout() {
setLayout(cd);//卡片布局,每张卡片显示一种布局效果
add("FlowLayout", new TestFlowLayoutone());
}
}
class LayoutDemo extends JFrame implements ActionListener {
TestCardLayout top = new TestCardLayout();
JPanel bottom = new JPanel();
JButton b1,b2, b3, b4;
public LayoutDemo() {
this.getContentPane().setLayout(new BorderLayout());
b1 = new JButton("日记本");
b2 = new JButton("时间表");
b3 = new JButton("记账薄");
b4 = new JButton("帮助");
bottom.add(b1);
bottom.add(b2);
bottom.add(b3);
bottom.add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
this.getContentPane().add(top, "Center");
this.getContentPane().add(bottom, "South");
setSize(400, 300);
setTitle("小管家");
setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
top.cd.next(top); // cl.show (panelMain, "pf");卡片对象名称.show(容器名称,卡片名称)
} else if (e.getSource() == b2) {
top.cd.previous(top);
} else if (e.getSource() == b3) {
top.cd.first(top);
} else {
top.cd.last(top);
}
}
public static void main(String args[]) {
JFrame.setDefaultLookAndFeelDecorated(true);
new LayoutDemo();
}
} 展开
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TestFlowLayoutone extends JPanel {//使用浮动布局
JButton b1, b2, b3, b4, b5, b6;
TestFlowLayoutone() {
setLayout(new FlowLayout());//从左到右,从上到下,流式布局
b1 = new JButton("one");
b2 = new JButton("two");
b3 = new JButton("three");
b4 = new JButton("four");
b5 = new JButton("five");
b6 = new JButton("six");
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
}
}
class TestCardLayout extends JPanel {//使用卡片布局
CardLayout cd = new CardLayout();
TestCardLayout() {
setLayout(cd);//卡片布局,每张卡片显示一种布局效果
add("FlowLayout", new TestFlowLayoutone());
}
}
class LayoutDemo extends JFrame implements ActionListener {
TestCardLayout top = new TestCardLayout();
JPanel bottom = new JPanel();
JButton b1,b2, b3, b4;
public LayoutDemo() {
this.getContentPane().setLayout(new BorderLayout());
b1 = new JButton("日记本");
b2 = new JButton("时间表");
b3 = new JButton("记账薄");
b4 = new JButton("帮助");
bottom.add(b1);
bottom.add(b2);
bottom.add(b3);
bottom.add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
this.getContentPane().add(top, "Center");
this.getContentPane().add(bottom, "South");
setSize(400, 300);
setTitle("小管家");
setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
top.cd.next(top); // cl.show (panelMain, "pf");卡片对象名称.show(容器名称,卡片名称)
} else if (e.getSource() == b2) {
top.cd.previous(top);
} else if (e.getSource() == b3) {
top.cd.first(top);
} else {
top.cd.last(top);
}
}
public static void main(String args[]) {
JFrame.setDefaultLookAndFeelDecorated(true);
new LayoutDemo();
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询