栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

以随机顺序显示CardLayout的卡片?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

以随机顺序显示CardLayout的卡片?

这是直接跳转到卡片的简单方法。

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/574646.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号