简体   繁体   English

我正在使用Card Layout在面板之间切换,但是当我尝试在按下按钮后使特定面板出现时,什么也没有发生

[英]I am using Card Layout to switch between panels but when I try to make a specific panel appear after pressing a button nothing happens

I am trying to get it so that when the next button is pressed it goes to the Customer page, but when I click the button nothing happens. 我试图获取它,以便在按下下一个按钮时转到“客户”页面,但是当我单击该按钮时,什么都没有发生。

This is my code package javaapplication1; 这是我的代码包javaapplication1;

import javax.swing.*;
import java.lang.*;
import java.awt.*;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;

public class Test extends javax.swing.JPanel implements  ActionListener{

JButton firstButton, lastButton, nextButton, previousButton;
JPanel cardPanel;
String cardNames[] = new String[3];
int cardCounter = 0;

public JPanel createContentPane (){

    JPanel totalGUI = new JPanel();

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));

    buttonPanel.add(Box.createRigidArea(new Dimension(10,0)));

    previousButton = new JButton("<- Previous");
    previousButton.addActionListener(this);
    buttonPanel.add(previousButton);
    buttonPanel.add(Box.createHorizontalGlue());        

    firstButton = new JButton("First");
    firstButton.addActionListener(this);
    buttonPanel.add(firstButton);
    buttonPanel.add(Box.createHorizontalGlue());

    lastButton =  new JButton("Last");
    lastButton.addActionListener(this);
    buttonPanel.add(lastButton);
    buttonPanel.add(Box.createHorizontalGlue());

    nextButton = new JButton("Next ->");
    nextButton.addActionListener(this);
    buttonPanel.add(nextButton);
    buttonPanel.add(Box.createRigidArea(new Dimension(10,0)));

    JPanel Shop = new JPanel();
    Shop panel1 = new Shop(); 
    Shop.add(panel1);

    JPanel Items = new JPanel();
    Items panel2 = new Items(); 
    Items.add(panel2); 

    JPanel Customers = new JPanel();
    Customers panel3 = new Customers(); 
    Customers.add(panel3); 

    cardPanel = new JPanel(new CardLayout(150, 50));

    cardNames[0] = "Component.CENTER";
    cardNames[1] = "Component.BOTTOM_ALIGNMENT";
    cardNames[2] = "Component.TOP_ALIGNMENT";

    cardPanel.add(Shop, cardNames[0]);
    cardPanel.add(Items, cardNames[1]);
    cardPanel.add(Customers, cardNames[2]);

    JPanel bottomPanel = new JPanel();
    bottomPanel.setLayout(new BorderLayout());

    bottomPanel.add(cardPanel, BorderLayout.CENTER);
    bottomPanel.add(buttonPanel, BorderLayout.PAGE_START);

    totalGUI.add(bottomPanel);
    return totalGUI;
}

public void actionPerformed(ActionEvent e) {


    if(e.getSource() == nextButton)
    {
        CardLayout cl = (CardLayout)(cardPanel.getLayout());

        cl.show(cardPanel, "Customers");
    }
    /*
    if(e.getSource() == firstButton)
    {
        cl.first(cardPanel);
        cardCounter = 0;
    }
    else if(e.getSource() == lastButton)
    {
        cl.last(cardPanel);
        cardCounter = 2;
    }
    else 
    }
    else if(e.getSource() == previousButton)
    {
        cl.previous(cardPanel);
        if(cardCounter > 0)
        {
            cardCounter--;
        }
        else
        {
            cardCounter = 2;
        }*/
    }


protected static void createAndShowGUI() {

    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("");

    Test demo = new Test();
    frame.setContentPane(demo.createContentPane());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

} }

Why is this the case? 为什么会这样呢? Am I missing something? 我想念什么吗? I am only new to using Cardlayout and don't know how it fully works 我只是使用Cardlayout的新手,不知道它是如何工作的

When in doubt, check the documentation . 如有疑问,请查阅文档

From CardLayout.addLayoutComponent : CardLayout.addLayoutComponent

The object specified by constraints must be a string. constraints指定的对象必须是字符串。 The card layout stores this string as a key-value pair that can be used for random access to a particular card. 卡布局将此字符串存储为键值对,可用于随机访问特定卡。 By calling the show method, an application can display the component with the specified name. 通过调用show方法,应用程序可以显示具有指定名称的组件。

In other words, the String you pass to CardLayout.show must be identical to one of the strings you provided as a constraint when adding a component to the CardLayout. 换句话说,传递给CardLayout.show的String 必须与将组件添加到CardLayout时作为约束条件提供的字符串之一相同。

The constraints you are using are: 您使用的约束是:

  • "Component.CENTER" “ Component.CENTER”
  • "Component.BOTTOM_ALIGNMENT" “ Component.BOTTOM_ALIGNMENT”
  • "Component.TOP_ALIGNMENT" “ Component.TOP_ALIGNMENT”

None of these match what you are passing to CardLayout.show: 这些都不符合您传递给CardLayout.show的内容:

cl.show(cardPanel, "Customers");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Thymeleaf + Spring 启动。 我尝试制作动态字段,但是当按下按钮时什么也没有发生 - Thymeleaf + Spring Boot. I try to make dynamic fields, but when the button is pressed nothing happens at all 按下按钮应该可以启动服务并显示文本,但是当我单击按钮时什么也没有发生 - Pressing a button is supposed to start service and display text, but nothing happens when I click the button 当我尝试在Eclipse上创建新的Java项目时没有任何反应 - Nothing happens when I try to create a new java project on Eclipse 当我尝试在 Android 中创建目录时没有任何反应 - Nothing happens when I try to create directories in Android 如果我点击按钮,什么也不会发生 - Nothing happens if i click the button 如何使用CardLayout在面板之间切换? - How do I switch between panels using a CardLayout? (Java新手 - 面板转换)如何在框架中的面板之间切换 - (Java Newbie - Panel Transitions) How do I switch between panels in a frame 当我单击“检查忙框”按钮时,没有任何反应 - Nothing happens when I click on the “Check For BusyBox” button 当我点击android中的任何按钮时没有任何反应 - nothing happens when i click on any button in android 每次在JFrame中单击任何按钮时,刷新/重新创建卡布局中的所有面板。 - Refresh/Recreate All Panel in a Card Layout each time any button is clicked in JFrame Holding these Panels
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM