java - KET 测试项目的 swing 导航

标签 java swing testing navigation

我正在为这个 KET 测试项目收集资料。但是真正困扰我的一件事是这个页面导航,我不知道如何让它正常工作。我正在尝试使用 CardsLayout。但它不能正常工作。我需要有关如何设置这些 Pane 的帮助。

在底部,我需要制作问题编号,一开始我想我只会制作 5 个。当按下数字时,它会导航到由该编号标记的页面。下一步和后退按钮导航 +- 一页,结果按钮转到总结测试的最后一页。

enter image description here

最佳答案

请看下面给出的代码。它是用于设置和处理 CardLayout 的非常基本的程序,非常符合您正在寻找的要求。我希望它能帮助您更好地理解 CardLayout 的使用和处理。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class CardLayoutDemo extends JFrame implements ActionListener
{
    JButton next;
    JButton prev;
    JButton[] buttons ;
    JPanel[]  cards;
    JPanel    centerPanel;//Going to add all cards
    JPanel lowerPanel;
    int currentButton = 0;
    public void prepareAndShowGUI() 
    {
        next      = new JButton("Next>>");
        prev      = new JButton("<<Previous");
        buttons   = new JButton[10];
        String[]  cardsName = {"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN"};
        cards     = new JPanel[10];
        centerPanel= new JPanel();
        lowerPanel = new JPanel();
        centerPanel.setLayout(new CardLayout());
        for (int i = 0; i < buttons.length ; i++ )
        {
            buttons[i] = new JButton(cardsName[i]);
            buttons[i].addActionListener(this);
            buttons[i].setActionCommand("CARDS"+i);
            lowerPanel.add(buttons[i]);
            cards[i] = new JPanel();
            cards[i].setLayout(new BorderLayout());
            cards[i].setBorder(BorderFactory.createTitledBorder("Card "+(i+1)));
            cards[i].add(new JLabel(cardsName[i], SwingUtilities.CENTER));
            centerPanel.add(cards[i], cardsName[i]);
        }
        getContentPane().add(centerPanel);
        getContentPane().add(lowerPanel,BorderLayout.SOUTH);
        getContentPane().add(next,BorderLayout.EAST);
        getContentPane().add(prev,BorderLayout.WEST);
        next.addActionListener(this);
        prev.addActionListener(this);
        prev.setEnabled(false);
        if (buttons.length == 1)
        {
            next.setEnabled(false);
        }
        setSize(700,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent evt) 
    {
        JButton clickedButton = (JButton)evt.getSource();
        String command  = clickedButton.getActionCommand();
        if (command.startsWith("CARDS"))
        {
            CardLayout cl = (CardLayout)(centerPanel.getLayout());
            cl.show(centerPanel, clickedButton.getText());
            currentButton = Integer.parseInt(command.substring("CARDS".length()));
            if (currentButton == 0)
            {
                prev.setEnabled(false);
                if (buttons.length > 1)
                {
                    next.setEnabled(true);
                }
            }
            else if (currentButton == buttons.length - 1)
            {
                next.setEnabled(false);
                if (buttons.length > 1)
                {
                    prev.setEnabled(true);
                }
            }
            else
            {
                if (buttons.length > 1)
                {
                    next.setEnabled(true);
                    prev.setEnabled(true);
                }
            }
        }
        else
        {
            if ("Next>>".equals(clickedButton.getText()))
            {
                buttons[++currentButton].doClick();
                if (currentButton == buttons.length - 1)
                {
                    next.setEnabled(false);
                    prev.requestFocus();
                }
                prev.setEnabled(true);
            }
            else if ("<<Previous".equals(clickedButton.getText()))
            {
                buttons[--currentButton].doClick();
                if (currentButton == 0)
                {
                    prev.setEnabled(false);
                    next.requestFocus();
                }
                next.setEnabled(true);
            }
        }

    }     
    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable() 
        {
            public void run() 
            {
                CardLayoutDemo cld = new CardLayoutDemo();
                cld.prepareAndShowGUI();
            }
        });
    }
}

To know more about how to use CardLayout watch this official tutorial .

关于java - KET 测试项目的 swing 导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14675717/

相关文章:

java - 无法实例化内部类

java - 如何从用户那里读取 10 个整数,并在不使用数组的情况下识别第二大整数?

java - 将 JButton 设置为另一个 JButton 的位置

java - 无法让计算器 JApplet GUI 以有序的方式显示按钮

java - Jtextpane 复制彩色文本并粘贴

testing - 将 Cookie 作为请求 header 传递 - SSO JMeter

java - Apache Web 服务器获取 - 文件大小

java - java.util.Collection 为什么不实现新的 Stream 接口(interface)?

javascript - 从 React 中的两个测试库渲染助手组成单个渲染助手

testing - 如何测试使用从另一个模块导入的客户端 API 函数的函数?