我想为我的CardLayout显示卡或屏幕随机顺序。 我需要如何做到这一点的指导。 什么是战略,我应该使用?
我试着用下面的代码,但它是在一个固定的顺序。 我希望能够选择我喜欢哪个顺序。
编辑!
对不起,按随机顺序我不是故意洗牌。 但是,它是很好的了解。 我希望该程序的用户可以输入一些。 根据输入的值,则显示一个特定的屏幕/卡。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CardLayoutExample extends JFrame {
private int currentCard = 1;
private JPanel cardPanel;
private CardLayout cl;
public CardLayoutExample() {
setTitle("Card Layout Example");
setSize(300, 150);
cardPanel = new JPanel();
cl = new CardLayout();
cardPanel.setLayout(cl);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JLabel lab1 = new JLabel("Card1");
JLabel lab2 = new JLabel("Card2");
JLabel lab3 = new JLabel("Card3");
JLabel lab4 = new JLabel("Card4");
p1.add(lab1);
p2.add(lab2);
p3.add(lab3);
p4.add(lab4);
cardPanel.add(p1, "1");
cardPanel.add(p2, "2");
cardPanel.add(p3, "3");
cardPanel.add(p4, "4");
JPanel buttonPanel = new JPanel();
JButton b1 = new JButton("Previous");
JButton b2 = new JButton("Next");
buttonPanel.add(b1);
buttonPanel.add(b2);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (currentCard > 1) {
currentCard -= 1;
cl.show(cardPanel, "" + (currentCard));
}
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (currentCard < 4) {
currentCard += 1;
cl.show(cardPanel, "" + (currentCard));
}
}
});
getContentPane().add(cardPanel, BorderLayout.NORTH);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
CardLayoutExample cl = new CardLayoutExample();
cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cl.setVisible(true);
}
}
这里是直接跳转到一个卡的简单方法。
final JButton jumpTo = new JButton("Jump To");
buttonPanel.add(jumpTo);
jumpTo.addActionListener( new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae) {
String[] names = {"1","2","3","4"};
String s = (String)JOptionPane.showInputDialog(
jumpTo,
"Jump to card",
"Navigate",
JOptionPane.QUESTION_MESSAGE,
null,
names,
names[0]);
if (s!=null) {
cl.show(cardPanel, s);
}
}
} );
显然,这将需要一些改变代码的其余部分。 这里是一个SSCCE。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CardLayoutExample extends JFrame {
private int currentCard = 1;
private JPanel cardPanel;
private CardLayout cl;
public CardLayoutExample() {
setTitle("Card Layout Example");
setSize(300, 150);
cardPanel = new JPanel();
cl = new CardLayout();
cardPanel.setLayout(cl);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JLabel lab1 = new JLabel("Card1");
JLabel lab2 = new JLabel("Card2");
JLabel lab3 = new JLabel("Card3");
JLabel lab4 = new JLabel("Card4");
p1.add(lab1);
p2.add(lab2);
p3.add(lab3);
p4.add(lab4);
cardPanel.add(p1, "1");
cardPanel.add(p2, "2");
cardPanel.add(p3, "3");
cardPanel.add(p4, "4");
JPanel buttonPanel = new JPanel();
JButton b1 = new JButton("Previous");
JButton b2 = new JButton("Next");
buttonPanel.add(b1);
buttonPanel.add(b2);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (currentCard > 1) {
currentCard -= 1;
cl.show(cardPanel, "" + (currentCard));
}
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (currentCard < 4) {
currentCard += 1;
cl.show(cardPanel, "" + (currentCard));
}
}
});
final JButton jumpTo = new JButton("Jump To");
buttonPanel.add(jumpTo);
jumpTo.addActionListener( new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae) {
String[] names = {"1","2","3","4"};
String s = (String)JOptionPane.showInputDialog(
jumpTo,
"Jump to card",
"Navigate",
JOptionPane.QUESTION_MESSAGE,
null,
names,
names[0]);
if (s!=null) {
cl.show(cardPanel, s);
}
}
} );
getContentPane().add(cardPanel, BorderLayout.NORTH);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
CardLayoutExample cl = new CardLayoutExample();
cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cl.setVisible(true);
}
}
顺便说一句-我的评论“哪里是你提示用户输入卡号代码的一部分吗?” 实际上,试图与沟通。 对于更好地帮助越早,后期的一个非常微妙的方式SSCCE 。
把CartLayouts在一个列表,随机播放列表,添加到列表的顺序包含布局。