CardLayout在Swing组件中的用法
推荐于2017-09-03
展开全部
一个容器如果是CardLayout布局,这个容器中有多个组件,只会看到最上面的组件,重叠起来的就像重叠起来的扑克牌,每次中能看到第一张。如果要看到其他组件调用show方法。下面一个例子可能理解理解:
mport java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TestImage implements ActionListener{
JPanel jp1=new JPanel();
CardLayout cl=new CardLayout();
Timer timer=new Timer(500,this);
public TestImage(){
JFrame jf = new JFrame("图片浏览器");
jp1.setLayout(cl);
String []name={"1.JPG","2.JPG","3.JPG","4.JPG","5.JPG","6.JPG","7.JPG",};
for(int i=0;i<name.length;i++){
Icon ic=new ImageIcon("d:\\1\\"+name[i]);
JLabel jl=new JLabel(ic);
jp1.add(jl,i+"");
}
jf.add(jp1);
JPanel jp2=new JPanel();
String[]s={"第一张","上一张","下一张","最后一张","播放","暂停"};
for(int i=0;i<s.length;i++){
JButton jb=new JButton(s[i]);
jb.addActionListener(this);
jp2.add(jb);
}
jf.add(jp2,BorderLayout.SOUTH);
//jf.setSize(800,500);
jf.pack();
jf.setLocation(500, 100);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String s=e.getActionCommand();
if("上一张".equals(s))
cl.previous(jp1);
else if("下一张".equals(s))
cl.next(jp1);
else if("最后一张".equals(s))
cl.last(jp1);
else if("第一张".equals(s))
cl.first(jp1);
else if("播放".equals(s))
timer.start();
else if("暂停".equals(s))
timer.stop();
else
cl.next(jp1);
}
public static void main(String[] args) {
new TestImage();
}
}
mport java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TestImage implements ActionListener{
JPanel jp1=new JPanel();
CardLayout cl=new CardLayout();
Timer timer=new Timer(500,this);
public TestImage(){
JFrame jf = new JFrame("图片浏览器");
jp1.setLayout(cl);
String []name={"1.JPG","2.JPG","3.JPG","4.JPG","5.JPG","6.JPG","7.JPG",};
for(int i=0;i<name.length;i++){
Icon ic=new ImageIcon("d:\\1\\"+name[i]);
JLabel jl=new JLabel(ic);
jp1.add(jl,i+"");
}
jf.add(jp1);
JPanel jp2=new JPanel();
String[]s={"第一张","上一张","下一张","最后一张","播放","暂停"};
for(int i=0;i<s.length;i++){
JButton jb=new JButton(s[i]);
jb.addActionListener(this);
jp2.add(jb);
}
jf.add(jp2,BorderLayout.SOUTH);
//jf.setSize(800,500);
jf.pack();
jf.setLocation(500, 100);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String s=e.getActionCommand();
if("上一张".equals(s))
cl.previous(jp1);
else if("下一张".equals(s))
cl.next(jp1);
else if("最后一张".equals(s))
cl.last(jp1);
else if("第一张".equals(s))
cl.first(jp1);
else if("播放".equals(s))
timer.start();
else if("暂停".equals(s))
timer.stop();
else
cl.next(jp1);
}
public static void main(String[] args) {
new TestImage();
}
}
追问
写的简单点吧,这太繁索了,
追答
呵呵
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestCardLayout implements ActionListener{
JPanel jp1=new JPanel();
JButton jButton=new JButton("上一句");
JButton jButton2=new JButton("下一句");
JButton jButton3=new JButton("第五句");
CardLayout cl=new CardLayout();
public TestCardLayout() {
JFrame jf = new JFrame("测试");
jp1.setLayout(cl);
jp1.add(new JLabel("很简单"),"1");
jp1.add(new JLabel("有界面,代码肯定少不了"),"2");
jp1.add(new JLabel("不要怕,先看!"),"3");
jp1.add(new JLabel("好啊!"),"4");
jp1.add(new JLabel("呼呼!"),"5");
jp1.add(new JLabel("真好玩!"),"6");
jp1.add(new JLabel("明白没"),"7");
jp1.add(new JLabel("要自己学会看API"),"8");
jp1.add(new JLabel("就这样!"),"9");
JPanel jPanel=new JPanel();
jPanel.add(jButton);
jPanel.add(jButton2);
jPanel.add(jButton3);
jButton.addActionListener(this);
jButton2.addActionListener(this);
jButton3.addActionListener(this);
jf.add(jp1,BorderLayout.CENTER);
jf.add(jPanel,BorderLayout.SOUTH);
jf.pack();
jf.setLocation(500, 100);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String string=e.getActionCommand();
if("上一句".equals(string))
cl.next(jp1);
if("下一句".equals(string))
cl.next(jp1);
if("第五句".equals(string))
cl.show(jp1,"5");
}
public static void main(String[] args) {
new TestCardLayout();
}
}
展开全部
一个容器如果是CardLayout布局,这个容器中有多个组件,只会看到最上面的组件,重叠起来的就像重叠起来的扑克牌,每次中能看到第一张。如果要看到其他组件调用show方法。下面一个例子可能理解理解:
mport java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TestImage implements ActionListener{
JPanel jp1=new JPanel();
CardLayout cl=new CardLayout();
Timer timer=new Timer(500,this);
public TestImage(){
JFrame jf = new JFrame("图片浏览器");
jp1.setLayout(cl);
String []name={"1.JPG","2.JPG","3.JPG","4.JPG","5.JPG","6.JPG","7.JPG",};
for(int i=0;i<name.length;i++){
Icon ic=new ImageIcon("d:\\1\\"+name[i]);
JLabel jl=new JLabel(ic);
jp1.add(jl,i+"");
}
jf.add(jp1);
JPanel jp2=new JPanel();
String[]s={"第一张","上一张","下一张","最后一张","播放","暂停"};
for(int i=0;i<s.length;i++){
JButton jb=new JButton(s[i]);
jb.addActionListener(this);
jp2.add(jb);
}
jf.add(jp2,BorderLayout.SOUTH);
//jf.setSize(800,500);
jf.pack();
jf.setLocation(500, 100);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String s=e.getActionCommand();
if("上一张".equals(s))
cl.previous(jp1);
else if("下一张".equals(s))
cl.next(jp1);
else if("最后一张".equals(s))
cl.last(jp1);
else if("第一张".equals(s))
cl.first(jp1);
else if("播放".equals(s))
timer.start();
else if("暂停".equals(s))
timer.stop();
else
cl.next(jp1);
}
public static void main(String[] args) {
new TestImage();
}
}
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestCardLayout implements ActionListener{
JPanel jp1=new JPanel();
JButton jButton=new JButton("上一句");
JButton jButton2=new JButton("下一句");
JButton jButton3=new JButton("第五句");
CardLayout cl=new CardLayout();
public TestCardLayout() {
JFrame jf = new JFrame("测试");
jp1.setLayout(cl);
jp1.add(new JLabel("很简单"),"1");
jp1.add(new JLabel("有界面,代码肯定少不了"),"2");
jp1.add(new JLabel("不要怕,先看!"),"3");
jp1.add(new JLabel("好啊!"),"4");
jp1.add(new JLabel("呼呼!"),"5");
jp1.add(new JLabel("真好玩!"),"6");
jp1.add(new JLabel("明白没"),"7");
jp1.add(new JLabel("要自己学会看API"),"8");
jp1.add(new JLabel("就这样!"),"9");
JPanel jPanel=new JPanel();
jPanel.add(jButton);
jPanel.add(jButton2);
jPanel.add(jButton3);
jButton.addActionListener(this);
jButton2.addActionListener(this);
jButton3.addActionListener(this);
jf.add(jp1,BorderLayout.CENTER);
jf.add(jPanel,BorderLayout.SOUTH);
jf.pack();
jf.setLocation(500, 100);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String string=e.getActionCommand();
if("上一句".equals(string))
cl.next(jp1);
if("下一句".equals(string))
cl.next(jp1);
if("第五句".equals(string))
cl.show(jp1,"5");
}
public static void main(String[] args) {
new TestCardLayout();
}
}
mport java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TestImage implements ActionListener{
JPanel jp1=new JPanel();
CardLayout cl=new CardLayout();
Timer timer=new Timer(500,this);
public TestImage(){
JFrame jf = new JFrame("图片浏览器");
jp1.setLayout(cl);
String []name={"1.JPG","2.JPG","3.JPG","4.JPG","5.JPG","6.JPG","7.JPG",};
for(int i=0;i<name.length;i++){
Icon ic=new ImageIcon("d:\\1\\"+name[i]);
JLabel jl=new JLabel(ic);
jp1.add(jl,i+"");
}
jf.add(jp1);
JPanel jp2=new JPanel();
String[]s={"第一张","上一张","下一张","最后一张","播放","暂停"};
for(int i=0;i<s.length;i++){
JButton jb=new JButton(s[i]);
jb.addActionListener(this);
jp2.add(jb);
}
jf.add(jp2,BorderLayout.SOUTH);
//jf.setSize(800,500);
jf.pack();
jf.setLocation(500, 100);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String s=e.getActionCommand();
if("上一张".equals(s))
cl.previous(jp1);
else if("下一张".equals(s))
cl.next(jp1);
else if("最后一张".equals(s))
cl.last(jp1);
else if("第一张".equals(s))
cl.first(jp1);
else if("播放".equals(s))
timer.start();
else if("暂停".equals(s))
timer.stop();
else
cl.next(jp1);
}
public static void main(String[] args) {
new TestImage();
}
}
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestCardLayout implements ActionListener{
JPanel jp1=new JPanel();
JButton jButton=new JButton("上一句");
JButton jButton2=new JButton("下一句");
JButton jButton3=new JButton("第五句");
CardLayout cl=new CardLayout();
public TestCardLayout() {
JFrame jf = new JFrame("测试");
jp1.setLayout(cl);
jp1.add(new JLabel("很简单"),"1");
jp1.add(new JLabel("有界面,代码肯定少不了"),"2");
jp1.add(new JLabel("不要怕,先看!"),"3");
jp1.add(new JLabel("好啊!"),"4");
jp1.add(new JLabel("呼呼!"),"5");
jp1.add(new JLabel("真好玩!"),"6");
jp1.add(new JLabel("明白没"),"7");
jp1.add(new JLabel("要自己学会看API"),"8");
jp1.add(new JLabel("就这样!"),"9");
JPanel jPanel=new JPanel();
jPanel.add(jButton);
jPanel.add(jButton2);
jPanel.add(jButton3);
jButton.addActionListener(this);
jButton2.addActionListener(this);
jButton3.addActionListener(this);
jf.add(jp1,BorderLayout.CENTER);
jf.add(jPanel,BorderLayout.SOUTH);
jf.pack();
jf.setLocation(500, 100);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String string=e.getActionCommand();
if("上一句".equals(string))
cl.next(jp1);
if("下一句".equals(string))
cl.next(jp1);
if("第五句".equals(string))
cl.show(jp1,"5");
}
public static void main(String[] args) {
new TestCardLayout();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询