CardLayout在Swing组件中的用法
答案:2 悬赏:70 手机版
解决时间 2021-11-12 20:09
- 提问者网友:爱唱彩虹
- 2021-11-12 16:12
CardLayout在Swing组件中的用法
最佳答案
- 五星知识达人网友:拾荒鲤
- 2021-11-12 16:17
一个容器如果是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 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 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
JLabel jl=new JLabel(ic);
jp1.add(jl,i+"");
}
jf.add(jp1);
JPanel jp2=new JPanel();
String[]s={"第一张","上一张","下一张","最后一张","播放","暂停"};
for(int i=0;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();
}
}
全部回答
- 1楼网友:轻雾山林
- 2021-11-12 17:38
一个容器如果是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 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 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
JLabel jl=new JLabel(ic);
jp1.add(jl,i+"");
}
jf.add(jp1);
JPanel jp2=new JPanel();
String[]s={"第一张","上一张","下一张","最后一张","播放","暂停"};
for(int i=0;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();
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯