java - 如何使用卡片布局?

标签 java command-line cardlayout

我很难弄清楚什么是卡片布局。我阅读了很多文章并实现了这个小示例来了解卡片布局的工作原理。但我无法理解一些方法(已注释)。有人可以帮助我吗(我使用命令行)。

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

class C_layout implements ActionListener
{
    JButton b2;
    JButton b1;
    JFrame f1;
    JPanel card1;
    JPanel card2;
    JPanel Jp;
    void Example()
    {
    f1=new JFrame("CardLayout Exercise");
    f1.setLocationRelativeTo(null);
    f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f1.setSize(500,500);
    f1.setVisible(true);

    Container cont=f1.getContentPane();
    cont.setLayout(null);

    Jp=new JPanel(new CardLayout()); //<-- How to implement card layout here (MAIN PANEL)
    f1.add(Jp);
    Jp.setLayout //<-- Not sure what means here ERROR
    card1=new JPanel(); // First panel
    Jp.add(card1);
    card2=new JPanel(); // Second panel
    Jp.add(card2);

    JLabel lb1=new JLabel("This is the first Panel");
    lb1.setBounds(250,100,100,30);
    card1.add(lb1);

    b1=new JButton("NEXT >>");
    b1.setBounds(350,400,100,30);
    b1.addActionListener(this);
    card1.add(b1);


    JLabel lb2=new JLabel("This is the second Panel");
    lb2.setBounds(250,100,100,30);
    card2.add(lb2);

    b2=new JButton("<< PREV");
    b2.setBounds(250,300,100,30);
    b2.addActionListener(this);
    card2.add(b2);
    }

    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==b1)
    {
    CardLayout cardLayout = (CardLayout) Jp.getLayout();
    cardLayout.show(card2,"2");
    }
    if(e.getSource()==b2)
    {
    // I still haven't implemented this action listener
    }
    }
}

class LayoutDemo1
{
    public static void main(String[] args)
    {
    C_layout c=new C_layout();
    c.Example();


    }
}

最佳答案

cont.setLayout(null); 不好,坏主意,很快就失去它......

您将需要对 CardLayout 的引用才能对其进行管理。首先定义 CardLayout 的实例字段...

private  CardLayout cardLayout;

现在,创建 CardLayout 实例并将其应用到您的面板...

cardLayout = new CardLayout();
Jp=new JPanel(cardLayout);

这个...

Jp.setLayout //<-- Not sure what means here ERROR

没有做任何事情,就Java而言,它不是一个有效的语句,事实上,它实际上是一个方法,它应该引用你想要使用的LayoutManager,但是因为您在创建 Jp 实例时已经完成了该操作,所以您不需要它...

您将需要某种方法来识别要显示的组件,CardLayout 通过 String 名称来实现此目的,例如...

card1=new JPanel(); // First panel
Jp.add(card1, "card1");
card2=new JPanel(); // Second panel
Jp.add(card2, "card2");

现在,在您的 ActionListener 中,您想要要求 CardLayout 显示 所需的 View ...

public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==b1)
    {
        cardLayout.show(Jp1,"card2");
    } else if(e.getSource()==b2)
    {
        cardLayout.show(Jp1,"card1");
    }
}

请注意,为了使 CardLayout#show 正常工作,您需要为其分配 CardLayout 所分配到的容器的引用以及 View 的名称您想要显示。

参见How to Use CardLayout了解更多详情

关于java - 如何使用卡片布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27286895/

相关文章:

java - 我可以在 CardLayout 中设置单个面板的大小吗?

java - 这个变量是如何改变的?

java - Java中If/Else语句的使用考虑内存IO

java - 检查插件本应使用 "compile files"时是否使用了 "provided"

java - 如何使用 JSP 连接 MySQL 数据库并检索和检查数据?

matlab - 如何从命令行调用 MATLAB 并在退出前打印到标准输出

linux - 如何在 bash 打印字符串中插入 ($x-$y)

java - 卡片布局和对象?

python - 如何读取/处理命令行参数?

Java 使用 jbutton 在卡片之间切换